drawContours as is, not filled, not just outline

asked 2017-12-27 04:23:57 -0600

Akash Budhia gravatar image

updated 2020-10-06 04:57:14 -0600

Hi,

With cv2.drawContours is there a way to draw the exact contour as is? Not just the border, or not completely filled.

I'm using it for extracting alphabets from a text, and for characters like A, P, 4 etc, it fills them inside as well.

Eg: Input Binary Image:

image description

Extracted problematic characters after drawContours:

image description image description image description

Adding Code:

for contour in countours:
    mask = np.zeros(img.shape[:2], dtype="uint8")
    mask.fill(255)
    cv2.drawContours(mask, [contour], 0, 0, cv2.FILLED)
    x,y,w,h = cv2.boundingRect(contour)
    charImg = mask[y:y+h, x:x+w]
    cv2.imwrite('char', charImg)
edit retag flag offensive close merge delete

Comments

how do you call it ?

berak gravatar imageberak ( 2017-12-27 04:52:31 -0600 )edit

added code

Akash Budhia gravatar imageAkash Budhia ( 2017-12-27 04:58:53 -0600 )edit

cv2.drawContours(mask, [contour], 0, 0, 2)

supra56 gravatar imagesupra56 ( 2017-12-27 06:30:07 -0600 )edit

That will draw the boundary with thickness and not the actual matching contour region.

Akash Budhia gravatar imageAkash Budhia ( 2017-12-27 06:36:42 -0600 )edit

It's important to note that a contour is just the boundary. So the inner part of those letters would be a different contour.

If you use connectedComponents you get the filled area, which may be more what you want. connectedComponentsWithStats will even return bounding boxes. It is important to note that it will return ALL the WHITE portions of the image and a separate component.

Tetragramm gravatar imageTetragramm ( 2017-12-27 11:34:46 -0600 )edit
2

@Akash Budhia Pay attention to Contours Hierarchy value. It will help you in your case. More detail : hierarchy

hoang anh tuan gravatar imagehoang anh tuan ( 2017-12-28 02:42:13 -0600 )edit