Opencv-python locate and image contour drawing, cv2.findContours () function, seeking to outline circumscribed rectangle, cv2.boundingrect ()

First, find an image profile
  • Find opencv-python API for image contour: findContours function
    This function takes as parameters binary map, according to the parameter, the outer contour of the object to find, and external profile, save profile point, compression etc ...

  • 如:contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

def findContours(image, mode, method, contours=None, hierarchy=None, offset=None): # real signature unknown; restored from __doc__
    """
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
    .   @brief Finds contours in a binary image.
    .   
    .   The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours
    .   are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the
    .   OpenCV sample directory.
    .   @note Since opencv 3.2 source image is not modified by this function.
    .   
    .   @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero
    .   pixels remain 0's, so the image is treated as binary . You can use #compare, #inRange, #threshold ,
    .   #adaptiveThreshold, #Canny, and others to create a binary image out of a grayscale or color one.
    .   If mode equals to #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
    .   @param contours Detected contours. Each contour is stored as a vector of points (e.g.
    .   std::vector<std::vector<cv::Point> >).
    .   @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has
    .   as many elements as the number of contours. For each i-th contour contours[i], the elements
    .   hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices
    .   in contours of the next and previous contours at the same hierarchical level, the first child
    .   contour and the parent contour, respectively. If for the contour i there are no next, previous,
    .   parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
    .   @param mode Contour retrieval mode, see #RetrievalModes
    .   @param method Contour approximation method, see #ContourApproximationModes
    .   @param offset Optional offset by which every contour point is shifted. This is useful if the
    .   contours are extracted from the image ROI and then they should be analyzed in the whole image
    .   context.
    """
    pass
parameter effect
cv2.RETR_EXTERNAL Find only the outer contour
cv2.RETR_LIST All contour detection, save it to a arry (list)
cv2.RETR_CCOMP Establish two levels contour (outer / inner), only two layers of tissue
cv2.RETR_TREE All contour detection, reconstruction of all levels of nested profile
cv2.CHAIN_APPROX_NONE Store all border points
cv2.CHAIN_APPROX_SIMPLE Compressed vertically, horizontally, diagonally, leaving only the endpoint
cv2.CHAIN_APPROX_TX89_L1 Approximate algorithm using teh-Chini
cv2.CHAIN_APPROX_TC89_KCOS Approximate algorithm using teh-Chini

cv2.drawContours (image, contours, contourIdx, color [, thickness [, lineType [, hierarchy [, maxLevel [, offset]]]]])
The first argument is specified on the drawn contour which image;
second parameter It contour itself, in Python is a list.
The third parameter specifies which of contouring strip profile list, if it is -1, which draw all contour. The latter parameter is simple. Wherein the thickness indicates that the width of the contour line, if it is -1 (cv2.FILLED), was fill mode

Reference article

Second, the bounding rectangle

Find profile

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

contours multidimensional array comprising a plurality of contours cnt

x, y, w, h = cv2.boudingrect (cnt) # circumscribed rectangle obtained

Parameters: x, y, w, h respectively represent an x-axis coordinate of the circumscribed rectangle and the y-axis, and the width and height of the rectangle, cnt represents the profile value input

Guess you like

Origin www.cnblogs.com/shiqi17/p/12169710.html