Contour differences between C ++ and Python

I am currently using opencv to detect simple points in shapes. At first I used C ++ and everything worked fine. Now I'm trying to do the same with Python, since I need to use it on the Internet, and edge detection does not seem to work either.

Here is my C ++ code:

_src = cv::imread(_imagePath); cv::Mat gray; cv::cvtColor(_src, gray, CV_BGR2GRAY); cv::Mat bw; cv::Canny(gray, bw, 0, 50, 5); cv::findContours(bw.clone(), allCountours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); 

As you can see, this is pretty simple, the same code is Python:

 self._src = cv2.imread(self._imagePath) gray = cv2.cvtColor(self._src, cv2.COLOR_BGR2GRAY) bw = cv2.Canny(gray, 0, 50, 5) allCountours, hierarchy = cv2.findContours(bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 

To show the results, I used drawcontours with random colors on different paths:

enter image description here

As you can see, in C ++ every outline of the figure is detected properly, although it is not perfect, whereas in Python I have much more outlines. Each time the line breaks a bit, a new contour is detected. Any idea how I could fix this? Thank you!

+6
source share
1 answer

The signature of a C ++ function is as follows: void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false )

And for Python this is: cv.Canny(image, edges, threshold1, threshold2, aperture_size=3) → None

As you can see, the last parameter is not available in Python. It may be set to true . Could you try it?

+2
source

Source: https://habr.com/ru/post/970635/


All Articles