Opencv 图像结构分析与形状识别—— drawContours函数用于绘制和填充

 在opencv中当我们使用一些基于连通域或者基于边缘信息的时候,往往会对contour进行处理除了利用findContours函数寻找连通域之外,也需要对连通域进行填充等操作这里介绍下drawContours函数,自己也是不断摸索和查阅了opencv相关手册

drawContours    在图像中绘制外部和内部的轮廓。
drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(),int maxLevel=INT_MAX, Point offset=Point())
image: 是最终需要被填充的图像;
contours:是得到的一系列点的集合;
contourIdx:是指定某个contours的点集;
color:被填充的颜色,单色可以设置为Scalar(255)等;
thickness: 所画Contour的线条宽度,如果为负或CV_FILLED则绘制所有的Contours;
lineType&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCVdrawContours函数用于图像绘制轮廓。 函数原型: ```cpp void cv::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:输入/输出图像,必须是8位单通道或三通道图像。 - contours:包含所有轮廓的数组,每个轮廓由点的数组表示。 - contourIdx:需要绘制的轮廓的索引,-1表示绘制所有轮廓。 - color:轮廓颜色,如果是三通道图像,则需要使用Scalar(r,g,b)格式。 - thickness:轮廓线条宽度,如果为负值,则表示填充轮廓内部。 - lineType:轮廓线条类型,有LINE_4、LINE_8、LINE_AA可选。 - hierarchy:轮廓层次结构,可选参数,默认值为noArray()。 - maxLevel:最大层级深度,可选参数,默认值为INT_MAX。 - offset:轮廓偏移量,可选参数,默认值为Point()。 示例: ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat src = imread("test.png"); Mat gray, binary; cvtColor(src, gray, COLOR_BGR2GRAY); threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); std::vector<std::vector<Point>> contours; findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); Mat dst = Mat::zeros(src.size(), CV_8UC3); for (size_t i = 0; i < contours.size(); i++) { drawContours(dst, contours, i, Scalar(0, 0, 255), 2); } imshow("src", src); imshow("dst", dst); waitKey(); return 0; } ``` 原图与绘制的轮廓如下所示: ![drawContours](https://img-blog.csdn.net/20180519223224420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F3ZXJ0eV9zdmNfMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值