Python cv2.matchShapes() Examples

The following are 2 code examples of cv2.matchShapes(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: RegionOfInterest.py    From DoNotSnap with GNU General Public License v3.0 7 votes vote down vote up
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 
Example #2
Source File: mask_analysis.py    From deepgaze with MIT License 5 votes vote down vote up
def matchMaxAreaWithShape(self, mask, shape):
        """it returns a value which identify the similarity between
            the largest area contour and a shape.
 
        The lower the result, the better match it is. It is calculated 
        based on the hu-moment values. For example if we have three shapes:
        A=star, B=rotated dilatated star, C=square
        Matching Image A with itself = 0.0
        Matching Image A with Image B = 0.001946
        Matching Image A with Image C = 0.326911
        @param mask the binary image to use in the function
        @param shape the contour to compare
        """        
        cnt = self.returnMaxAreaContour(mask)
        return cv2.matchShapes(cnt, shape, 1, 0.0)