@TOC

Contour extraction by convex hull detection

The principle of

After the contour analysis of the binary image, the convex hull of each contour can be constructed for each contour data obtained. After the construction, the point set contained in the convex hull will be returned. The convex hull corresponding to the contour can be drawn according to the set of convex hull points returned. OpenCV API function for contour extraction convex hull is as follows:  convexHull( InputArray points, OutputArray hull, bool clockwise = false, Bool returnPoints = true) The points argument is the output of the hull convex hull detection of the input contour point set. When returnPoints is true it returns the index of each vertex in the convex hull which is the set of points. When returnPoints is false it returns a vector of integers which is the index of each vertex in the convex hull The opencvAPI function is used to determine whether a contour is convex. The method is as follows: isContourConvex(InputArray Contour)Copy the code

code

def findholl(src):# Convex hull detection # Read the image and go to gray mode= cv.cvtcolor (SRC, cv.COLOR_BGR2GRAY235
    ret, thresh = cv.threshold(gray, 235.255Contours, hierarchy = cv.findContours(thresh,2.1Find the convex hull of the object and draw the outline of the convex hullforCNT in contours: hull = CV. ConvexHull (CNT) length = len(hull5
        if length > 5: # Draw the outline of the convex hull of the imagefor i in range(length):
                cv.line(src, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0), (0.0.255), 2)

    cv.imshow('finger', src)


Copy the code

Testing:

Contour detection draws a rectangular box

Access to outline

Cv2.findcontours ()

cv2.findContours(img, mode, method)
Copy the code

Draw the contour

Thickness [, lineType[, hierarchy[, size [, size [, size [, size [, size [, size [, size [, size [, size [, size [, size [, size]] MaxLevel [, offset]]]]]) The first parameter specifies which image to draw the outline on; The second argument is the outline itself, which in Python is a list. The third argument specifies which of the Outlines in the outline list to draw, or if -1, all of them. The following parameters are simple. Thickness indicates the width of the contour. If it is -1 (cv2.filled), it is the fill mode.Copy the code

code

def Conmtours(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 127.255, cv.THRESH_BINARY)

    contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

    draw_img0 = cv.drawContours(img.copy(), contours, 0, (0.255.255), 3)
    draw_img1 = cv.drawContours(img.copy(), contours, 1, (255.0.255), 3)
    draw_img2 = cv.drawContours(img.copy(), contours, 2, (255.255.0), 3)
    draw_img3 = cv.drawContours(img.copy(), contours, - 1, (0.0.255), 3)

    cv.imshow("img", img)
    cv.imshow("draw_img0", draw_img0)
    cv.imshow("draw_img1", draw_img1)
    cv.imshow("draw_img2", draw_img2)
    cv.imshow("draw_img3", draw_img3)
Copy the code

Draw the outline in the figure with circles and rectangles

def ConmtoursMAX(img): # Binarization, black and white binarization ret, Thresh= cv.threshold(cv.cvtcolor (img.copy(), cv.COLOR_BGR2GRAY),127.255, # is more than127Instead of255Otherwise to0Contours, hierarchy = cv.findContours(thresh, cv.retr_external, cv.CHAIN_APPROX_SIMPLE)for c in contours:
        x, y, w, h = cv.boundingRect(c)
        """Pass in an outline image, return x, y is the point in the top left corner, w and h are the width and height of the rectangular border."""
        cv.rectangle(img, (x, y), (x + w, y + h), (0.255.0), 2Rect = cv.minareaRect (c) # Calculate the coordinates of the minimum region box = cV.boxpoints (rect) # Normalize the coordinates to integers box = NP.int0 (box) # Draw the outline  cv.drawContours(img, [box],0, (0.0.255), 3(x, y) = cv.minEnclosingCircle(c) # convert to integer center = (x, y)int(x), int(y))
        radius = intImg = CV. Circle (img, center, radius, (0.255.0), 2CV. DrawContours (img, contours,- 1, (255.0.0), 1)
    cv.imshow("contours", img)    
Copy the code

(I’m tired of drawing the center of the circle.)