opencv轮廓提取与轮廓拟合

上传人:pu****.1 文档编号:543852507 上传时间:2022-11-21 格式:DOC 页数:5 大小:43KB
返回 下载 相关 举报
opencv轮廓提取与轮廓拟合_第1页
第1页 / 共5页
opencv轮廓提取与轮廓拟合_第2页
第2页 / 共5页
opencv轮廓提取与轮廓拟合_第3页
第3页 / 共5页
opencv轮廓提取与轮廓拟合_第4页
第4页 / 共5页
opencv轮廓提取与轮廓拟合_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《opencv轮廓提取与轮廓拟合》由会员分享,可在线阅读,更多相关《opencv轮廓提取与轮廓拟合(5页珍藏版)》请在金锄头文库上搜索。

1、opencv轮廓提取与轮廓拟合#i nclude #in elude #i nclude #i nclude #in clude using n amespace std;using n amespace cv;int main()/ Read in put binary imageMat image= imread(./bi naryGroup.bmp,。);if (!image.data)return 0;n amedWi ndow(Bi nary Image);imshow(Binary lmage,image);/ Get the con tours of the conn ected

2、 comp onents vectorvector con tours;/fin dCo ntours的输入是二值图像findCon tours(image,con tours, / a vector of con tours CV_RETR_EXTERNAL, / retrieve the external c on tours CV_CHAIN_APPROX_NONE); / retrieve all pixels of each con tours/ Print con tours le ngth轮廓的个数cout Con tours: con tours.size() en dl; v

3、ectorvector:c on st_iterator itC on tours= con tours.begi n();for ( ; itC on tours!=c on tours.e nd(); +itC on tours) 每个cout Size: size() en dl;/ 轮廓包含的点数/ draw black con tours on white imageMat result(image.size(),CV_8U,Scalar(0);drawC on tours(result,c on tours,/画出轮廓-1, / draw all con tours Scalar(

4、255), / in black2); / with a thickness of 2n amedWi ndow(Co ntours); imshow(C on tours,result);/ Elim in ate too short or too long con tours int cmi n= 100; / mi nimum con tour len gth int cmax= 1000; / maximum con tour len gth vectorvector:c on st_iterator itc= con tours.begi n(); while (itc!=c on

5、tours.e nd() if (itc-size() size() cmax)itc= con tours.erase(itc);else+itc;/ draw con tours on the orig inal image Mat origi nal= imread(./group.bmp); drawC on tours(orig in al,c on tours,-1, / draw all con toursScalar(255,255,255), / in white2); / with a thickness of 2n amedWi ndow(Co ntours on Ani

6、 mals); imshow(C on tours on Ani mals,origi nal);/ Lets now draw black con tours on white image result.setTo(Scalar(0);/If the third parameter of this function is a negative value, the n all con tours are draw n./Otherwise, it is possible to specify the in dex of the con tour to be draw ndrawC on to

7、urs(result,c on tours,-1, / draw all con tours Scalar(255), / in black1); / with a thick ness of 1image= imread(./bi naryGroup.bmp,1);/ test ing the bounding box获取这个对轮廓进Rect r0= bou ndin gRect(Mat(co ntours0);/bou ndin gRect外接矩形recta ngle(result,r0,Scalar(255,255,255),2);/ testing the enclosing circ

8、lefloat radius;Poin t2f cen ter;minEn closi ngCircle(Mat(co ntours1),ce nter,radius);/ 行多变形逼近circle(result,Poi nt(ce nter),static_cast(radius),Scalar(2 55),2);RotatedRect rrect= fitEllipse(Mat(co ntours2); ellipse(result,rrect,Scalar(255),2);/testi ng the approximate polyg on vectorvPo int poly;appr

9、oxPolyDP(Mat(co ntours2),poly,5,true);cout Polyg on size: poly.size() en dl;/ Iterate over each segme nt and draw it vector:c on st_iterator itp= poly.beg in(); while (itp!=(poly.end()-1) lin e(result,*itp,*(itp+1),Scalar(255),2);+itp;/ last point lin ked to first pointlin e(result,*(poly.begi n(),*

10、(poly.e nd()-1),Scalar(20),2);/ test ing the convex hull vector hull; con vexHull(Mat(co ntours3),hull);/ Iterate over each segme nt and draw it vector:c on st_iterator it= hull.begi n(); while (it!=(hull.end()-1) lin e(result,*it,*(it+1),Scalar(255),2);+it;/ last point lin ked to first pointlin e(r

11、esult,*(hull.begi n(),*(hull.e nd()-1),Scalar(255),2);/ testi ng the mome nts/iterate over all con toursitc= con tours.beg in();while (itc!=c on tours.e nd() / compute all mome ntsMome nts mom= mome nts(Mat(*itc+);/ draw mass cen tercircle(result,/ positi on of mass cen ter con verted to in teger Po

12、i nt(mom.m10/mom.m00,mom.m01/mom.m00), 2,Scalar(255),2); / draw black dotn amedWi ndow(Some Shape descriptors); imshow(Some Shape descriptors,result);/ New call to fin dCo ntours but with CV_RETR_LIST flag image= imread(./b in aryGroup.bmp,。);/ Get the con tours of the conn ected comp onents findCon

13、 tours(image,con tours, / a vector of con tours CV_RETR_LIST, / retrieve the external a nd in ternal con toursCV_CHAIN_APPROX_NONE); / retrieve all pixels of each con tours/ draw black con tours on white image result.setTo(Scalar(0);drawC on tours(result,c on tours,-1, / draw all con toursScalar(255), / in black2); / with a thickness of 2n amedWi ndow(All Co ntours);imshow(All Con tours,result);waitKey(); return 0;一、对于相对路径的读取./表示当前目录下,即cpp所在目录下。./表示上一目录下二、 group.bmpbin aryGroup.bmp HH

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号