refCnts, hierarchy = cv2.findContours
时间: 2023-11-02 09:34:40 浏览: 120
cvFindContours
(image, mode, method, offset=None, hierarchy=None, thresh=None)
This function finds contours in a binary image. It takes the following parameters:
- **image**: The input binary image. This should be an 8-bit single-channel image, where the foreground pixels are white and the background pixels are black.
- **mode**: The contour retrieval mode. This can be one of the following:
- cv2.RETR_EXTERNAL: Only the external contours are returned.
- cv2.RETR_LIST: All contours are returned in a list.
- cv2.RETR_CCOMP: All contours are returned in a two-level hierarchy, where the top-level contours are the external boundaries and the second-level contours are the boundaries of the holes inside the objects.
- cv2.RETR_TREE: All contours are returned in a full hierarchy tree.
- **method**: The contour approximation method. This can be one of the following:
- cv2.CHAIN_APPROX_NONE: All the contour points are returned.
- cv2.CHAIN_APPROX_SIMPLE: Only the end points of the contour segments are returned.
- cv2.CHAIN_APPROX_TC89_L1 or cv2.CHAIN_APPROX_TC89_KCOS: The contour points are approximated using the Teh-Chin chain approximation algorithm.
- **offset**: An optional offset value to be added to all the contour points. This is useful when the input image is a subregion of a larger image.
- **hierarchy**: An optional output array that describes the hierarchy of the contours.
- **thresh**: An optional threshold value to be used when converting a grayscale image to a binary image.
The function returns two values:
- **refCnts**: A list of contours, where each contour is represented as a numpy array of shape (n, 1, 2), where n is the number of contour points and each contour point is a (x,y) coordinate.
- **hierarchy**: An optional output array that describes the hierarchy of the contours. This array has shape (1, n, 4), where n is the number of contours. Each contour is represented by a four-element tuple (next, prev, child, parent), where next and prev are the indices of the next and previous contours at the same hierarchical level, child is the index of the first child contour, and parent is the index of the parent contour. If a contour has no next, prev, child, or parent contour, the corresponding element is set to -1.
阅读全文