3

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?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42

2 Answers2

6

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
2

You can try merging contours based on distance between the contours' centers. Since you want to put more emphasis on the vertical connections, you can modify your distance function so that it puts more importance on vertical connections. This can be done by treating horizontal distances as more "expensive" in terms of threshold. For example, to put emphasis on vertical proximity more than horizontal proximity, you can scale the horizonal difference with a larger than 1 constant:

distance = hypotenuse(distance_x * 5, distance_y)
sshashank124
  • 31,495
  • 9
  • 67
  • 76