Home > Software design >  How to draw contour without filling in OpenCV
How to draw contour without filling in OpenCV

Time:12-27

Trying to extract numbers from an image, however I'm having issues with 0, 8 and such because of the enclosed portions.

When I use cv.drawContours(black_img, [contour_num], -1, (255, 255, 255), thickness=cv.FILLED)

it obviously fills the inside of the contour as well. How do I prevent this? How do I draw only the "outer" part of the number? Thanks

drawn contour orig image

CodePudding user response:

To draw a contour without filling it in OpenCV, you can use the cv2.drawContours() function and pass in the contour and the index of the contour to draw, as well as the desired color and thickness of the contour. You can also set the filled parameter to False to disable filling.

Here's an example of how you can use cv2.drawContours() to draw a single contour without filling:

cv2.drawContours(image, contours, 0, (0, 255, 0), 2, cv2.LINE_AA, hierarchy=None, maxLevel=1, offset=(0, 0), filled=False)

CodePudding user response:

cv.drawContours(black_img, [contour_num], -1, (255, 255, 255), thickness=cv.FILLED)

cv.FILLED is equal to -1 and is the reason of drawing contours filled.

you should specify a value is greater than zero like thickness=1 , thickness=2 etc.

  • Related