常大OPENCV(图像处理参考函数)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

序号:学号:xxxxxxxxxxxCHANGZHOUUNIVERSITY作业报告学生姓名:xxxxx学院(系):信息、数理学院专业班级:应数1311、彩色图像转换为灰度图像#includehighgui.h#includecv.h#includecvaux.hintmain(intargc,char*argv[]){constchar*imagename=lena.jpg;IplImage*img=cvLoadImage(imagename);if(!img){fprintf(stderr,Cannotloadimage%s\n,imagename);return-1;}if(!img-imageData)//检查是否正确载入图像return-1;cvNamedWindow(image,CV_WINDOW_AUTOSIZE);//创建窗口cvShowImage(image,img);//显示图像IplImage*img1=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);//创建目标图像cvCvtColor(img,img1,CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)cvNamedWindow(gray_image,CV_WINDOW_AUTOSIZE);//创建显示目标的窗口cvShowImage(gray_image,img1);//显示灰度图像cvWaitKey();cvReleaseImage(&img);cvReleaseImage(&img1);cvDestroyAllWindows();return0;}2、灰度图像反色#includestdio.h#includemath.h#includecv.h#includehighgui.hintmain(intargc,char*argv[]){IplImage*img=0;intheight,width,step,channels;UCHAR*data;inti,j,k;if(argc2){printf(Usage:InvImageimage-file-name\n\7);exit(0);}img=cvLoadImage(argv[1]);if(!img){printf(Couldnotloadimagefile:%s\n,argv[1]);exit(0);}height=img-height;width=img-width;step=img-widthStep;channels=img-nChannels;data=(UCHAR*)img-imageData;printf(Processinga%d*%dimagewith%dchannels\n,height,width,channels);cvNamedWindow(mainWin,CV_WINDOW_AUTOSIZE);cvMoveWindow(mainWin,100,100);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];cvShowImage(mainWin,img);cvWaitKey(0);cvReleaseImage(&img);return0;}3、彩色图像灰度化后二值化#includecv.h#includecxcore.h#includehighgui.h#includeiostreamusingnamespacestd;//声明图像IplImage指针IplImage*pSrcImg=NULL;IplImage*pGrayImg=NULL;IplImage*pBinaryImg=NULL;intThresh=127;//初始化滑动条位置//滑动条响应函数voidonTrackerSlid(intthresh){//将图像转化为二值图pBinaryImg=cvCreateImage(cvGetSize(pSrcImg),pSrcImg-depth,pSrcImg-nChannels);cvCopy(pSrcImg,pBinaryImg);//复制图像数据CvScalarscalar;intavg;for(inti=0;ipBinaryImg-height;i++){for(intj=0;jpBinaryImg-width;j++){scalar=cvGet2D(pBinaryImg,i,j);//获取点(i,j)的像素avg=(scalar.val[0]+scalar.val[1]+scalar.val[2])/3;if(avgthresh)//与阈值比较{scalar.val[0]=scalar.val[1]=scalar.val[2]=0;cvSet2D(pBinaryImg,i,j,scalar);}else{scalar.val[0]=scalar.val[1]=scalar.val[2]=255;cvSet2D(pBinaryImg,i,j,scalar);}}}//显示图像cvShowImage(二值化图像,pBinaryImg);}intmain(intargc,char**argv){if(argc!=2)argv[1]=lena.jpg;//载入图像if((pSrcImg=cvLoadImage(argv[1],1))!=0){cout按Esc键退出...endl;//将颜色空间由RGB转化为GraypGrayImg=cvCreateImage(cvGetSize(pSrcImg),8,1);cvCvtColor(pSrcImg,pGrayImg,CV_RGB2GRAY);//创建图像窗口cvNamedWindow(原图像,CV_WINDOW_AUTOSIZE);cvNamedWindow(灰度图像,CV_WINDOW_AUTOSIZE);cvNamedWindow(二值化图像,CV_WINDOW_AUTOSIZE);//添加滑动条来调节阈值cvCreateTrackbar(阈值,二值化图像,&Thresh,255,onTrackerSlid);onTrackerSlid(Thresh);//显示图像cvShowImage(原图像,pSrcImg);cvShowImage(灰度图像,pGrayImg);//保存图像cvSaveImage(Gray_Image.jpg,pGrayImg);cvSaveImage(Binary_Image.jpg,pBinaryImg);//等待按Esc键退出while(true)if(cvWaitKey(100)==27)break;//销毁窗口cvDestroyWindow(原图像);cvDestroyWindow(灰度图像);cvDestroyWindow(二值化图像);//释放图像cvReleaseImage(&pSrcImg);cvReleaseImage(&pGrayImg);cvReleaseImage(&pBinaryImg);return0;}return-1;}4、分别提取彩色图像中的RGB信息并显示#includecv.h#includecxcore.h#includehighgui.h#pragmacomment(lib,cv.lib)#pragmacomment(lib,cxcore.lib)#pragmacomment(lib,highgui.lib)//////////////////////////////////////////////////////////////////////////#includeiostreamusingnamespacestd;constchar*pstrSrcWnd=Src;constchar*pstrBHistWnd=bplane;constchar*pstrGHistWnd=gplane;constchar*pstrRHistWnd=rplane;//计算和绘制直方图(R,G,B)/*img通道图像*hist_img:直方图的绘图图像*pstrWndName:绘图窗口*/voiddraw_histogram(IplImage*img,IplImage*hist_img,constchar*pstrWndName){CvHistogram*hist=NULL;intbin_count=256;floatrange[]={0,255};float*ranges[]={range};hist=cvCreateHist(1,//一维&bin_count,//每一维上bin(直方柱)的个数,此处为256【由上述两个参数,函数就会创建一个1*256的矩阵】CV_HIST_ARRAY,ranges,1);cvClearHist(hist);//防止初始化时有其它数据,先清理一下cvCalcHist(&img,hist,0,0);//得到直方图的最值及标号floatmin,max;intmin_idx,max_idx;cvGetMinMaxHistValue(hist,&min,&max,&min_idx,&max_idx);//coutmin:minmax:maxendl;if(max==0){coutmax=0err!endl;max=100;}//缩放直方图的大小,和图像相适应cvScale(hist-bins,hist-bins,((double)hist_img-height)/max,0);//设置所有的直方图的数值为255cvSet(hist_img,cvScalarAll(255),0);//平均每个直放柱的宽度intbin_w=cvRound((double)hist_img-width/bin_count);//画直方图for(inti=0;ibin_count;i++){cvRectangle(hist_img,cvPoint(i*bin_w,hist_img-height),//左下角的点(i*bin_w,height)cvPoint((i+1)*bin_w,hist_img-height-cvRound(cvGetReal1D(hist-bins,i))),//右上角的点((i+1)*bin_w,图像高度-直方柱的高度)cvScalarAll(0),-1,8,0);}//显示直方图cvShowImage(pstrWndName,hist_img);}intmain(intargc,char*argv[]){//创建窗口cvNamedWindow(pstrSrcWnd,1);cvNamedWindow(pstrBHistWnd,1);cvNamedWindow(pstrGHistWnd,1);cvNamedWindow(pstrRHistWnd,1);IplImage*Img;//声明IplImage指针//载入图像if(argc==2&&(Img=cvLoadImage(argv[1],1))!=0)cvNamedWindow(Image,1);//创建窗口cvShowImage(Image,Img);//显示图像//BGR通道CvSizeimg_size;img_size.width=frame-width;img_size.height=frame-height;IplImage*b=cvCreateImage(img_size,8,1);IplImage*g=cvCreateImage(img_size,8,1);IplImage*r=cvCreateImage(img_size,8,1);//直方图CvSizesize;s

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功