您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 纺织服装 > opencv学习程序大集合
Opencv2.3.1中:每新建一个项目必做的,选择“Debug”,在“附加依赖项”那栏点击右边的“编辑”按钮,复制下面的库文件名到上方空白处:opencv_calib3d231d.libopencv_contrib231d.libopencv_core231d.libopencv_features2d231d.libopencv_flann231d.libopencv_gpu231d.libopencv_highgui231d.libopencv_imgproc231d.libopencv_legacy231d.libopencv_ml231d.libopencv_objdetect231d.libopencv_ts231d.libopencv_video231d.lib(可根据实际需要删减)然后,在“配置”下拉框中选择“Release”,照上面的操作,复制下面的库文件名到上方的空白处:opencv_calib3d231.libopencv_contrib231.libopencv_core231.libopencv_features2d231.libopencv_flann231.libopencv_gpu231.libopencv_highgui231.libopencv_imgproc231.libopencv_legacy231.libopencv_ml231.libopencv_objdetect231.libopencv_ts231.libopencv_video231.lib例2-1显示照片#includestdafx.h#includeopencv2/opencv.hppusingnamespacestd;usingnamespacecv;intmain(intargc,char*argv[]){constchar*imagename=woheadai.jpg;//从䨮文?件t中D读¨¢入¨?图ª?像?Matimg=imread(imagename);//如¨?果?读¨¢入¨?图ª?像?失º¡ì败㨹if(img.empty()){fprintf(stderr,Cannotloadimage%s\n,imagename);return-1;}//显?示º?图ª?像?imshow(image,img);//此ä?函¡¥数ºy等̨¨待äy按ã¡ä键¨¹,ê?按ã¡ä键¨¹盘¨¬任¨?意°a键¨¹就¨ª返¤¦Ì回?waitKey();return0;}从文件中读入一幅图像,将之反色,然后显示出来////////////////////////////////////////////////////////////////////////////hello-world.cpp////该?程¨¬序¨°从䨮文?件t中D读¨¢入¨?一°?幅¤¨´图ª?像?,ê?将?之?反¤¡ä色¦?,ê?然¨?后¨®显?示º?出?来¤¡ä.//////////////////////////////////////////////////////////////////////////#includestdafx.h#includestdlib.h#includestdio.h#includemath.h#includecv.h#includehighgui.hintmain(intargc,char*argv[]){IplImage*img=0;intheight,width,step,channels;uchar*data;inti,j,k;constchar*imagename=argc1?argv[1]:ha.jpg;/*if(argc2){printf(Usage:mainimage-file-name\n\7);exit(0);}*///loadanimageimg=cvLoadImage(imagename);if(!img){printf(Couldnotloadimagefile:%s\n,argv[1]);exit(0);}//gettheimagedataheight=img-height;width=img-width;step=img-widthStep;channels=img-nChannels;data=(uchar*)img-imageData;printf(Processinga%dx%dimagewith%dchannels\n,height,width,channels);//createawindowcvNamedWindow(ao,0);cvMoveWindow(ao,100,100);//inverttheimage//相¨¤当Ì¡À于®¨²cvNot(img);for(i=0;iheight;i++)for(j=0;jwidth;j++)for(k=0;kchannels;k++)data[i*step+j*channels+k]=255-data[i*step+j*channels+k];//showtheimagecvShowImage(ao,img);//waitforakeycvWaitKey(0);//releasetheimagecvReleaseImage(&img);return0;}单张RGB图片变成灰度图并保存#includestdafx.h#includecv.h#includehighgui.hintmain(intargc,char*argv[]){IplImage*image;IplImage*result;image=cvLoadImage(lena.jpg,-1);//注|意指针变量定要先初始化才能使用,否则崩溃//灰度转换时通道一定¬要设置正确intchannel=1;//image-nChannels;intdepth=image-depth;CvSizesz;sz.width=image-width;sz.height=image-height;result=cvCreateImage(sz,depth,channel);cvCvtColor(image,result,CV_BGR2GRAY);cvNamedWindow(original,1);cvShowImage(original,image);cvNamedWindow(gray,1);cvShowImage(gray,result);cvSaveImage(hlena.jpg,result);cvWaitKey(0);cvReleaseImage(&image);cvReleaseImage(&result);cvDestroyWindow(original);cvDestroyWindow(gray);return0;}例2-2显示视频#includestdafx.h#includecv.h#includecxcore.h#includehighgui.hvoidmain(){cvNamedWindow(example2,0);CvCapture*capture=cvCreateFileCapture(晚ª¨ª秋?电Ì?影®¡ã<晚ª¨ª秋?>同ª?名?主¡Â题¬a曲¨²--音°?悦?台¬¡..mp4);IplImage*frame;while(1){frame=cvQueryFrame(capture);if(!frame){break;}cvShowImage(example2,frame);charc=cvWaitKey(33);if(c==27){break;}}cvReleaseCapture(&capture);cvDestroyWindow(example2);}例2-3滚动条显示视频#includestdafx.h#includecv.h#includecxcore.h#includehighgui.hintg_slider_position=0;CvCapture*g_capture=NULL;voidonTrackbarSlide(intpos){cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);}intmain(intargc,char**argv){cvNamedWindow(Example3,CV_WINDOW_AUTOSIZE);g_capture=cvCreateFileCapture(晚ª¨ª秋?电Ì?影®¡ã<晚ª¨ª秋?>同ª?名?主¡Â题¬a曲¨²--音°?悦?台¬¡§.mp4);intframes=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);if(frames!=0){cvCreateTrackbar(Position,Example3,&g_slider_position,frames,onTrackbarSlide);}IplImage*frame;while(1){frame=cvQueryFrame(g_capture);if(!frame){break;}cvShowImage(example2,frame);charc=cvWaitKey(33);if(c==27){break;}}//Whileloop(asinExample2)capture&showvideo…-cvReleaseCapture(&g_capture);cvDestroyWindow(example2);//Releasememoryanddestroywindow…-return(0);}例2—4图像平滑处理#includestdafx.h#includecv.h#includehighgui.hvoidexample2_4(IplImage*image){//Createsomewindowstoshowtheinput//andoutputimagesin.cvNamedWindow(Example4-in);cvNamedWindow(Example4-out);//Createawindowtoshowourinputimage//cvShowImage(Example4-in,image);//Createanimagetoholdthesmoothedoutput//cvGetSize(image)当Ì¡À前¡ã图ª?像?结¨¢构1的Ì?大䨮小?,各¡Â通ª¡§道̨¤每?个?像?素?点Ì?的Ì?数ºy据Y类¤¨¤型¨ª,通ª¡§道̨¤的Ì?总Á¨¹数ºyIplImage*out=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);//Dothesmoothing//cvSmooth(image,out,CV_GAUSSIAN,3,3);//CV_GAUSSIAN对?图ª?像?进?行D核?大䨮小?为aparam1ר¢param2的Ì?高?斯1卷¨ª积y。¡ê//Showthesmoothedimageintheoutputwindow//cvShowImage(Example4-out,out);//Betidy//cvReleaseImage(&out);//Waitfortheusertohitakey,thencleanupthewindows//cvWaitKey(0);cvDestroyWindow(Example4-in);cvDestroyWindow(Example4-out);}intmain(void){IplImage*img=cvLoadImage(woheadai.jpg);example2_4(img);return1;}例2-5使用cvPyDown()使图片长宽各缩小一半#includestda
本文标题:opencv学习程序大集合
链接地址:https://www.777doc.com/doc-5233485 .html