Python cv2 模块,matchShapes() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用cv2.matchShapes()

项目:DoNotSnap    作者:AVGInnovationLabs    | 项目源码 | 文件源码
def findEllipses(edges):
    contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    ellipseMask = np.zeros(edges.shape, dtype=np.uint8)
    contourMask = np.zeros(edges.shape, dtype=np.uint8)

    pi_4 = np.pi * 4

    for i, contour in enumerate(contours):
        if len(contour) < 5:
            continue

        area = cv2.contourArea(contour)
        if area <= 100:  # skip ellipses smaller then 10x10
            continue

        arclen = cv2.arcLength(contour, True)
        circularity = (pi_4 * area) / (arclen * arclen)
        ellipse = cv2.fitEllipse(contour)
        poly = cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5)

        # if contour is circular enough
        if circularity > 0.6:
            cv2.fillPoly(ellipseMask, [poly], 255)
            continue

        # if contour has enough similarity to an ellipse
        similarity = cv2.matchShapes(poly.reshape((poly.shape[0], 1, poly.shape[1])), contour, cv2.cv.CV_CONTOURS_MATCH_I2, 0)
        if similarity <= 0.2:
            cv2.fillPoly(contourMask, [poly], 255)

    return ellipseMask, contourMask
项目:Stronghold-2016-Vision    作者:team4099    | 项目源码 | 文件源码
def average_goal_matching(contour):
    global DEFINITE_GOALS
    total_score = 0
    min_score = 9999999999999999
    # number_of_things = 0
    if len(contour) < 8:
        return 9999999999999999
    for definite_goal in DEFINITE_GOALS:
        # number_of_things += 1
        this_score = cv2.matchShapes(contour, definite_goal, 1, 0.0)
        # print("Score:", this_score)
        total_score += this_score
        if this_score < min_score:
            min_score = this_score
    # print(number_of_things)
    # print("Smallest score:", min_score)
    # return total_score / DEFINITE_GOALS.size
    return min_score
项目:CE264-Computer_Vision    作者:RobinCPC    | 项目源码 | 文件源码
def single_finger_check(self, cnt):
        # use single finger image to check current fame has single finger
        grey_fin1 = cv2.cvtColor(self.fin1, cv2.COLOR_BGR2GRAY)
        _, thresh_fin1 = cv2.threshold(grey_fin1, 127, 255, 0)
        contour_fin1, hierarchy = cv2.findContours(thresh_fin1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        cnt1 = contour_fin1[0]
        ret1 = cv2.matchShapes(cnt, cnt1, 1, 0)

        grey_fin2 = cv2.cvtColor(self.fin2, cv2.COLOR_BGR2GRAY)
        _, thresh_fin2 = cv2.threshold(grey_fin2, 127, 255, 0)
        contour_fin2, hierarchy = cv2.findContours(thresh_fin2.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        cnt2 = contour_fin2[0]
        ret2 = cv2.matchShapes(cnt, cnt2, 1, 0)

        grey_fin3 = cv2.cvtColor(self.fin3, cv2.COLOR_BGR2GRAY)
        _, thresh_fin3 = cv2.threshold(grey_fin3, 127, 255, 0)
        contour_fin3, hierarchy = cv2.findContours(thresh_fin3.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        cnt3 = contour_fin3[0]
        ret3 = cv2.matchShapes(cnt, cnt3, 1, 0)
        reta = (ret1 + ret2 + ret3)/3
        if reta <= 0.3:
            return 5        # set as one-finger module
        else:
            return 0        # not detect, still 0


# Use PyAutoGUI to control mouse event
项目:ghetto_omr    作者:pohzhiee    | 项目源码 | 文件源码
def contouring(img,match_coeff):
    #Defining coefficients
    #----------------------------------
    #Max value of contour shape matching coefficient
    match_coeff = 0.1
    #max contour area
    max_cont_area = 100
    #----------------------------------
    #find countours
    im2, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    #truncate contours less than predefined maximum area
    c_counter = 0
    for c in contours:

        A = cv2.contourArea(c)

        if A<max_cont_area:
            contours=np.delete(contours,c_counter,0)
            c_counter=c_counter-1
        c_counter=c_counter+1

    #length of truncated contour array
    clen=c_counter

    #create match_array [dimension = len x len] with 0s
    match_array=np.zeros((clen,clen),np.uint8)

    #loop over the contours and compare two by two
    icounter = 0
    for i in contours:
        jcounter = 0

        for j in contours:
        #If difference has index <0.01 then regard as TRUE
            ret=cv2.matchShapes(i,j,1,0.0)
            if ret<match_coeff:
                match_array[icounter,jcounter]=1
            else:
                match_array[icounter,jcounter]=0
            jcounter=jcounter+1
        icounter=icounter+1


    #sum each row of the array (for TRUEs and FALSEs]
    sum_array=np.sum(match_array,axis=1,dtype=np.uint16)
    #finding mean of the comparison value
    sum_all=np.sum(sum_array,axis=0,dtype=np.uint16)
    ave_sim_val=sum_all/clen
    #Assumption: there is a lot of 1s
    return contours,sum_array,ave_sim_val