cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
时间: 2024-03-07 18:45:37 浏览: 116
cv2.findContours是OpenCV中的一个函数,用于在二值图像中查找轮廓。它的参数包括输入图像、轮廓检索模式和轮廓近似方法。
具体来说,cv2.findContours函数的参数如下:
- thresh:输入的二值图像,通常是通过阈值处理后得到的图像。
- cv2.RETR_LIST:轮廓检索模式,表示检测所有的轮廓,并将它们存储在列表中。
- cv2.CHAIN_APPROX_SIMPLE:轮廓近似方法,表示使用简单的轮廓近似方法来压缩轮廓信息。
该函数的返回值包括两个:轮廓列表和层次结构。轮廓列表是一个包含所有检测到的轮廓的数组,每个轮廓都是一个点集。层次结构是一个可选的输出参数,用于表示轮廓之间的关系。
相关问题
refCnts, hierarchy = cv2.findContours
(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.
cv2.findContours
cv2.findContours是OpenCV中的一个函数,用于在二进制图像中查找轮廓。它返回一个轮廓列表以及每个轮廓的层次结构。该函数采用以下参数:
- image:输入图像(必须为二进制图像,即黑白图像)。
- mode:轮廓检索模式,可以是cv2.RETR_EXTERNAL(只检测最外层轮廓)、cv2.RETR_LIST(检测所有轮廓,不建立轮廓间的等级关系)、cv2.RETR_TREE(检测所有轮廓,建立轮廓间的等级关系)。
- method:轮廓逼近方法,可以是cv2.CHAIN_APPROX_NONE(存储所有的轮廓点)、cv2.CHAIN_APPROX_SIMPLE(仅存储轮廓的端点)。
- contours:输出参数,表示检测到的轮廓。
- hierarchy:输出参数,表示轮廓的层次结构。
例如,以下代码展示了如何使用cv2.findContours函数:
```
import cv2
# 读取图像
img = cv2.imread('image.png', 0)
# 二值化处理
ret, thresh = cv2.threshold(img, 127, 255, 0)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文