免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
opencv轮廓提取与轮廓拟合#include #include #include #include #include using namespace std;using namespace cv;int main()/ Read input binary image Mat image= imread(./binaryGroup.bmp,0); if (!image.data)return 0; namedWindow(Binary Image); imshow(Binary Image,image);/ Get the contours of the connected components vectorvector contours; /findContours的输入是二值图像 findContours(image, contours, / a vector of contours CV_RETR_EXTERNAL, / retrieve the external contoursCV_CHAIN_APPROX_NONE); / retrieve all pixels of each contours/ Print contours length轮廓的个数 cout Contours: contours.size() endl; vectorvector:const_iterator itContours= contours.begin();for ( ; itContours!=contours.end(); +itContours) cout Size: size() endl;/每个轮廓包含的点数/ draw black contours on white image Mat result(image.size(),CV_8U,Scalar(0); drawContours(result,contours, /画出轮廓-1, / draw all contoursScalar(255), / in black2); / with a thickness of 2namedWindow(Contours);imshow(Contours,result);/ Eliminate too short or too long contoursint cmin= 100; / minimum contour lengthint cmax= 1000; / maximum contour lengthvectorvector:const_iterator itc= contours.begin();while (itc!=contours.end() if (itc-size() size() cmax)itc= contours.erase(itc);else +itc;/ draw contours on the original imageMat original= imread(./group.bmp);drawContours(original,contours,-1, / draw all contoursScalar(255,255,255), / in white2); / with a thickness of 2namedWindow(Contours on Animals);imshow(Contours on Animals,original);/ Lets now draw black contours on white image/result.setTo(Scalar(0);/If the third parameter of this function is a negative value, then all contours are drawn./Otherwise, it is possible to specify the index of the contour to be drawndrawContours(result,contours,-1, / draw all contoursScalar(255), / in black1); / with a thickness of 1image= imread(./binaryGroup.bmp,1);/ testing the bounding box Rect r0= boundingRect(Mat(contours0);/boundingRect获取这个外接矩形rectangle(result,r0,Scalar(255,255,255),2);/ testing the enclosing circle float radius;Point2f center;minEnclosingCircle(Mat(contours1),center,radius);/对轮廓进行多变形逼近circle(result,Point(center),static_cast(radius),Scalar(255),2);RotatedRect rrect= fitEllipse(Mat(contours2);ellipse(result,rrect,Scalar(255),2); /testing the approximate polygonvector poly;approxPolyDP(Mat(contours2),poly,5,true);cout Polygon size: poly.size() endl;/ Iterate over each segment and draw itvector:const_iterator itp= poly.begin();while (itp!=(poly.end()-1) line(result,*itp,*(itp+1),Scalar(255),2);+itp;/ last point linked to first pointline(result,*(poly.begin(),*(poly.end()-1),Scalar(20),2);/ testing the convex hullvector hull;convexHull(Mat(contours3),hull);/ Iterate over each segment and draw itvector:const_iterator it= hull.begin();while (it!=(hull.end()-1) line(result,*it,*(it+1),Scalar(255),2);+it;/ last point linked to first pointline(result,*(hull.begin(),*(hull.end()-1),Scalar(255),2);/ testing the moments/iterate over all contoursitc= contours.begin();while (itc!=contours.end() / compute all momentsMoments mom= moments(Mat(*itc+);/ draw mass centercircle(result,/ position of mass center converted to integerPoint(mom.m10/mom.m00,mom.m01/mom.m00),2,Scalar(255),2); / draw black dotnamedWindow(Some Shape descriptors);imshow(Some Shape descriptors,result);/ New call to findContours but with CV_RETR_LIST flagimage= imread(./binaryGroup.bmp,0);/ Get the contours of the connected componentsfindContours(image, contours, / a vector of contours CV_RETR_LIST, / retrieve the external and internal contoursCV_CHAIN_APPROX_NONE); / retrieve all pixels of each contours/ draw black contours on white imageresult.setTo(Scalar(0);drawContours(result,contours,-1, / draw all contou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 货运输协议书合同
- 河道围堰专项施工方案
- 施工现场临时用水及消防用水专项方案
- 竖井倒挂井壁专项施工方案
- 莆田市人民医院种植急诊处理考核
- 宿迁市中医院气道管理技能考核
- 淄博市人民医院术中疑难诊断考核
- 吉安市中医院放疗急性反应处理考核
- 上饶市中医院胎盘羊水B超评估考核
- 丽水市中医院免疫抑制患者的输血策略专题考核
- 学堂在线 军事理论 章节测试答案
- 初中生物知识双向细目表
- 影视鉴赏(北京大学)【超星尔雅学习通】章节答案
- 中国建筑科技管理手册
- 科学小常识广播稿
- 《中国古代的民族关系与对外交往》课件
- 2022年上海市初中学业考试地理中考试卷真题(含答案详解)
- GB 5009.236-2016食品安全国家标准动植物油脂水分及挥发物的测定
- 律师事务所员工手册
- 预算绩效管理课件讲解课件
- 《幼儿文学》幼儿文学的基本理论 课件
评论
0/150
提交评论