《学习OpenCV3》第6章课后习题
//ExercisesatendofChapter5,《learningOpenCV3》 #include"stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> usingnamespacecv; usingnamespacestd; voidhelp(constchar**argv){ cout<<"\n\n" <<"ThisprogramsolvestheExercisesattheendofChapter5\n" <<"Call:\n" <<argv[0]<<"<path/image_name>\n\n" <<"Forexample:"<<argv[0]<<"/test.jpg\n" <<endl; } intmain(intargc,constchar**argv) { help(argv); if(argc<2){ cout<<"\nERROR:Youhadtoofewparameters.\n"<<endl; return-1; } /************************************************************************/ /*5.1.Drawingpractice:loadorcreateanddisplayacolorimage.Drawoneexampleof everyshapeandlinethatOpenCVcandraw.*/ /************************************************************************/ Matsrc=imread("e:/template/lena.jpg"); cv::circle(src,Point(100,100),100,Scalar(255,255,255),2);//circle cv::rectangle(src,Point(0,0),Point(300,300),Scalar(255,255,255),2);//rectangle cv::line(src,Point(0,0),Point(300,300),Scalar(255,255,255),2);//line cv::ellipse(src,cv::Point(100,100),Size(100,100),45,0,180,Scalar(255,0,0),2);//ellipse /************************************************************************/ /*5.2.Grayscale:loadanddisplayacolorimage. a.Turnitintothree-channelgrayscale(itisstillanBGRimage,butitlooksgray totheuser). b.Drawcolortextontotheimage.*/ /************************************************************************/ //a Mattmp; cvtColor(src,tmp,COLOR_BGR2GRAY); cvtColor(tmp,src,COLOR_GRAY2BGR); //b putText(src,"puttext",Point(50,30),CV_FONT_HERSHEY_DUPLEX,1.0f,Scalar(0,255,0)); /************************************************************************/ /*5.5.Usecv::LineIteratortocountpixelsondifferentlinesegmentsin,say,a300×300image. a.Atwhatanglesdoyougetthesamenumberofpixelsfor4-connectedand 8-connectedlines? b.Forlinesegmentanglesotherthantheabove,whichcountsmorepixels: 4-connectedor8-connectedlines? c.Foragivenlinesegment,explainthedifferenceinthelengthofthelinecompared tothenumberofpixelsyoucountiteratingalongthelinefor both4-connectedand8-connected?Whichconnectednessisclosertothetrue linelength? /************************************************************************/ //a、 LineIteratorit_4_x(src,Point(0,0),Point(0,100),4); LineIteratorit_8_x(src,Point(0,0),Point(0,100),4); LineIteratorit_4_y(src,Point(0,0),Point(100,0),4); LineIteratorit_8_y(src,Point(0,0),Point(100,0),4); cout<<"it_4_x"<<it_4_x.count<<"it_8_x"<<it_8_x.count<<endl; cout<<"it_4_y"<<it_4_y.count<<"it_8_y"<<it_8_y.count<<endl; //btheansweris:4-connectedcountsmorepixelsthan8-connectedcounts LineIteratorit_4(src,Point(0,0),Point(100,100),4); LineIteratorit_8(src,Point(0,0),Point(100,100),8); cout<<"it_4"<<it_4.count<<"largethanit_8"<<it_8.count<<endl; //c //thedifferenceisthesameasthedifferencebetween4-connectedand8-connected //Ivelevethe8-connectedisclosertothetruelinelength. waitKey(); return0; } 来自为知笔记(Wiz) 目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com