I have an image and I am using OpenCV to find the contours within that image. For example:
contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
Each contour that get returned is sometimes very irregularly shaped. For example, the return of one of them is:
contour = [[[34 4]]
[[35 3]]
[[36 4]]
[[36 11]]
[[35 12]]
[[34 11]]
[[34 6]]
[[33 6]]
[[32 5]]
[[33 4]]]
I would like to set the interior of this contour to a specific color. I’ve tried using cv2.BoundingRect to get the bounding rectangle of this contour, and then using that to set the color, however, I would like a more precise way to do this using the actual contour.
Is there a straightforward way to do this through a function that OpenCV provides, or through NumPy?
Use
cv2.drawContours()function withlineType = -1flag (Docs)eg : cv2.drawContours(img,[cnt],0,color,-1) # for filling inside a specific contour
or
cv2.drawContours(img,contours,-1,color,-1) # to fill inside all the contours in a single step
For more details, visit : Contours -1 : Getting Started