return 不同于 Java 和 Python cv2.findContours
Different return from Java and Python cv2.findContours
我正在将使用 OpenCV 的 Python 程序翻译成具有 Java 绑定的 C#。我使用相同的图像测试了 Python 和 C# 程序,我发现 findContours
方法 return 两个程序之间的轮廓不同。
Python: _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
C#: Imgproc.FindContours(edges, contours, hierarchy, Imgproc.RetrTree, Imgproc.ChainApproxSimple);
对于 Python,我检查使用 len(contours)
,对于 C# contours.Count
,它们分别 return 值 94 和 106。我认为这可能是我翻译的程序中出现许多差异的原因,我不确定为什么。我在这里做错了什么?
Add-on: The Canny
method below is called before calling findContours
. Anything before is just reading the image, and converting the image into a gray one, thus the grayImg
variable.
C#: Imgproc.Canny(grayImg, edges, 100, 200, 3, false);
Python: edges = cv2.Canny(gray, 100, 200, apertureSize = 3)
我以前认为这是因为 OpenCV 版本不同,但我意识到两者都使用 OpenCV 3。除非 3.1 中的 FindContours
方法与 3.4 中的方法不同,否则我又回到了原点,因为我不知道问题的原因。
我终于找到了这个现象的答案。 python 程序使用的是 OpenCV 3.4,而我的 C# 程序的 Java 绑定使用的是较旧的 OpenCV 3.1。 FindContours
方法可以在两个版本中找到,但显然 returns 不同的值。
我正在将使用 OpenCV 的 Python 程序翻译成具有 Java 绑定的 C#。我使用相同的图像测试了 Python 和 C# 程序,我发现 findContours
方法 return 两个程序之间的轮廓不同。
Python: _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
C#: Imgproc.FindContours(edges, contours, hierarchy, Imgproc.RetrTree, Imgproc.ChainApproxSimple);
对于 Python,我检查使用 len(contours)
,对于 C# contours.Count
,它们分别 return 值 94 和 106。我认为这可能是我翻译的程序中出现许多差异的原因,我不确定为什么。我在这里做错了什么?
Add-on: The
Canny
method below is called before callingfindContours
. Anything before is just reading the image, and converting the image into a gray one, thus thegrayImg
variable.
C#: Imgproc.Canny(grayImg, edges, 100, 200, 3, false);
Python: edges = cv2.Canny(gray, 100, 200, apertureSize = 3)
我以前认为这是因为 OpenCV 版本不同,但我意识到两者都使用 OpenCV 3。除非 3.1 中的 FindContours
方法与 3.4 中的方法不同,否则我又回到了原点,因为我不知道问题的原因。
我终于找到了这个现象的答案。 python 程序使用的是 OpenCV 3.4,而我的 C# 程序的 Java 绑定使用的是较旧的 OpenCV 3.1。 FindContours
方法可以在两个版本中找到,但显然 returns 不同的值。