资源描述:
《opencv学习(二)---图像的加载》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、OpenCV入门(二)图像的加载Opencv以及VS的相关配置见前面一篇博文。一、加载一张图片#include"stdafx.h"#include"highgui.h"intmain(intargc,char**argv){IplImage*pImg;//声明IplImage指针//载入图像if(argc==2&&(pImg=cvLoadImage(argv[1],1))!=0){cvNamedWindow("Image",1);//创建窗口cvShowImage("Image",pImg);//显示图像cvWaitKey(0);//等待按键cvDestroyWindow("Ima
2、ge");//销毁窗口cvReleaseImage(&pImg);//释放图像return0;}return-1;//此处if做了一个异常处理,如果加载图片不成功,则会出现cmd窗口一闪即消失的情况。}注解:上面是opencv中文论坛的程序。另外一种版本,比较简单,不做判断,直接加载图像。注解:cvLoadImage(filename,-1);默认读取图像的原通道数cvLoadImage(filename,0);强制转化读取图像为灰度图cvLoadImage(filename,1);读取彩色图intmain(intargc,char**argv){ IplImage*img=cvL
3、oadImage(argv[1]); cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE); cvShowImage("Example1",img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("Example1");}注解:设置命令参数来进行加载。也可以直接加载,不过记住路径是\而不是,程序如下。intmain(){//加载一张图片IplImage*Image1=cvLoadImage("C:\ProgramFiles\OpenCV\samples\test1.jpg",
4、0);otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationof
5、StateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand//创建窗体显示cvNamedWindow("Image1",1);cvShowImage("Image1",Image1);cvWaitKey(0);cvDestroyWindow("Image1");//销毁窗口cvReleaseImage(&Image1);//释放图像retur
6、n0;}二、加载两张图片#include"stdafx.h"#include"highgui.h"intmain(intargc,char**argv){IplImage*pImg1;IplImage*pImg2;//声明IplImage指针//载入图像if(argc==3&&(pImg1=cvLoadImage(argv[1],1))!=0&&(pImg2=cvLoadImage(argv[2],1))!=0)//参数改为3了{cvNamedWindow("Image1",1);//创建窗口cvShowImage("Image1",pImg1);//显示图像cvNamedWind
7、ow("Image2",1);//创建窗口cvShowImage("Image2",pImg2);//显示图像cvWaitKey(0);//等待按键cvDestroyWindow("Image1");//销毁窗口cvReleaseImage(&pImg1);//释放图像cvDestroyWindow("Image2");//销毁窗口cvReleaseImage(&pImg2);//释放图像return0;}return-1;//此处if做了一个异常处理,如果加载图片不