List of usage examples for org.opencv.imgproc Imgproc distanceTransform
public static void distanceTransform(Mat src, Mat dst, int distanceType, int maskSize)
From source file:OCV_DistanceTransform.java
License:Open Source License
@Override public void run(ImageProcessor ip) { // srcdst/*from w w w . j a v a 2 s . co m*/ int imw = ip.getWidth(); int imh = ip.getHeight(); float[] srcdst_floats = (float[]) ip.getPixels(); // mat Mat src_mat_32f = new Mat(imh, imw, CvType.CV_32FC1); Mat src_mat_8u = new Mat(imh, imw, CvType.CV_8UC1); Mat dst_mat_32f = new Mat(imh, imw, CvType.CV_32FC1); // run src_mat_32f.put(0, 0, srcdst_floats); src_mat_32f.convertTo(src_mat_8u, CvType.CV_8UC1); Imgproc.distanceTransform(src_mat_8u, dst_mat_32f, INT_DISTANCETYPE[indDistType], INT_DISTANCETRANSFORMMASKS[indMskSize]); dst_mat_32f.get(0, 0, srcdst_floats); }
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 ww w . j a va2s .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:qupath.opencv.processing.OpenCVTools.java
License:Open Source License
public static void watershedDistanceTransformSplit(Mat matBinary, int maxFilterRadius) { Mat matWatershedSeedsBinary;/*from w ww .j a va 2 s . c o m*/ // Create a background mask Mat matBackground = new Mat(); Core.compare(matBinary, new Scalar(255), matBackground, Core.CMP_NE); // Separate by shape using the watershed transform Mat matDistanceTransform = new Mat(); Imgproc.distanceTransform(matBinary, matDistanceTransform, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_PRECISE); // Find local maxima matWatershedSeedsBinary = new Mat(); Imgproc.dilate(matDistanceTransform, matWatershedSeedsBinary, OpenCVTools.getCircularStructuringElement(maxFilterRadius)); Core.compare(matDistanceTransform, matWatershedSeedsBinary, matWatershedSeedsBinary, Core.CMP_EQ); matWatershedSeedsBinary.setTo(new Scalar(0), matBackground); // Dilate slightly to merge nearby maxima Imgproc.dilate(matWatershedSeedsBinary, matWatershedSeedsBinary, OpenCVTools.getCircularStructuringElement(2)); // Create labels for watershed Mat matLabels = new Mat(matDistanceTransform.size(), CvType.CV_32F, new Scalar(0)); labelImage(matWatershedSeedsBinary, matLabels, Imgproc.RETR_CCOMP); // Remove everything outside the thresholded region matLabels.setTo(new Scalar(0), matBackground); // Do watershed // 8-connectivity is essential for the watershed lines to be preserved - otherwise OpenCV's findContours could not be used ProcessingCV.doWatershed(matDistanceTransform, matLabels, 0.1, true); // Update the binary image to remove the watershed lines Core.multiply(matBinary, matLabels, matBinary, 1, matBinary.type()); }