imutils.contours的参数是什么返回值是什么
时间: 2023-06-01 16:05:42 浏览: 56
imutils.contours是一个函数,用于在图像中查找轮廓。它的输入参数是图像,输出参数是一组轮廓。具体来说,它的输入参数包括二值图像、轮廓查找方法和轮廓逼近方法等。输出参数是一个列表,其中每个元素代表一个轮廓,包含了轮廓的一些属性信息。
相关问题
refCnts = imutils.contours.sort_contours(refCnts, method='left-to-right')[0] # 相当于 refCnts ct = imutils.contours.sort_contours(refCnts, method='left-to-right') digits = {}
在这段代码中,`imutils.contours.sort_contours()`函数用于对轮廓进行排序。它接受两个参数:要排序的轮廓列表和排序的方法。
在这里,`refCnts`是要排序的轮廓列表,`method='left-to-right'`表示按照从左到右的顺序进行排序。
函数返回一个元组,包含两个元素。第一个元素是排序后的轮廓列表,对应于`refCnts`的排序结果。第二个元素是与排序后的轮廓列表相对应的索引数组,可以根据这个索引数组对其他相关数据进行排序。
在您的代码中,将排序后的轮廓列表赋值给了变量`ct`,然后创建了一个空字典`digits`。
这样,您可以使用排序后的轮廓列表进行后续操作,例如识别数字等。
imutils.grab_contours()
`imutils.grab_contours()` 是一个 OpenCV 工具包中的函数,它用于从图像中提取轮廓。
在使用 `findContours()` 函数查找图像中的轮廓后,`imutils.grab_contours()` 函数可以帮助我们处理不同版本的 OpenCV 返回的轮廓值。因为在不同版本的 OpenCV 中,返回的轮廓值可能是不同类型的变量(例如列表或元组),这使得我们在编写通用代码时可能会遇到问题。
`imutils.grab_contours()` 函数可以将返回的轮廓值转换为适当类型的列表,这样我们就可以方便地对其进行迭代和处理。
例如,我们可以使用以下代码来提取图像中的轮廓并绘制出来:
```python
import cv2
import imutils
# 加载图像
image = cv2.imread("image.jpg")
# 将图像转换为灰度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 对图像进行模糊处理
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 使用阈值化进行图像分割
_, thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)
# 查找图像中的轮廓
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
# 在图像上绘制轮廓
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# 显示图像
cv2.imshow("Contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这样,使用 `imutils.grab_contours()` 函数后,我们就可以方便地处理轮廓并在图像上进行绘制。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)