How can I sort contours from left to right and top to bottom?

前端 未结 4 1390
[愿得一人]
[愿得一人] 2020-12-06 07:14

I am trying to build an character recognition program using Python. I am stuck on sorting the contours. I am using this page as a reference.

I managed to find the c

4条回答
  •  孤城傲影
    2020-12-06 08:10

    after finding the contours using contours=cv2.findContours(),use -

    boundary=[]
    for c,cnt in enumerate(contours):
        x,y,w,h = cv2.boundingRect(cnt)
        boundary.append((x,y,w,h))
    count=np.asarray(boundary)
    max_width = np.sum(count[::, (0, 2)], axis=1).max()
    max_height = np.max(count[::, 3])
    nearest = max_height * 1.4
    ind_list=np.lexsort((count[:,0],count[:,1]))
    
    c=count[ind_list]
    

    now c will be sorted in left to right and top to bottom.

提交回复
热议问题