OpenCV将Python代码转换为Java

时间:2018-02-07 07:47:16

标签: java python opencv

我有以下Python代码:

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)

我试图将其转换为Java。这是我目前的版本:

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);

目前我无法找到应该应用thresh参数的地方,而且我没有使用iterations和{Imgproc.erode参数1}}方法,因为在这种情况下方法签名还需要额外的Imgproc.dilate参数,我现在还没有。

如何正确地将此代码转换为Java?

已更新

我做了以下翻译:

Point anchor

看起来它按预期工作但我不确定Mat erosion = new Mat(); Imgproc.erode(gray, erosion, kernel2, new Point(), 1); Mat dilation = new Mat(); Imgproc.dilate(erosion, dilation, kernel1, new Point(), 7); 这是否是正确的翻译?

1 个答案:

答案 0 :(得分:2)

以下是<!DOCTYPE html> <html> <head> <style> .main { background-color: black; } .top { margin: 0 50px; height: 100px; background-color: white; } .mid { height: 500px; background-color: green; } .left-col, .right-col { height: 100%; width: 100px; background-color: blue; } .left-col { float: left; } .right-col { float: right; } .bottom { margin: 0 75px; height: 80px; background-color: white; } </style> </head> <body> <div class="main"> <div class="top"></div> <div class="mid"> <div class="left-col"> </div> <div class="right-col"> </div> </div> <div class="bottom"> </div> </div> </body> </html> 中的dilate声明:

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) ,则anchor没有默认值。所以通过iterations是一个更好的选择。

new Point(-1,-1)