Ask Your Question

Revision history [back]

Difference in drawn result between cv2.drawContours and drawing contours with a loop

Hello

I am hoping someone could help me understand this strange result I stumbled upon today while drawing contours.

When I draw all the contours at once with cv2.drawContours(image, contours, -1, (255, 50, 255),1) I get a nice result:

image description

However, when I draw each contour individually by looping through the contours with a for loop:

for cnt in contours: cv2.drawContours(blank2, cnt, -1, (255, 50, 255), 1)

I get a very low quality result where the car shape is not even fully outlined anymore

image description

I have tried displaying the images with cv2.imShow, zooming in, increasing line thickness and I keep getting this weird result. Is there a difference I do not know about between drawing the contours in these two different ways?

I have copy pasted my code below. Thank you for your help!

image = cv2.imread(imagePath)
imageB,_,_ = cv2.split(image)

_, imageThresh = cv2.threshold(imageB,118,255,cv2.THRESH_BINARY_INV)

contours, hierarchy = cv2.findContours(imageThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

blank = np.ones_like(image)
cv2.drawContours(blank, contours, -1, (255, 50, 255), 1)

blank2 = np.ones_like(image)
for index,cnt in enumerate(contours):
    cv2.drawContours(blank2, cnt, -1, (255, 50, 255), 1)

plt.figure(figsize=(10,10))
plt.subplot(121)
plt.imshow(blank)
plt.title("Result")
plt.subplot(122)
plt.imshow(blank2)
plt.title("Result 2")
plt.show()

Difference in drawn result between cv2.drawContours and drawing contours with a loop

Hello

I am hoping someone could help me understand this strange result I stumbled upon today while drawing contours.

When I draw all the contours at once with cv2.drawContours(image, contours, -1, (255, 50, 255),1) I get a nice result:

image description

However, when I draw each contour individually by looping through the contours with a for loop:

for cnt in contours: cv2.drawContours(blank2, cnt, -1, (255, 50, 255), 1)

I get a very low quality result where the car shape is not even fully outlined anymore

image description

I have tried displaying the images with cv2.imShow, zooming in, increasing line thickness and I keep getting this weird result. Is there a difference I do not know about between drawing the contours in these two different ways?

I have copy pasted my code below. Thank you for your help!

image = cv2.imread(imagePath)
imageB,_,_ = cv2.split(image)

_, imageThresh = cv2.threshold(imageB,118,255,cv2.THRESH_BINARY_INV)

contours, hierarchy = cv2.findContours(imageThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

blank = np.ones_like(image)
cv2.drawContours(blank, contours, -1, (255, 50, 255), 1)

blank2 = np.ones_like(image)
for index,cnt in enumerate(contours):
    cv2.drawContours(blank2, cnt, -1, (255, 50, 255), 1)

plt.figure(figsize=(10,10))
plt.subplot(121)
plt.imshow(blank)
plt.title("Result")
plt.subplot(122)
plt.imshow(blank2)
plt.title("Result 2")
plt.show()