Python cv2.convexityDefects() Examples

The following are 13 code examples of cv2.convexityDefects(). 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: chapter2.py    From OpenCV-Computer-Vision-Projects-with-Python with MIT License 19 votes vote down vote up
def FindHullDefects(self, segment):
        _,contours,hierarchy = cv2.findContours(segment, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        # find largest area contour
        max_area = -1
        for i in range(len(contours)):
            area = cv2.contourArea(contours[i])
            if area>max_area:
                cnt = contours[i]
                max_area = area

        cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
        hull = cv2.convexHull(cnt, returnPoints=False)
        defects = cv2.convexityDefects(cnt, hull)

        return [cnt,defects] 
Example #2
Source File: new.py    From Fingers-Detection-using-OpenCV-and-Python with MIT License 7 votes vote down vote up
def calculateFingers(res,drawing):  # -> finished bool, cnt: finger count
    #  convexity defect
    hull = cv2.convexHull(res, returnPoints=False)
    if len(hull) > 3:
        defects = cv2.convexityDefects(res, hull)
        if type(defects) != type(None):  # avoid crashing.   (BUG not found)

            cnt = 0
            for i in range(defects.shape[0]):  # calculate the angle
                s, e, f, d = defects[i][0]
                start = tuple(res[s][0])
                end = tuple(res[e][0])
                far = tuple(res[f][0])
                a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
                b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
                c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
                angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))  # cosine theorem
                if angle <= math.pi / 2:  # angle less than 90 degree, treat as fingers
                    cnt += 1
                    cv2.circle(drawing, far, 8, [211, 84, 0], -1)
            return True, cnt
    return False, 0


# Camera 
Example #3
Source File: FingerDetection.py    From Finger-Detection-and-Tracking with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def manage_image_opr(frame, hand_hist):
    hist_mask_image = hist_masking(frame, hand_hist)

    hist_mask_image = cv2.erode(hist_mask_image, None, iterations=2)
    hist_mask_image = cv2.dilate(hist_mask_image, None, iterations=2)

    contour_list = contours(hist_mask_image)
    max_cont = max(contour_list, key=cv2.contourArea)

    cnt_centroid = centroid(max_cont)
    cv2.circle(frame, cnt_centroid, 5, [255, 0, 255], -1)

    if max_cont is not None:
        hull = cv2.convexHull(max_cont, returnPoints=False)
        defects = cv2.convexityDefects(max_cont, hull)
        far_point = farthest_point(defects, max_cont, cnt_centroid)
        print("Centroid : " + str(cnt_centroid) + ", farthest Point : " + str(far_point))
        cv2.circle(frame, far_point, 5, [0, 0, 255], -1)
        if len(traverse_point) < 20:
            traverse_point.append(far_point)
        else:
            traverse_point.pop(0)
            traverse_point.append(far_point)

        draw_circles(frame, traverse_point) 
Example #4
Source File: find_arrows.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints = False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2: 
            return False
      
        farpoints = [d[0][2] for d in defects]    
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True    

        return False 
Example #5
Source File: whale176-rectangle.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints=False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2:
            return False

        farpoints = [d[0][2] for d in defects]
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True

        return False 
Example #6
Source File: whale176-5angle.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints=False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2:
            return False

        farpoints = [d[0][2] for d in defects]
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True

        return False 
Example #7
Source File: whale176-circle.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints=False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2:
            return False

        farpoints = [d[0][2] for d in defects]
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True

        return False 
Example #8
Source File: whale176-triangle.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints=False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2:
            return False

        farpoints = [d[0][2] for d in defects]
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True

        return False 
Example #9
Source File: _whale176-square.py    From PyCV-time with MIT License 6 votes vote down vote up
def isArrow(heptagon):
    hull = cv2.convexHull(heptagon, returnPoints=False)

    if len(hull) > 2:
        defects = cv2.convexityDefects(heptagon, hull)
        if defects is None or len(defects) != 2:
            return False

        farpoints = [d[0][2] for d in defects]
        if not np.abs(farpoints[0] - farpoints[1]) in [3, 4]:
            return False

        for defect in defects:
            s, e, f, d = defect[0]
            #    print defects
            #    s, e, f, d = defect[0]
            ps = heptagon[s, 0]
            pe = heptagon[e, 0]
            pd = heptagon[f, 0]
            if angle(ps, pd, pe) < 120:
                return True

        return False 
Example #10
Source File: find_arrows.py    From PyCV-time with MIT License 5 votes vote down vote up
def tip(arrow):
    hull = cv2.convexHull(arrow, returnPoints = False)
    defects = cv2.convexityDefects(arrow, hull)
    farpoints = [d[0][2] for d in defects]
    if np.abs(farpoints[0] - farpoints[1]) == 4:
        return arrow[sum(farpoints) / 2, 0]
    else:
        return arrow[0, 0] 
Example #11
Source File: whale176-rectangle.py    From PyCV-time with MIT License 5 votes vote down vote up
def tip(arrow):
    hull = cv2.convexHull(arrow, returnPoints=False)
    defects = cv2.convexityDefects(arrow, hull)
    farpoints = [d[0][2] for d in defects]
    if np.abs(farpoints[0] - farpoints[1]) == 4:
        return arrow[sum(farpoints) / 2, 0]
    else:
        return arrow[0, 0] 
Example #12
Source File: whale176-circle.py    From PyCV-time with MIT License 5 votes vote down vote up
def tip(arrow):
    hull = cv2.convexHull(arrow, returnPoints=False)
    defects = cv2.convexityDefects(arrow, hull)
    farpoints = [d[0][2] for d in defects]
    if np.abs(farpoints[0] - farpoints[1]) == 4:
        return arrow[sum(farpoints) / 2, 0]
    else:
        return arrow[0, 0] 
Example #13
Source File: whale176-triangle.py    From PyCV-time with MIT License 5 votes vote down vote up
def tip(arrow):
    hull = cv2.convexHull(arrow, returnPoints=False)
    defects = cv2.convexityDefects(arrow, hull)
    farpoints = [d[0][2] for d in defects]
    if np.abs(farpoints[0] - farpoints[1]) == 4:
        return arrow[sum(farpoints) / 2, 0]
    else:
        return arrow[0, 0]