opencv学习程序大集合.docx
- 文档编号:5453724
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:100
- 大小:136.69KB
opencv学习程序大集合.docx
《opencv学习程序大集合.docx》由会员分享,可在线阅读,更多相关《opencv学习程序大集合.docx(100页珍藏版)》请在冰豆网上搜索。
opencv学习程序大集合
Opencv2.3.1中:
每新建一个项目必做的,选择“Debug”,在“附加依赖项”那栏点击右边的“编辑”按钮,复制下面的库文件名到上方空白处:
opencv_calib3d231d.lib
opencv_contrib231d.lib
opencv_core231d.lib
opencv_features2d231d.lib
opencv_flann231d.lib
opencv_gpu231d.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib
opencv_legacy231d.lib
opencv_ml231d.lib
opencv_objdetect231d.lib
opencv_ts231d.lib
opencv_video231d.lib
(可根据实际需要删减)
然后,在“配置”下拉框中选择“Release”,照上面的操作,复制下面的库文件名到上方的空白处:
opencv_calib3d231.lib
opencv_contrib231.lib
opencv_core231.lib
opencv_features2d231.lib
opencv_flann231.lib
opencv_gpu231.lib
opencv_highgui231.lib
opencv_imgproc231.lib
opencv_legacy231.lib
opencv_ml231.lib
opencv_objdetect231.lib
opencv_ts231.lib
opencv_video231.lib
例2-1显示照片
#include"stdafx.h"
#include
usingnamespacestd;
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读¨¢入¨?
一°?
幅¤¨´图ª?
像?
,ê?
将?
之?
反¤¡ä色¦?
,ê?
然¨?
后¨®显?
示º?
出?
来¤¡ä.
//
////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include
intmain(intargc,char*argv[])
{
IplImage*img=0;
intheight,width,step,channels;
uchar*data;
inti,j,k;
constchar*imagename=argc>1?
argv[1]:
"ha.jpg";
/*if(argc<2){
printf("Usage:
main
exit(0);
}
*/
//loadanimage
img=cvLoadImage(imagename);
if(!
img){
printf("Couldnotloadimagefile:
%s\n",argv[1]);
exit(0);
}
//gettheimagedata
height=img->height;
width=img->width;
step=img->widthStep;
channels=img->nChannels;
data=(uchar*)img->imageData;
printf("Processinga%dx%dimagewith%dchannels\n",height,width,channels);
//createawindow
cvNamedWindow("ao",0);
cvMoveWindow("ao",100,100);
//inverttheimage
//相¨¤当Ì¡À于®¨²cvNot(img);
for(i=0;i data[i*step+j*channels+k]=255-data[i*step+j*channels+k]; //showtheimage cvShowImage("ao",img); //waitforakey cvWaitKey(0); //releasetheimage cvReleaseImage(&img); return0; } 单张RGB图片变成灰度图并保存 #include"stdafx.h" #include"cv.h" #include"highgui.h" intmain(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显示视频 #include"stdafx.h" #include #include #include voidmain() { 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滚动条显示视频 #include"stdafx.h" #include #include #include intg_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图像平滑处理 #include"stdafx.h" #include"cv.h" #include"highgui.h" voidexample2_4(IplImage*image) { //Createsomewindowstoshowtheinput //andoutputimagesin. cvNamedWindow("Example4-in"); cvNamedWindow("Example4-out"); //Createawindowtoshowourinputimage // cvShowImage("Example4-in",image); //Createanimagetoholdthesmoothedoutput //cvGetSize(image)当Ì¡À前¡ã图ª? 像? 结¨¢构1的Ì? 大䨮小? 各¡Â通ª¡§道̨¤每? 个? 像? 素? 点Ì? 的Ì? 数ºy据Y类¤¨¤型¨ª,通ª¡§道̨¤的Ì? 总Á¨¹数ºy IplImage*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()使图片长宽各缩小一半 #include"stdafx.h" #include"highgui.h" #include"cv.h" IplImage*doPyrDown(IplImage*in)//intfilter=IPL_GAUSSIAN_5X5 { assert(in->width%2==0&&in->height%2==0); IplImage*out=cvCreateImage( cvSize(in->width/2,in->height/2), in->depth, in->nChannels ); cvPyrDown(in,out,CV_GAUSSIAN_5x5); cvNamedWindow("tu1",1); cvShowImage("tu1",out); cvWaitKey(0); cvSaveImage("wheadai.jpg",out); cvReleaseImage(&out); cvDestroyWindow("tu1"); return(out); }; intmain(intargc,char**argv) { IplImage*img=cvLoadImage("woheadai.jpg"); doPyrDown(img); cvReleaseImage(&img); return0; } 例2-6canny边缘检测将输出写入一个单通道(灰度级)图像 #include"stdafx.h" #include"highgui.h" #include"cv.h" IplImage*doCanny(IplImage*in,doublelowThresh,doublehighThresh,intaperture) { if(in->nChannels! =1) return(0); IplImage*out=cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1); cvCanny(in,out,lowThresh,highThresh,aperture); cvNamedWindow("hb",1); cvShowImage("hb",out); cvWaitKey(0); cvSaveImage("奥ã? 黛¨¬丽¤? ·¡è赫? 本À? .jpg",out); cvReleaseImage(&out); cvDestroyWindow("hb"); return(out); }; intmain(intargc,char**argv) { IplImage*img=cvLoadImage("奥ã? 黛¨¬丽¤? ·¡è赫? 本À? (194).jpg",0); doCanny(img,1,2.4,3); cvReleaseImage(&img); return0; } 例2-7图像处理中两次缩放和canny边缘检测例2-8通过每个独立阶段释放内存来简化2-7中图像处理 #include"stdafx.h" #include"highgui.h" #include"cv.h" IplImage*out; IplImage*doPyrDown(IplImage*in) { assert(in->width%2==0&&in->height%2==0); out=cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels); cvPyrDown(in,out,CV_GAUSSIAN_5x5); return(out); }; IplImage*doCanny(IplImage*in,doublelowThresh,doublehighThresh,intaperture) { if(in->nChannels! =1) return(0); IplImage*out=cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1); cvCanny(in,out,lowThresh,highThresh,aperture); return(out); }; intmain(intargc,char**argv) { IplImage*img=cvLoadImage("ha.jpg",0); out=doPyrDown(img); out=doPyrDown(out); out=doCanny(out,1,3,3); cvNamedWindow("kk",1); cvShowImage("kk",out); cvWaitKey(0); cvSaveImage("haha.jpg",out); cvReleaseImage(&out); cvDestroyWindow("kk"); cvReleaseImage(&img); return0; } 例2-9capture结构初始化后,从摄像设备读入图像并保存(或者从视频无区别) #include"stdafx.h" #include"cv.h" #include"highgui.h" #include"conio.h" intmain(){ CvCapture*capture=cvCaptureFromCAM(CV_CAP_ANY); IplImage*image=NULL;//*dst=NULL; image=cvQueryFrame(capture); CvSizesize=cvGetSize(image); //dst=cvCreateImage(size,image->depth,1); doublefps=10; CvVideoWriter*writer=cvCreateVideoWriter("VideofromCAMERA.avi",CV_FOURCC('X','V','I','D'),fps,size,1); cvNamedWindow("ha",1); while((image=cvQueryFrame(capture))! =NULL) { printf("Enteredn"); //cvCvtColor(image,dst,CV_RGB2GRAY); cvShowImage("ha",image); cvWriteFrame(writer,image); if((cvWaitKey(100))==27) break; } cvReleaseVideoWriter(&writer); cvReleaseCapture(&capture); //cvReleaseImage(&dst); cvDestroyWindow("ha"); } 例2-10彩色视频变灰度视频 //Convertavideotograyscale //argv[1]: inputvideofile //argv[2]: nameofnewoutputfile // #include"stdafx.h" #include"cv.h" #include"highgui.h" intmain(intargc,char*argv[]) { CvCapture*capture=0; capture=cvCreateFileCapture("wanqiu.avi"); if(! capture) { return-1; } IplImage*bgr_frame=cvQueryFrame(capture);//Initthevideoread doublefps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS); CvSizesize=cvSize( (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT) ); CvVideoWriter*writer=cvCreateVideoWriter("hah.avi",CV_FOURCC('M','J','P','G'),fps,size); IplImage*logpolar_frame=cvCreateImage(size,IPL_DEPTH_8U,3); while((bgr_frame=cvQueryFrame(capture))! =NULL) { cvLogP
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- opencv 学习 程序 集合