Пример #1
0
  /**
   * @param inputImg
   * @return Mat
   */
  public static Mat watershed(Mat inputImg) {

    Mat target = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_8UC3);
    Imgproc.cvtColor(inputImg, target, Imgproc.COLOR_BGR2RGB);

    // Conversion to 8UC1 grayscale image
    Mat grayScale = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_32SC1);
    Imgproc.cvtColor(inputImg, grayScale, Imgproc.COLOR_BGR2GRAY);

    // constructing a 3x3 kernel for morphological opening
    Mat openingKernel = Mat.ones(9, 9, CvType.CV_8U);

    // яскравість
    // target.convertTo(target, -1, 10d * 12 / 100, 0);
    // Imgproc.dilate(target, target, new Mat(), new Point(-1, -1), 1);

    Size s = new Size(27, 27);
    Imgproc.GaussianBlur(target, target, s, 1.7);

    Imgproc.morphologyEx(target, target, Imgproc.MORPH_OPEN, openingKernel);

    // dilation operation for extracting the background
    // Imgproc.dilate(target, target, openingKernel);
    // Imgproc.erode(target, target, new Mat(), new Point(-1, -1), 1);

    Mat seeds = new Mat(target.rows(), target.cols(), CvType.CV_32SC1);

    for (int i = 0; i < 10; i++) {
      seeds.put(((int) Math.random()) % target.rows(), ((int) Math.random()) % target.cols(), i);
    }

    Imgproc.watershed(target, seeds);
    // Imgproc.threshold(target,target, 50, 155, Imgproc.THRESH_BINARY );
    return target;
  }