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?
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With