opencvpython填充轮廓
时间: 2023-11-17 11:04:18 浏览: 157
以下是使用OpenCV Python填充轮廓的示例代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 创建一个黑色图像
mask = np.zeros(img.shape, np.uint8)
绘制轮廓
cv2.drawContours(mask, contours, -1, (255, 255, 255), -1)
# 显示原始图像和掩膜图像
cv2.imshow('Original Image', img)
cv2.imshow('Mask', mask)
# 等待按下任意按键退出
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上面的代码中,我们首先读取图像并将其转换为灰度图像。然后,我们对图像进行二值化处理,并使用`cv2.findContours()`函数查找轮廓。接下来,我们创建一个与原始图像大小相同的黑色图像,并使用`cv2.drawContours()`函数绘制轮廓。最后,我们将原始图像和掩膜图像显示出来。
阅读全文
相关推荐














