Example usage for org.opencv.imgproc Imgproc watershed

List of usage examples for org.opencv.imgproc Imgproc watershed

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc watershed.

Prototype

public static void watershed(Mat image, Mat markers) 

Source Link

Usage

From source file:OCV_Watershed.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src (RGB)/*from  ww  w  . j a  v  a  2 s . c  o m*/
    int[] arr_src_rgb = (int[]) imp_src.getChannelProcessor().getPixels();
    int imw_src = imp_src.getWidth();
    int imh_src = imp_src.getHeight();
    Mat mat_src_rgb = new Mat(imh_src, imw_src, CvType.CV_8UC3);

    // map (32bit)
    float[] arr_map_32f = (float[]) imp_map.getChannelProcessor().getPixels();
    int imw_map = imp_map.getWidth();
    int imh_map = imp_map.getHeight();
    Mat mat_map_32f = new Mat(imh_map, imw_map, CvType.CV_32FC1);
    Mat mat_map_32s = new Mat(imh_map, imw_map, CvType.CV_32SC1);

    // run
    OCV__LoadLibrary.intarray2mat(arr_src_rgb, mat_src_rgb, imw_src, imh_src);
    mat_map_32f.put(0, 0, arr_map_32f);
    mat_map_32f.convertTo(mat_map_32s, CvType.CV_32SC1);

    Imgproc.watershed(mat_src_rgb, mat_map_32s);

    mat_map_32s.convertTo(mat_map_32f, CvType.CV_32FC1);
    mat_map_32f.get(0, 0, arr_map_32f);
}

From source file:com.astrocytes.core.operationsengine.OperationsImpl.java

License:Open Source License

private Mat applyRayCastingSegmentation() {
    //Mat cannyEdges = CoreOperations.cannyFilter(sourceImage, 26, 58);
    Mat contours = new Mat(preparedImage.rows(), preparedImage.cols(), CvType.CV_32S);
    int contoursCount = /*neurons.size();*/ CoreOperations
            .drawAllContours(CoreOperations.erode(preparedImage, 5), contours);
    Mat result = new Mat(preparedImage.rows(), preparedImage.cols(), preparedImage.type());//CoreOperations.or(CoreOperations.and(cannyEdges, CoreOperations.grayscale(preparedImage)), contours);
    //cannyEdges.release();

    //Mat markers = new Mat(contours.rows(), contours.cols(), CvType.CV_32S);
    //contours.copyTo(markers);
    contours.convertTo(contours, CvType.CV_32S);

    for (Neuron neuron : neurons) {
        int x = (int) neuron.getCenter().x;
        int y = (int) neuron.getCenter().y;
        int color = (int) preparedImage.get(y, x)[0];
        /*contours.put(y, x, color);
        contours.put(y - 2, x, color);/*from w  w w.j a  va 2  s. c o  m*/
        contours.put(y + 2, x, color);
        contours.put(y, x - 2, color);
        contours.put(y, x + 2, color);*/
        Imgproc.circle(contours, neuron.getCenter(), (int) (0.4f * neuron.getRadius()), new Scalar(color), -1);
    }

    Imgproc.watershed(sourceImage, contours);

    for (int i = 0; i < contours.rows(); i++) {
        for (int j = 0; j < contours.cols(); j++) {
            int index = (int) contours.get(i, j)[0];
            if (index == -1) {
                result.put(i, j, 0, 0, 0);
            } else if (index <= 0 || index > contoursCount) {
                result.put(i, j, 0, 0, 0);
            } else {
                if (index == 255) {
                    result.put(i, j, 0, 0, 0/*sourceImage.get(i, j)*/);
                } else {
                    result.put(i, j, index, index, index);
                }
            }
        }
    }

    result = CoreOperations.erode(result, 2);
    result = CoreOperations.dilate(result, 3);

    contours.release();

    contours = sourceImage.clone();
    CoreOperations.drawAllContours(result, contours);

    return contours;
}

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);//w w w. jav  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;
}