Combine contours vertically and get convex hull - opencv - python

Ishara Madhawa picture Ishara Madhawa · May 20, 2018 · Viewed 8.5k times · Source

I have character images like this:

After getting contours and convexHull the output is like this:

For that I used following code:

import cv2
img = cv2.imread('input.png', -1)

ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                        127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    # get convex hull
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)    
cv2.imwrite("output.png", img)

As you can see in the following image there are identified contours which are vertically aligned with the original character. But those are separated with the original core character. (Those are actually modifiers of the language called sinhala - සිංහල)

Now I want to merge those vertically aligned contours with the core character. Ultimately the output should be as follows. How could I do that efficiently?

Answer

Jeru Luke picture Jeru Luke · May 20, 2018

You could try performing a morphological operation using a kernel with a vertical rectangular shape. In this way ceratin characters above the original characters would be joined as one.

rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 30))
threshed = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, rect_kernel)
cv2.imshow('threshed', threshed)

enter image description here

imgContours, Contours, Hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in Contours:
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img2, [hull], -1, (0, 0, 255), 1) 
cv2.imshow('convex hull', img2)

enter image description here