基于C++的opencv练习findcontours 过程

#include<iostream>
#include<opencv2/opencv.hpp>
#include<string>
using namespace std;
using namespace cv;
int main()
{
 Mat img = imread("contours.jpg");//边界线在绘制的时候是使用3通道rgb绘制的所以应该绘制在rgb图像上
 Mat dsimg; //进行图像转换将rgb转化为灰度图像进行二值化然后边缘提取
 cvtColor(img, dsimg, CV_RGB2GRAY);
 Mat outimg;//用于保存二值化之后的图像然后进行轮廓检测
 namedWindow("contours", 2);//选择2 表示显示的窗口能够进行自己随意调整
 threshold(dsimg, outimg,200,255,THRESH_BINARY_INV); //反二值化
 vector<vector<Point>> contours;  //建立一个双重列表格式,外层一个动态列表储存编号,内层储存编号下的坐标点
 vector<Vec4i> hierarchy;//1*4的列表
 findContours(outimg, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
 //int index = hierarchy[0][0];
 //Scalar color(255,255, 0);
 //drawContours(img, contours, 1, color, FILLED, 8, hierarchy);
 cout << contours[1] << endl;
 int index = 0; //当index返回值为-1是表示结束没有下一个轮廓
 for (; index != -1; index = hierarchy[index][0])
 {
  Scalar color(0, 0, 255);
  drawContours(img, contours, index, color, FILLED, 8, hierarchy);
 }
 imshow("contours",img);
 waitKey(0);
 //system("pause");
 return 1;
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值