findContours()函数与drawContours()函数 —— opencv学习记录(五)

findContours()函数与drawContours()函数 —— opencv学习记录(五) mini菜 2023-07-12 11:06:49 558

opencv中利用函数 findContours() 可以监测出物体的轮廓
drawContours() 可以画出物体的轮廓

1. findContours()函数源码

  1. void findContours(InputArray image, OutputArrayOfArrays contours,
  2. OutputArray hierarchy, int mode,
  3. int method, Point offset = Point());
  4. image 8位单通道图像。 非零像素被视为1 零像素保持为0,因此图像被视为二值。
  5. 可以使用compareinRangethresholdadaptiveThresholdCanny
  6. 其他方法来从灰度或彩色图像创建二值图像。
  7. contours 监测到的轮廓 原型 vector<vector<Point>> contours; 每个轮廓线存储为一个点的向量
  8. hierarchy 可选输出向量 原型vector<Vec4i> hierarchy;
  9. model 定义轮廓的检索模式 RETR_EXTERNAL 只监测最外围轮廓
  10. RETR_LIST 检测所有轮廓
  11. method 定义轮廓的近似方法 CHAIN_APPROX_NONE 保存物体所有连续的轮廓点到contours向量内
  12. CHAIN_APPROX_SIMPLE 仅保存轮廓的拐点信息 拐点与拐点直接的信息不保留
  13. point 偏移量

2. drawContours()函数源码

  1. void drawContours(InputOutputArray image, InputArrayOfArrays contours,
  2. int contourIdx, const Scalar& color,
  3. int thickness = 1, int lineType = LINE_8,
  4. InputArray hierarchy = noArray(),
  5. int maxLevel = INT_MAX, Point offset = Point());
  6. imgae 目标图像
  7. contours 所有的输入轮廓。 每条轮廓线存储为一个点向量。
  8. contouridx要绘制的轮廓参数。 如果它是负的,就画出所有的等高线。
  9. color 颜色
  10. thickness 宽度 如果是负数 表示填充轮廓内部
  11. lineType 线型
  12. hierarchy可选信息。 它只在你想绘制一些轮廓时才需要(参见maxLevel)。
  13. maxlevel 绘制轮廓的最大水平。 如果为0,则只绘制指定的轮廓。
  14. 如果为1,函数绘制轮廓线和所有嵌套轮廓线。
  15. 如果是2,该函数绘制等值线、所有嵌套等值线、所有嵌套到嵌套等值线,等等。
  16. 只有当存在可用的层次结构时,才会考虑此参数。
  17. offset 可选轮廓偏移参数。 将所有绘制的等高线按指定偏移量(dx, dy)移动。

3 代码

  1. int main()
  2. {
  3. Mat image, img2;
  4. img2 = imread("2_3.jpg", 0);
  5. GaussianBlur(img2, image, Size(3, 3), 0);//高斯
  6. Canny(image, image, 100, 250);//二值化
  7. vector<vector<Point>> contours;
  8. vector<Vec4i> hierarchy;
  9. //3.利用函数findContours()查找图像A的轮廓;
  10. findContours(image, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE, Point());
  11. Mat imageContours = Mat::zeros(image.size(), CV_8UC1);
  12. Mat Contours = Mat::zeros(image.size(), CV_8UC1); //绘制
  13. for (int i = 0; i < contours.size(); i++)
  14. {
  15. //contours[i]代表的是第i个轮廓,contours[i].size()代表的是第i个轮廓上所有的像素点数
  16. for (int j = 0; j < contours[i].size(); j++)
  17. {
  18. //绘制出contours向量内所有的像素点
  19. Point P = Point(contours[i][j].x, contours[i][j].y);
  20. Contours.at<uchar>(P) = 255;//白色像素点
  21. }
  22. }
  23. //绘制轮廓
  24. drawContours(imageContours, contours, -1, Scalar(255));
  25. imshow("Contours Image", imageContours); //轮廓
  26. imshow("Point of Contours", Contours); //向量contours内保存的所有轮廓点
  27. waitKey(0);
  28. }

4 method 参数对比

4.1 CHAIN_APPROX_NONE 保存物体所有连续的轮廓点到contours向量内

4.2 CHAIN_APPROX_SIMPLE 仅保存轮廓的拐点信息 拐点与拐点直接的信息不保留

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
mini菜
红包 点赞 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
mini菜
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区