Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have the following Python code:

ret,thresh = cv2.threshold(gray,5,255,cv2.THRESH_TOZERO)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11))
kernel2 = np.ones((3,3),np.uint8)
erosion = cv2.erode(thresh,kernel2,iterations = 1)
dilation = cv2.dilate(erosion,kernel1,iterations = 7)

I'm trying to convert it to Java. This is my current version:

double thresh = Imgproc.threshold(gray, gray, 5, 255, Imgproc.THRESH_TOZERO);
Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(11, 11));
Mat kernel2 = Mat.ones(3, 3, CvType.CV_8U);
Mat erosion = new Mat();
Imgproc.erode(gray, erosion, kernel2);
Mat dilation = new Mat();
Imgproc.dilate(erosion, dilation, kernel1);

Right now I'm unable to find a place where thresh parameter should be applied and also I'm not using the iterations parameter for Imgproc.erode and Imgproc.dilate methods because the method signature in this case also requires additional Point anchor parameter which I do not have right now.

How to properly convert this code to Java?

UPDATED

I do the following translation:

Mat erosion = new Mat();
Imgproc.erode(gray, erosion, kernel2, new Point(), 1);
Mat dilation = new Mat();
Imgproc.dilate(erosion, dilation, kernel1, new Point(), 7);

and looks like it is working as expected but I'm not sure about new Point() Is it a correct translation?

Here are the dilate declarations in C++/Python/Java:

C++: void dilate(InputArray src, OutputArray dst, InputArray kernel, 
        Point anchor=Point(-1,-1),
        int iterations=1,
        int borderType=BORDER_CONSTANT,
        const Scalar& borderValue=morphologyDefaultBorderValue() )
Python: cv2.dilate(src, kernel[, dst
            [, anchor[, iterations[, borderType[, borderValue]]]]]) → dst
Java: static void dilate(Mat src, Mat dst, Mat kernel, Point anchor, int iterations)

Because there is no default value for anchor if you want to use the param iterations. So pass new Point(-1,-1) is a better choice.

Imgproc.dilate(src, dst, kernel, new Point(-1,-1), iterations);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.