cv2.find_contours
时间: 2023-09-03 12:13:33 浏览: 56
`cv2.find_contours`是OpenCV中的一个函数,用于在二值化图像中寻找轮廓。它的参数为二值化图像,以及轮廓寻找模式和轮廓逼近方法。函数会返回一个包含所有轮廓的列表,每个轮廓都是一个包含(x,y)坐标的数组。我们可以使用这些轮廓信息来进一步处理图像,如计算轮廓面积、周长、绘制轮廓等等。
相关问题
cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
This line of code uses the OpenCV library's "findContours" function to find the contours (boundaries) of the white regions in a binary mask image.
The function takes three arguments:
1. The binary mask image (in this case, a copy of the original mask image)
2. The retrieval mode (cv2.RETR_EXTERNAL) which retrieves only the extreme outer contours (ignoring contours within contours)
3. The contour approximation method (cv2.CHAIN_APPROX_SIMPLE) which compresses horizontal, vertical, and diagonal segments and leaves only their end points.
The function returns two values: the contours themselves and the hierarchy of the contours (which is not needed in this case, so only the contours are returned using the [-2] index).
contours, _ = cv.findContours(binary1, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
This line of code uses the OpenCV library to find contours in a binary image.
- The first argument, "binary1", is the input image. It should be a binary image with white objects on a black background.
- The second argument, "cv.RETR_TREE", specifies the retrieval mode for contours. This mode retrieves all of the contours and reconstructs a full hierarchy of nested contours.
- The third argument, "cv.CHAIN_APPROX_SIMPLE", specifies the contour approximation method. This method compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, a rectangle contour would be represented by only 4 points instead of a series of connected lines.
The function returns two values:
- "contours" is a list of all the contours found in the image, each represented as a list of points.
- The second value is not assigned to anything, so it is discarded. It represents the hierarchy of contours, which is useful for understanding how contours are nested within each other.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)