How to read the result of cv2.findContours?

Question:

I have a binary 2D-matrix b with shape (340,490) and apply cv2.findContours(b,1,2) on it. The result is a 3D array with shape (6, 2, 1) like [[90,3],[5,60],[90, 110],[5,135],[3,200],[3,3]]. I can’t read it. I applied cv2.drawContours(b,contours, -1, (0,0,255), 3) It worked prefected. Since I have bunch of noisy on the original b. I don’t know why it could be fixed only by 12 number. what does these number stand for?

Asked By: Tommy Yu

||

Answers:

cv2.drawContours() function returns 2 values:

The first one in "Contours" and the second one is "Hierarchy".

Contours contain the coordinates of boundary points of each contour detected in the image.

To find number of contours, use:

len(Contours)

This will give the number of contours found in the image.

Now each element in the "Contours" will be of the following type:

Contours[i] = [[[x1, y1]],
               [[x2, y2]],
               [[x3, y3]],
               [[x4, y4]],
               [[x5, y5]],
               ...
               [[xn, yn]]]

where x and y are the coordinates of the boundary point of that contour in the image and n is the total number of boundary points of that contour.

To know about return value "Hierarchy", refer to this link.

Answered By: Rahul Kedia
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.