资源描述:
《OPENCV实现的轮廓检测与处理.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、////Thefull"SquareDetector"program.//Itloadsseveralimagessubsequentallyandtriestofindsquaresin//eachimage//#ifdef_CH_#pragmapackage#endif#ifndef_EiC#include"cv.h"#include"highgui.h"#include#include#include#endifintthresh=50;IplImage*img=0;IplImage*img0=0;CvMemStor
2、age*storage=0;CvPointpt[4];constchar*wndname="SquareDetectionDemo";//helperfunction://findsacosineofanglebetweenvectors//frompt0->pt1andfrompt0->pt2doubleangle(CvPoint*pt1,CvPoint*pt2,CvPoint*pt0){doubledx1=pt1->x-pt0->x;doubledy1=pt1->y-pt0->y;doubledx2=pt2->x-pt0->x;doubledy2=pt2->y-pt0->y;return
3、(dx1*dx2+dy1*dy2)/sqrt((dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2)+1e-10);}//returnssequenceofsquaresdetectedontheimage.//thesequenceisstoredinthespecifiedmemorystorageCvSeq*findSquares4(IplImage*img,CvMemStorage*storage){CvSeq*contours;inti,c,l,N=11;CvSizesz=cvSize(img->width&-2,img->height&-2);IplImage*t
4、img=cvCloneImage(img);//makeacopyofinputimageIplImage*gray=cvCreateImage(sz,8,1);IplImage*pyr=cvCreateImage(cvSize(sz.width/2,sz.height/2),8,3);IplImage*tgray;CvSeq*result;doubles,t;//createemptysequencethatwillcontainpoints-//4pointspersquare(thesquare'svertices)CvSeq*squares=cvCreateSeq(0,sizeof(
5、CvSeq),sizeof(CvPoint),storage);//selectthemaximumROIintheimage//withthewidthandheightdivisibleby2cvSetImageROI(timg,cvRect(0,0,sz.width,sz.height));//down-scaleandupscaletheimagetofilteroutthenoisecvPyrDown(timg,pyr,7);cvPyrUp(pyr,timg,7);tgray=cvCreateImage(sz,8,1);//findsquaresineverycolorplaneo
6、ftheimagefor(c=0;c<3;c++){//extractthec-thcolorplanecvSetImageCOI(timg,c+1);cvCopy(timg,tgray,0);//tryseveralthresholdlevelsfor(l=0;l7、o0(whichforcesedgesmerging)cvCanny(tgray,gray,0,thresh,5);//dilatecannyoutputtoremovepotential//holesbetweenedgesegmentscvDilate(gray,gray,0,1);}else{//applythresholdifl!=0://tgray(x,y)=gray(x,y)<(l+1)*255/