How do I merge the bounding rectangles generated for i,j and '?'?

asked 2016-08-05 04:58:34 -0600

AkashShrestha gravatar image

I want to create an OCR software using python and openCV. I managed to find the contours and generated the bounding rectangles. However the code seems to detect two separate bounding rectangles for 'i', 'j' and '?'. One for the dot and one for the rest of the body. I want to merge those two bounding rectangles. How do I go about doing that?

Here is an example for i.

Here's the code snippet I used to find the contours and add the rectangles.

mo_image = bw_image.copy()
contour0 = cv2.findContours(mo_image.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours = [cv2.approxPolyDP(cnt,3,True) for cnt in contour0[0]]

# === Extract Bounding Rectangles

maxArea = 0
for ctr in contours:
    maxArea = max(maxArea,cv2.contourArea(ctr))

if img == "Food.jpg":
    areaRatio = 0.05
elif img == "Plate.jpg":
    areaRatio = 0.5
elif img == "Test.jpg":
    areaRatio = 0.01

for ctr in contours:    
    if cv2.contourArea(ctr) > maxArea * areaRatio: 

I understand that I need to properly sort these rectangles first from left to right and top to bottom and then do the merge. But I cannot seem to be able to figure out a way to do either. Any help would be greatly appreciated.

edit retag flag offensive close merge delete



for the sorting, see here

berak gravatar imageberak ( 2016-08-05 05:04:47 -0600 )edit