Sorting contours left to right in Python (OpenCV)

梦想与她 提交于 2019-12-03 03:58:56
David Clifte

Implements de sort function of list in python like here.

In the implemented function you calculate the center and verify if the X position is gretter or smaller them the other. If gretter return 1, smaller -1 and equals 0.

def greater(a, b):
    momA = cv2.moments(a)        
    (xa,ya) = int(momA['m10']/momA['m00']), int(momA['m01']/momA['m00'])

    momB = cv2.moments(b)        
    (xb,yb) = int(momB['m10']/momB['m00']), int(momB['m01']/momB['m00'])
    if xa>xb 
        return 1

    if xa == xb
        return 0
    else
        return -1

For sure you can do better if you calculate de centers only once.

then just do

contours.sort(greater)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!