Ask Your Question

Nbel's profile - activity

2017-11-12 06:27:33 -0600 received badge  Student (source)
2014-06-04 05:31:35 -0600 asked a question Contours differences between C++ and Python

Hi everyone, I'm currently using opencv to detect simple countours on shapes. At first, I used c++ and everything worked well. Now, I'm trying to do the same with Python as I need to use it online, and the contours detection doesn't seem to be working as well.

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, it's quite simple, the same code is Python is :

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 the different contours :

image description

As you can see, in c++ each shape contour is detected properly, evn though it's not perfect, whereas in Python I have much more contours. Every time a line breaks a little, a new contour is detected. Any idea how I could fix this ? Thanks you !