I'm trying to draw the contour of the biggest object.
First I will show an image drawing all contours:
To find the biggest object I used this code:
maxsize = 0
best = 0
count = 0
for cnt in contours:
if cv2.contourArea(cnt) > maxsize:
maxsize = cv2.contourArea(cnt)
best = count
count += 1
cv2.drawContours(img_rgb, contours[best], -1, (0,0,255), 2)
And the result is the next:
Why the contours are not connected?
Thanks in advance.
See that on your code you are telling by the
-1
parameter to the function draw all of your contours, when you actually wants to draw only thebest
one. So, instead of the-1
(all) you can simply ask for the function to draw the contour that you desire.You can fix this problem replacing the line:
with:
or you can still use the
-1
, but then you are going to need as parameter a set of points ([]
):You can have more information about this theme in the OpenCV Docs page