drawContours() 函数作用
时间: 2023-11-14 18:24:46 浏览: 79
OpenCV中的drawContours()函数可以用于在图像上绘制轮廓线。它可以接受三个参数:源图像、要绘制的轮廓、轮廓的索引以及绘制颜色。如果轮廓的索引为负数,则绘制所有轮廓;如果绘制颜色为负数,则绘制轮廓的外边界。
示例代码:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 提取轮廓
contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例代码中,我们首先读取了一张图像,并将其转换为灰度图像。然后使用findContours()函数提取了图像中的轮廓,接着使用drawContours()函数将轮廓绘制在原始图像上,并显示出来。
相关问题
drawContours函数用法
drawContours函数是OpenCV中的一个函数,用于在图像上绘制轮廓。它的用法如下:
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness = 1, int lineType = LINE_8, InputArray hierarchy = noArray(), int maxLevel = INT_MAX, Point offset = Point())
其中,image是要绘制轮廓的图像,contours是轮廓的数组,contourIdx是要绘制的轮廓的索引,color是轮廓的颜色,thickness是轮廓的线宽,lineType是轮廓的线型,hierarchy是轮廓的层次结构,maxLevel是要绘制的轮廓的最大层次,offset是轮廓的偏移量。
例如,以下代码可以在图像上绘制所有轮廓:
Mat image = imread("image.jpg");
vector<vector<Point>> contours;
findContours(image, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(image, contours, -1, Scalar(0, 0, 255), 2);
这段代码首先读取一张图像,然后使用findContours函数找到图像中的所有轮廓,最后使用drawContours函数在图像上绘制所有轮廓。
opencv drawcontours函数
OpenCV中的drawContours函数是用来绘制轮廓的函数。它可以在图像上绘制指定轮廓的边界线。该函数需要传入三个参数:绘制轮廓的图像、轮廓的点集、轮廓的索引。可以通过设置参数来控制绘制的线条颜色、粗细等属性。该函数可以用于图像分割、形状识别等领域。
阅读全文
相关推荐















