List of usage examples for org.opencv.core Mat convertTo
public void convertTo(Mat m, int rtype)
From source file:gab.opencv.OpenCV.java
License:Open Source License
public void findSobelEdges(int dx, int dy) { Mat sobeled = new Mat(getCurrentMat().height(), getCurrentMat().width(), CvType.CV_32F); Imgproc.Sobel(getCurrentMat(), sobeled, CvType.CV_32F, dx, dy); sobeled.convertTo(getCurrentMat(), getCurrentMat().type()); }
From source file:houghtransform.transform_process.MyTransform.java
@Override public void houghTransform(Mat edges) { Mat _edges = edges.clone(); double radian = Math.PI / 180; int degrees = (int) Math.floor(theta * 180 / Math.PI + 0.5); int w = _edges.cols(); int h = _edges.rows(); _edges.convertTo(_edges, CvType.CV_64FC3); int size = w * h; double[] img_data = new double[size]; _edges.get(0, 0, img_data); // Gets all pixels _img_w = w; //Number of columns _img_h = h; //Number of lines //Create the accumulator double hough_h = ((Math.sqrt(2.0) * (double) (h > w ? h : w)) / 2.0); _accu_h = (int) (hough_h * 2.0); // -r -> +r _accu_w = 180;//from w w w.ja v a2 s .c o m _accu = new int[_accu_h * _accu_w]; for (int i = 0; i < _accu_h * _accu_w; i++) { _accu[i] = 0; } double center_x = w / 2; double center_y = h / 2; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (img_data[(y * w) + x] > 250) { for (int t = 0; t < 180; t = t + degrees) { // y = x * cos( theta ) + y * sin( theta ) double r = (((double) x - center_x) * Math.cos((double) t * radian)) + (((double) y - center_y) * Math.sin((double) t * radian)); _accu[(int) ((Math.floor(r + hough_h) * 180.0)) + t]++; } } } } ArrayList<Point> lines = new ArrayList<>(); if (_accu.length == 0) try { throw new IOException("MyTransform: _accu == 0"); } catch (IOException ex) { System.out.println(ex); } for (int r = 0; r < _accu_h; r++) { for (int t = 0; t < _accu_w; t++) { // Searching in the accumulator a value greater //or equal to the set threshold if (((int) _accu[(r * _accu_w) + t]) >= threshold) { // Is this point a local maxima (9x9) int max = _accu[(r * _accu_w) + t]; //////////////////////////////// for (int ly = -4; ly <= 4; ly++) { for (int lx = -4; lx <= 4; lx++) { if (((ly + r) >= 0 && (ly + r) < _accu_h) && ((lx + t) >= 0 && (lx + t) < _accu_w)) { if ((int) _accu[((r + ly) * _accu_w) + (t + lx)] > max) { max = _accu[((r + ly) * _accu_w) + (t + lx)]; ly = lx = 5; } } } } ///////////////////////////////// if (max > (int) _accu[(r * _accu_w) + t]) continue; Point point = new Point(); point.x = r; point.y = t * radian; lines.add(point); } } } _lines = lines; }
From source file:imageanalysis.Analyzer.java
private Mat findDifferences() { Mat image = ImgTools.getImageFromClipboard(); // Gets images (both halves) Mat leftHalf = image.submat(left);/* w ww .j a v a 2 s .co m*/ Mat rightHalf = image.submat(right); // Computes their difference Mat diff1 = new Mat(); Mat diff2 = new Mat(); Core.subtract(leftHalf, rightHalf, diff1); Core.subtract(rightHalf, leftHalf, diff2); // Gets sum of both differences (image that highlightes different objects) Mat sum = new Mat(diff1.size(), CvType.CV_32F); Core.add(diff1, diff2, sum); // Normalize Core.normalize(sum, sum, 0, 255, Core.NORM_MINMAX); sum.convertTo(sum, CvType.CV_8U); return sum; }
From source file:logic.featurepointextractor.MouthFPE.java
/** * Detect mouth feature points//www . ja va 2 s .c o m * Algorithm: Equalize histogram of mouth rect * Implement Sobel horizontal filter * Find corners * Invert color + Binarization * Find lip up and down points * @param mc * @return */ @Override public Point[] detect(MatContainer mc) { /**Algorithm * find pix(i) = (R-G)/R * normalize: 2arctan(pix(i))/pi */ //find pix(i) = (R-G)/R Mat mouthRGBMat = mc.origFrame.submat(mc.mouthRect); List mouthSplitChannelsList = new ArrayList<Mat>(); Core.split(mouthRGBMat, mouthSplitChannelsList); //extract R-channel Mat mouthR = (Mat) mouthSplitChannelsList.get(2); mouthR.convertTo(mouthR, CvType.CV_64FC1); //extract G-channel Mat mouthG = (Mat) mouthSplitChannelsList.get(1); mouthG.convertTo(mouthG, CvType.CV_64FC1); //calculate (R-G)/R Mat dst = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); mc.mouthProcessedMat = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); Core.absdiff(mouthR, mouthG, dst); // Core.divide(dst, mouthR, mc.mouthProcessedMat); mc.mouthProcessedMat = dst; mc.mouthProcessedMat.convertTo(mc.mouthProcessedMat, CvType.CV_8UC1); Imgproc.equalizeHist(mc.mouthProcessedMat, mc.mouthProcessedMat); // Imgproc.blur(mc.mouthProcessedMat, mc.mouthProcessedMat, new Size(4,4)); // Imgproc.morphologyEx(mc.mouthProcessedMat, mc.mouthProcessedMat, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(4,4))); Imgproc.threshold(mc.mouthProcessedMat, mc.mouthProcessedMat, 230, 255, THRESH_BINARY); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(mc.mouthProcessedMat, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); //find the biggest contour int maxSize = -1; int tmpSize = -1; int index = -1; Rect centMouthRect = new Rect(mc.mouthRect.x + mc.mouthRect.width / 4, mc.mouthRect.y + mc.mouthRect.height / 4, mc.mouthRect.width / 2, mc.mouthRect.height / 2); if (contours.size() != 0) { maxSize = contours.get(0).toArray().length; tmpSize = 0; index = 0; } //find max contour for (int j = 0; j < contours.size(); ++j) { //if contour is vertical, exclude it Rect boundRect = Imgproc.boundingRect(contours.get(j)); int centX = mc.mouthRect.x + boundRect.x + boundRect.width / 2; int centY = mc.mouthRect.y + boundRect.y + boundRect.height / 2; // LOG.info("Center = " + centX + "; " + centY); // LOG.info("Rect = " + centMouthRect.x + "; " + centMouthRect.y); if (!centMouthRect.contains(new Point(centX, centY))) continue; tmpSize = contours.get(j).toArray().length; LOG.info("Contour " + j + "; size = " + tmpSize); if (tmpSize > maxSize) { maxSize = tmpSize; index = j; } } //appproximate curve Point[] p1 = contours.get(index).toArray(); MatOfPoint2f p2 = new MatOfPoint2f(p1); MatOfPoint2f p3 = new MatOfPoint2f(); Imgproc.approxPolyDP(p2, p3, 1, true); p1 = p3.toArray(); MatOfInt tmpMatOfPoint = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(p1), tmpMatOfPoint); Rect boundRect = Imgproc.boundingRect(new MatOfPoint(p1)); if (boundRect.area() / mc.mouthRect.area() > 0.3) return null; int size = (int) tmpMatOfPoint.size().height; Point[] _p1 = new Point[size]; int[] a = tmpMatOfPoint.toArray(); _p1[0] = new Point(p1[a[0]].x + mc.mouthRect.x, p1[a[0]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[0], 3, new Scalar(0, 0, 255), -1); for (int i = 1; i < size; i++) { _p1[i] = new Point(p1[a[i]].x + mc.mouthRect.x, p1[a[i]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[i], 3, new Scalar(0, 0, 255), -1); Core.line(mc.origFrame, _p1[i - 1], _p1[i], new Scalar(255, 0, 0), 2); } Core.line(mc.origFrame, _p1[size - 1], _p1[0], new Scalar(255, 0, 0), 2); /* contours.set(index, new MatOfPoint(_p1)); mc.mouthProcessedMat.setTo(new Scalar(0)); Imgproc.drawContours(mc.mouthProcessedMat, contours, index, new Scalar(255), -1); */ mc.mouthMatOfPoint = _p1; MatOfPoint matOfPoint = new MatOfPoint(_p1); mc.mouthBoundRect = Imgproc.boundingRect(matOfPoint); mc.features.mouthBoundRect = mc.mouthBoundRect; /**extract feature points: 1 most left * 2 most right * 3,4 up * 5,6 down */ // mc.mouthMatOfPoint = extractFeaturePoints(contours.get(index)); return null; }
From source file:mvision.Bhattacharyya.java
public Mat histogram(String img) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat image = Highgui.imread(img);/*from w w w. ja va2 s. c o m*/ //Mat image = Highgui.imread("C:\\image1.jpg"); //Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2); Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV); java.util.List<Mat> matList = new LinkedList<Mat>(); matList.add(image); Mat histogram = new Mat(); MatOfFloat ranges = new MatOfFloat(0, 256); MatOfInt histSize = new MatOfInt(255); Imgproc.calcHist(matList, new MatOfInt(0), new Mat(), histogram, histSize, ranges); // Create space for histogram image Mat histImage = Mat.zeros(100, (int) histSize.get(0, 0)[0], CvType.CV_8UC1); histogram.convertTo(histogram, CvType.CV_32F); // Normalize histogram Core.normalize(histogram, histogram, 1, histImage.rows(), Core.NORM_MINMAX, -1, new Mat()); // Draw lines for histogram points for (int i = 0; i < (int) histSize.get(0, 0)[0]; i++) { Core.line(histImage, new org.opencv.core.Point(i, histImage.rows()), new org.opencv.core.Point(i, histImage.rows() - Math.round(histogram.get(i, 0)[0])), new Scalar(255, 255, 255), 1, 8, 0); } return histogram; }
From source file:org.openpnp.vision.FluentCv.java
License:Open Source License
public static double calculatePsnr(Mat I1, Mat I2) { Mat s1 = new Mat(); Core.absdiff(I1, I2, s1); // |I1 - I2| s1.convertTo(s1, CvType.CV_32F); // cannot make a square on 8 bits s1 = s1.mul(s1); // |I1 - I2|^2 Scalar s = Core.sumElems(s1); // sum elements per channel double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels if (sse <= 1e-10) // for small values return zero return 0; else {// ww w. j a v a 2s . co m double mse = sse / (double) (I1.channels() * I1.total()); double psnr = 10.0 * Math.log10((255 * 255) / mse); return psnr; } }
From source file:org.pattern.detection.contour.ContourDetectionAlgorithm.java
@Override public List<? extends Particle> detectAndAssign(ParticleImage image) { // take the copy of image that we dont modify the original Mat img = new Mat(); image.getPixels().copyTo(img);//from w w w .j a v a 2 s. c o m // blur the image to denoise //Imgproc.blur(imagePixels, imagePixels, new Size(3, 3)); // thresholds the image Mat thresholded = new Mat(); // Imgproc.threshold(imagePixels, thresholded, // THRESHOLD, MAX, Imgproc.THRESH_TOZERO_INV); // thresholding Imgproc.adaptiveThreshold(img, thresholded, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 155, 15); Highgui.imwrite("1_thresholded.jpg", thresholded); Mat edges = new Mat(); Imgproc.Canny(img, edges, 100, 200); Highgui.imwrite("1_canny.jpg", edges); // remove small noises // Mat kernel = Mat.ones(new Size(3, 3), CvType.CV_8UC1); Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5, 5)); Imgproc.morphologyEx(thresholded, thresholded, Imgproc.MORPH_OPEN, kernel); Highgui.imwrite("2_opening.jpg", thresholded); // Imgproc.erode(thresholded, thresholded, kernel, ORIGIN, 3); // Highgui.imwrite("3_erode.jpg", thresholded); Mat distTransform = new Mat(); Imgproc.distanceTransform(thresholded, distTransform, Imgproc.CV_DIST_C, 5); distTransform.convertTo(distTransform, CvType.CV_8UC1); Imgproc.equalizeHist(distTransform, distTransform); Highgui.imwrite("4_distance_transform.jpg", distTransform); Mat markerMask = Mat.zeros(img.size(), CvType.CV_8UC1); double max = Core.minMaxLoc(distTransform).maxVal; Imgproc.threshold(distTransform, markerMask, max * 0.9, 255, Imgproc.THRESH_BINARY); markerMask.convertTo(markerMask, CvType.CV_8UC1); Highgui.imwrite("5_thresholded_distance.jpg", markerMask); List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(markerMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); Mat markers = Mat.zeros(img.size(), CvType.CV_32S); //markers.setTo(Scalar.all(0)); Random rand = new Random(); for (int idx = 0; idx < contours.size(); idx++) { Scalar color = new Scalar(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); Imgproc.drawContours(markers, contours, idx, color, -1); } Highgui.imwrite("6_markers.jpg", markers); Imgproc.cvtColor(img, img, Imgproc.COLOR_GRAY2RGB); img.convertTo(img, CvType.CV_8UC3); Imgproc.watershed(img, markers); Highgui.imwrite("7_wattershed.jpg", markers); // detect contours // List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); // create particle from each contour List<Particle> particles = new ArrayList<>(); int i = 0; for (MatOfPoint contour : contours) { Point cog = calcCog(contour); if (!Double.isNaN(cog.x) && !Double.isNaN(cog.y)) { System.out.println(cog); Particle p = new Particle(cog, contour); particles.add(p); // just for reorting reasons image.assign(p); } } return particles; }
From source file:processdata.depthDataProcessingUtilities.java
/** * converts depth data to opencv Mat object leaving depth values that are only within min and max thresholds * @param path// w ww.j a va 2 s. co m * @param minThreshold * @param maxThreshold * @return * @throws FileNotFoundException */ public static Mat processDepthDataFile(String path, int minThreshold, int maxThreshold) throws FileNotFoundException { File depthData = new File(path); double[][] depthDataArray = new double[1][217088]; //read depth data into array int count = 0; inDepthDataFile = new Scanner(depthData);//.useDelimiter(",\\s*"); while (inDepthDataFile.hasNext()) { String currentStr = inDepthDataFile.nextLine(); if (!currentStr.isEmpty()) depthDataArray[0][count++] = Double.parseDouble(currentStr); } double depthDataMatrix[][] = new double[512][424]; depthDataMatrix = reshape(depthDataArray, 512, 424); Mat matDepthDataMatrix = new Mat(512, 424, CvType.CV_64F); //cut-off the remaining depth values for (int i = 0; i < depthDataMatrix.length; i++) { for (int j = 0; j < depthDataMatrix[0].length; j++) { if (depthDataMatrix[i][j] > maxThreshold || depthDataMatrix[i][j] < minThreshold) depthDataMatrix[i][j] = 0; } } //find max value double max = 0; for (int i = 0; i < depthDataMatrix.length; i++) { for (int j = 0; j < depthDataMatrix[0].length; j++) { if (depthDataMatrix[i][j] > max) max = depthDataMatrix[i][j]; } } //FILL THE DEPTH MATRIX //System.out.println("Max Element "+ max); for (int i = 0; i < depthDataMatrix.length; i++) { for (int j = 0; j < depthDataMatrix[0].length; j++) { matDepthDataMatrix.put(i, j, depthDataMatrix[i][j] / max * 255.0); } } // //printout the depth matrix // for(int i = 0;i<depthDataMatrix.length;i++){ // for(int j = 0;j<depthDataMatrix[0].length;j++){ // System.out.print(depthDataMatrix[i][j]+"\t"); // } // System.out.println(); // } // //apply colormap to visualize Mat processedMathDepthImage = new Mat(matDepthDataMatrix.size(), CvType.CV_8U); matDepthDataMatrix.convertTo(processedMathDepthImage, CvType.CV_8UC1); Core.transpose(processedMathDepthImage, processedMathDepthImage); org.opencv.contrib.Contrib.applyColorMap(processedMathDepthImage, processedMathDepthImage, org.opencv.contrib.Contrib.COLORMAP_JET); return processedMathDepthImage; }
From source file:qupath.opencv.processing.PixelImageCV.java
License:Open Source License
public PixelImageCV(Mat mat) { // Extract dimensions and pixels this.width = (int) mat.size().width; this.height = (int) mat.size().height; pixels = new float[(int) mat.total()]; if (mat.depth() == CvType.CV_32F) mat.get(0, 0, pixels);/* www .j a va 2s . c o m*/ else { Mat mat2 = new Mat(); mat.convertTo(mat2, CvType.CV_32F); mat2.get(0, 0, pixels); } }
From source file:qupath.opencv.processing.PixelImageCV.java
License:Open Source License
public void put(Mat mat) { if (mat.depth() == CvType.CV_32F) mat.put(0, 0, pixels);// w w w.jav a 2s . c om else { Mat mat2 = new Mat(new Size(width, height), CvType.CV_32F); mat2.put(0, 0, pixels); mat2.convertTo(mat, mat.depth()); } }