Ask Your Question

read_a_book's profile - activity

2020-09-05 07:25:07 -0500 marked best answer 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()
2020-06-18 04:34:38 -0500 commented answer Difference in drawn result between cv2.drawContours and drawing contours with a loop

Aaaaahhhh Thank you!!! Ive been copy pasting without checking my arguments. Glad I can blame python and not myself : )

2020-06-18 04:28:19 -0500 received badge  Student (source)
2020-06-18 04:11:26 -0500 asked a question Difference in drawn result between cv2.drawContours and drawing contours with a loop

Difference in drawn result between cv2.drawContours and drawing contours with a loop Hello I am hoping someone could he