Ask Your Question

AkashShrestha's profile - activity

2019-11-23 11:20:24 -0600 received badge  Famous Question (source)
2018-07-26 02:36:52 -0600 received badge  Notable Question (source)
2017-11-08 21:23:23 -0600 received badge  Popular Question (source)
2017-08-09 07:21:09 -0600 received badge  Student (source)
2016-08-06 09:36:15 -0600 received badge  Editor (source)
2016-08-05 05:35:16 -0600 commented answer How to sort contours left to right, while going top to bottom

Could you please provide a code snippet for what you've just described? Also will that work for sorting alphabet contours as well? They have different y values for taller and smaller characters.

2016-08-05 04:58:34 -0600 asked a question How do I merge the bounding rectangles generated for i,j and '?'?

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. http://i.imgur.com/mtu7SSr.png

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
rect=[]
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: 
        rect.append(cv2.boundingRect(cv2.approxPolyDP(ctr,1,True)))

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.

2016-08-05 04:46:30 -0600 received badge  Supporter (source)
2016-07-29 09:21:35 -0600 asked a question How to sort contours left to right, while going top to bottom

I'm finding the contours for an image with digits and characters, for OCR. So, I need the contours to be sorted left to right, while going line to line, i.e. top to bottom. Right now, the contours aren't sorted that way.

image description

For example, the contours for the above image is sorted randomly.

What I need is the sorting as D,o,y,o,u,k,n,o,w,s,o,m,e,o,n,e,r,.(dot),i(without dot),c,h...and so on. I've tried couple of methods where we first observe the y-coordinate and then use some keys and the x-coordinate. Like right now, I have the following sorting code. It works for the first 2 lines. Then in the 3rd line, the sorting somehow doesn't happen. The main problem seem to be in the letters such as i, j, ?, (dot), (comma), etc where the y axis of the (dot) varies, despite belonging to the same line. So what might be a good solution for this?

for ctr in contours:    
    if cv2.contourArea(ctr) > maxArea * areaRatio: 
        rect.append(cv2.boundingRect(cv2.approxPolyDP(ctr,1,True)))

for i in rect:
    x = i[0]
    y = i[1]
    w = i[2]
    h = i[3]

    if(h>max_line_height):
        max_line_height = h

mlh = max_line_height*2
max_line_width = raw_image.shape[1] #width of the input image
mlw = max_line_width
rect = np.asarray(rect)
s = rect.astype( np.uint32 ) #prevent overflows
order= mlw*(s[:,1]/mlh)+s[:,0]
sort_order= np.argsort( order )
rect = rect[ sort_order ]