学习Opencv参考程序源码_第1页
学习Opencv参考程序源码_第2页
学习Opencv参考程序源码_第3页
学习Opencv参考程序源码_第4页
学习Opencv参考程序源码_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、4.3 #include <cv.h>#include <highgui.h>#include <stdio.h>/全局变量CvRect g_rect;bool g_isdown;void my_callback(int event, int x, int y, int flags, void* param);/将image图像的rect区域部分做加亮处理void high_light(IplImage *image,CvRect rect);/对选中的部分绘制直方图void Draw_hist(IplImage* img,CvRect Rect);int

2、main()/载入图像IplImage *orginal_image=cvLoadImage("lena.jpg");assert(orginal_image!=NULL);/创建临时图像IplImage *temp_image=cvCreateImage(cvGetSize(orginal_image), orginal_image->depth, orginal_image->nChannels);assert(temp_image!=NULL);/创建窗口并指定其回调函数cvNamedWindow("Show");cvSetMouseC

3、allback("Show",my_callback,(void*)temp_image);while (1)IplImage *temp_image= cvCloneImage(orginal_image);if (true=g_isdown)high_light(temp_image,g_rect);Draw_hist(temp_image,g_rect);elsehigh_light(temp_image,g_rect);Draw_hist(temp_image,g_rect);cvShowImage("Show",temp_image);if (

4、27=cvWaitKey(30)break;cvDestroyAllWindows();return 0;void my_callback(int event, int x, int y, int flags, void* param)IplImage *image=(IplImage*)(param);switch (event)case CV_EVENT_LBUTTONDOWN :g_isdown=true;g_rect=cvRect(x,y,0,0); break;case CV_EVENT_MOUSEMOVE:if (true=g_isdown)g_rect.width=x-g_rec

5、t.x;g_rect.height=y-g_rect.y;break;case CV_EVENT_LBUTTONUP :g_isdown=false;if (g_rect.width<0)g_rect.x=g_rect.x+g_rect.width;g_rect.width*=-1;if (g_rect.height<0)g_rect.y=g_rect.y+g_rect.height;g_rect.height*=-1;high_light(image,g_rect);Draw_hist(image,g_rect);/printf("n %d %d %d %dn"

6、;,g_rect.x,g_rect.y,g_rect.width,g_rect.height); break;/将图像进行加亮处理void high_light(IplImage *image,CvRect rect)assert(image!=NULL);int row,col;for (row=rect.y;row<rect.y+rect.height;row+)uchar *ptr=(uchar*)(image->imageData+row*image->widthStep);for (col=rect.x;col<rect.x+rect.width;col+)p

7、tr(col*3+1)=150;ptr(col*3+2)=110;/if (row>=image->height-1) row=image->height; /对选中的区域绘制直方图void Draw_hist(IplImage* img,CvRect Rect)if (Rect.x<=0|Rect.y<=0|Rect.width<=0|Rect.height<=0)return;cvSetImageROI(img,Rect);IplImage* hsv = cvCreateImage(/*cvSize(Rect.width ,Rect.height)

8、*/cvGetSize(img), 8, 3 );IplImage* h_plane = cvCreateImage(/*cvSize(Rect.width ,Rect.height)*/cvGetSize(img), 8, 1 );IplImage* s_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );IplImage* v_plane = cvCreateImage( cvSize(Rect.width ,Rect.height), 8, 1 );IplImage* planes = h_plane, s_pla

9、ne ;/* H 分量划分为16个等级,S分量划分为8个等级 */int h_bins = 16, s_bins = 8;int hist_size = h_bins, s_bins;/* H 分量的变化范围 */float h_ranges = 0, 180 ; /* S 分量的变化范围*/float s_ranges = 0, 255 ;float* ranges = h_ranges, s_ranges ;/* 输入图像转换到HSV颜色空间 */cvCvtColor( img, hsv, CV_BGR2HSV );cvResetImageROI(img);cvCvtPixToPlane(

10、 hsv, h_plane, s_plane, v_plane, 0 );/* 创建直方图,二维, 每个维度上均分 */CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );/* 根据H,S两个平面数据统计直方图 */cvCalcHist( planes, hist, 0, 0 );/* 获取直方图统计的最大值,用于动态显示直方图 */float max_value;cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );/* 设置直方图显示图像

11、 */int height = 480;int width = (h_bins*s_bins*6);IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );cvZero( hist_img );/* 用来进行HSV到RGB颜色转换的临时单位图像 */IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);int bin_w = width / (h_bins *

12、s_bins);for(int h = 0; h < h_bins; h+)for(int s = 0; s < s_bins; s+)int i = h*s_bins + s;/* 获得直方图中的统计次数,计算显示在图像中的高度 */float bin_val = cvQueryHistValue_2D( hist, h, s );int intensity = cvRound(bin_val*height/max_value);/* 获得当前直方图代表的颜色,转换成RGB用于绘制 */cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins

13、,s*255.f/s_bins,255,0);cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);CvScalar color = cvGet2D(rgb_color,0,0);cvRectangle( hist_img, cvPoint(i*bin_w,height),cvPoint(i+1)*bin_w,height - intensity),color, -1, 8, 0 );cvNamedWindow( "H-S Histogram", 1 );cvShowImage( "H-S Histogram", hist

14、_img );4.4#include <cv.h>#include <highgui.h>#include <cxcore.h>#include<stdio.h>int g_slider_position=0;CvCapture *g_capture=NULL;int i=0;int g_slider_position1=0;bool t=true;void onTrackbarSlide(int pos)cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos); i=pos;void

15、switch_callback(int position)if(position = 0)t=true;elset=false;int main()cvNamedWindow("Video",CV_WINDOW_AUTOSIZE); g_capture=cvCaptureFromFile("D:/c6opencv/SDC14186.avi");int frames=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);if(frames!=0)cvCreateTrackbar("

16、;Position","Video",&g_slider_position,frames,onTrackbarSlide);cvCreateTrackbar("Position1","Video",&g_slider_position1,1,switch_callback);else printf("read video wrong");cvSetTrackbarPos("Position","Video",1);cvSetTrackbarPos(&

17、quot;Position1","Video",0); IplImage *frame; while(frame=cvQueryFrame(g_capture)!=NULL)char c=cvWaitKey(100); if(c=27) break; cvShowImage("Video",frame);cvSetTrackbarPos("Position","Video",i); if(t)i=i+10;/10为步长 cvSetCaptureProperty(g_capture,CV_CAP_PROP_

18、POS_FRAMES,i);elsecvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,i);cvReleaseCapture(&g_capture); cvDestroyWindow("Video");return 0;4.7 #include <cv.h>#include <highgui.h>#include "math.h" /旋转时要用到三角函数int main(int argc, char* argv)IplImage *src_image=cvLoadI

19、mage("lena.jpg");assert(src_image!=NULL);/* a */IplImage *dst_image=cvCreateImage(cvGetSize(src_image), src_image->depth, src_image->nChannels);assert(dst_image!=NULL);dst_image->origin=src_image->origin; dst_image=cvCloneImage(src_image);CvPoint2D32f srcTri3;CvPoint2D32f dstTr

20、i3;/创建一个2*3的矩阵CvMat *warp_mat=cvCreateMat(2,3,CV_32FC1);srcTri0.x=0.0f;srcTri0.y=0.0f;srcTri1.x=(float)src_image->width-1;srcTri1.y=0.0f;srcTri2.x=0.0f;srcTri2.y=(float)src_image->height-1;/创建窗口cvNamedWindow("Show_Src",1);cvNamedWindow("Show_Dst",1);/显示图像 cvShowImage("

21、Show_Src",src_image);cvShowImage("Show_Dst",dst_image);while (1)char c=cvWaitKey(0);switch (c)case '1':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.90;dstTri1.y=src_image->height*0.10;dstTri2.x=src_image->width*0.10

22、;dstTri2.y=src_image->height*0.90;break;case '2':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.80;dstTri1.y=src_image->height*0.20;dstTri2.x=src_image->width*0.20;dstTri2.y=src_image->height*0.80;break;case '3':dstTri

23、0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.70;dstTri1.y=src_image->height*0.30;dstTri2.x=src_image->width*0.30;dstTri2.y=src_image->height*0.70;break;case '4':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dst

24、Tri1.x=src_image->width*0.60;dstTri1.y=src_image->height*0.40;dstTri2.x=src_image->width*0.40;dstTri2.y=src_image->height*0.60;break;case '5':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.55;dstTri1.y=src_image->height*0.4

25、5;dstTri2.x=src_image->width*0.45;dstTri2.y=src_image->height*0.55;break;case '6':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.40;dstTri1.y=src_image->height*0.60;dstTri2.x=src_image->width*0.60;dstTri2.y=src_image->heigh

26、t*0.40;break;case '7':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.30;dstTri1.y=src_image->height*0.70;dstTri2.x=src_image->width*0.70;dstTri2.y=src_image->height*0.30;case '8':dstTri0.x=src_image->width*0.0;dstTri0.

27、y=src_image->height*0.0;dstTri1.x=src_image->width*0.20;dstTri1.y=src_image->height*0.80;dstTri2.x=src_image->width*0.80;dstTri2.y=src_image->height*0.20;break;case '9':dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*0.10;dstT

28、ri1.y=src_image->height*0.90;dstTri2.x=src_image->width*0.90;dstTri2.y=src_image->height*0.10;break;default:dstTri0.x=src_image->width*0.0;dstTri0.y=src_image->height*0.0;dstTri1.x=src_image->width*1.0;dstTri1.y=src_image->height*0.0;dstTri2.x=src_image->width*0.0;dstTri2.y=s

29、rc_image->height*1.0;/功能:得到仿射矩阵。由三个不共线的点得到,这样具有唯一性cvGetAffineTransform(srcTri,dstTri,warp_mat);/实现仿射变换,第三个参数为上一个函数得到的仿射矩阵cvWarpAffine(src_image,dst_image,warp_mat);cvShowImage("Show_Dst",dst_image);if(cvWaitKey(0)=27) break;/* b */ IplImage* dst_1=0; /放大目标图像 IplImage* dst_2; /缩小目标图像 flo

30、at scaler1 = 1.5; /图像放大为原来的5倍 float scaler2 = 0.618; /图像缩小为原来的0.5倍 CvSize Size; /放大目标图像大小 CvSize Size1; /缩小目标图像大小 /设置放大的图像的尺寸 Size.width= src_image->width*scaler1; Size.height= src_image->height*scaler1; dst_1 = cvCreateImage(Size,src_image->depth,src_image->nChannels); cvResize(src_imag

31、e,dst_1,CV_INTER_LINEAR); /重置源图像大小,并放入dst1 Size1.width=src_image->width*scaler2; Size1.height=src_image->height*scaler2; dst_2 = cvCreateImage(Size1,src_image->depth,src_image->nChannels); cvResize(src_image,dst_2,CV_INTER_LINEAR); cvNamedWindow("dst_max"); cvNamedWindow("

32、dst_min"); cvShowImage("dst_max",dst_1); cvShowImage("dst_min",dst_2); cvWaitKey(); /* c */ int delta = 1; /用于控制旋转的角度 int angle = 0; /旋转的角度 int opt = 0;/ 1: 旋转加缩放 / 0: 仅仅旋转double factor; /控制旋转IplImage* dst; /目标图像存放dst = cvCloneImage (src_image); for (;) float m6; /创建2*3矩阵 Cv

33、Mat M = cvMat (2, 3, CV_32F, m); int w = src_image->width; int h = src_image->height; if (1)/ 旋转加缩放 factor = (cos (angle * CV_PI / 180.0) + 1.0) * 2; else/ 仅仅旋转 factor = 1; m0 = (float) (factor * cos (-angle * 2 * CV_PI / 180.); m1 = (float) (factor * sin (-angle * 2 * CV_PI / 180.); m3 = -m1;

34、 m4 = m0; / 将旋转中心移至图像中间 m2 = w * 0.5f; m5 = h * 0.5f; / dst(x,y) = A * src(x,y) + b cvZero (dst); /提取像素四边形,使用子像素精度 cvGetQuadrangleSubPix (src_image, dst, &M); cvNamedWindow ("dst", 1); cvShowImage ("dst", dst); if (cvWaitKey (1) = 27)/ESC break; angle = (int) (angle + delta)

35、% 360;/ for-loopreturn 0;5.1#include <cv.h>#include <highgui.h>int main(int argc, char* argv)IplImage*input=cvLoadImage("wenli.jpg");cvNamedWindow("yuantu",CV_WINDOW_AUTOSIZE);cvShowImage("yuantu",input);IplImage*out_three_img=cvCreateImage(cvGetSize(input),

36、input->depth,input->nChannels);assert(out_three_img!=NULL);IplImage*out_five_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_five_img!=NULL);IplImage*out_five_twice_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_five_twice_

37、img!=NULL);IplImage*out_nine_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_nine_img!=NULL);IplImage*out_eleven_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_eleven_img!=NULL);IplImage*out_eleven_twice_img=cvCreateImage(cvGetSi

38、ze(input),input->depth,input->nChannels);assert(out_eleven_twice_img!=NULL);cvSmooth(input,out_three_img,CV_GAUSSIAN,3,3);/3*3平滑窗口cvSmooth(input,out_five_img,CV_GAUSSIAN,5,5);/5*5窗口cvSmooth(input,out_nine_img,CV_GAUSSIAN,9,9);/9*9cvSmooth(input,out_eleven_img,CV_GAUSSIAN,11,11);/11*11cvSmooth(

39、out_five_img,out_five_twice_img);cvSmooth(out_eleven_img,out_eleven_twice_img);cvNamedWindow("3*3",CV_WINDOW_AUTOSIZE);cvNamedWindow("5*5",CV_WINDOW_AUTOSIZE);cvNamedWindow("9*9",CV_WINDOW_AUTOSIZE);cvNamedWindow("11*11",CV_WINDOW_AUTOSIZE);cvNamedWindow("

40、;两次5*5",CV_WINDOW_AUTOSIZE);cvNamedWindow("两次11*11",CV_WINDOW_AUTOSIZE);cvShowImage("3*3",out_three_img);cvShowImage("5*5",out_five_img);cvShowImage("9*9",out_nine_img);cvShowImage("11*11",out_eleven_img);cvShowImage("两次5*5",out_five_t

41、wice_img);cvShowImage("两次11*11",out_eleven_twice_img);cvWaitKey();cvReleaseImage(&input);cvReleaseImage(&out_three_img);cvReleaseImage(&out_five_img);cvReleaseImage(&out_nine_img);cvReleaseImage(&out_eleven_img);cvReleaseImage(&out_five_twice_img);cvReleaseImage(&am

42、p;out_eleven_twice_img);cvDestroyAllWindows();return 0;5.3 #include <cv.h>#include <highgui.h>int main(int argc, char* argv)IplImage*input=cvLoadImage("wenli.jpg");cvNamedWindow("yuantu",CV_WINDOW_AUTOSIZE);cvShowImage("yuantu",input);IplImage*out_a_img=cvCr

43、eateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_a_img!=NULL);IplImage*out_b_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_b_img!=NULL);IplImage*out_c_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_c_i

44、mg!=NULL);IplImage*out_d_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_d_img!=NULL);IplImage*out_e_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_e_img!=NULL);IplImage*out_f_img=cvCreateImage(cvGetSize(input),input->depth,in

45、put->nChannels);assert(out_f_img!=NULL);IplImage*out_g_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_g_img!=NULL);IplImage*out_h_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_h_img!=NULL);IplImage*out_g_h_img=cvCreateImage(

46、cvGetSize(input),input->depth,input->nChannels);assert(out_g_h_img!=NULL);IplImage*out_m_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_m_img!=NULL);IplImage*out_n_img=cvCreateImage(cvGetSize(input),input->depth,input->nChannels);assert(out_n_img!=NULL

47、);/a.param1=param2=9;param3=1,4,6会随着值的扩大,图像的模糊程度也扩大cvSmooth(input,out_a_img,CV_GAUSSIAN,9,9,1);cvSmooth(input,out_b_img,CV_GAUSSIAN,9,9,4);cvSmooth(input,out_c_img,CV_GAUSSIAN,9,9,6);/b.param1=param2=0;param3=1,4,6该情况比a情况的模糊程度更大cvSmooth(input,out_d_img,CV_GAUSSIAN,0,0,1);cvSmooth(input,out_e_img,CV_

48、GAUSSIAN,0,0,4);cvSmooth(input,out_f_img,CV_GAUSSIAN,0,0,6);/c.param1=param2=0;param3=1;param4=9该情况下图像的模糊程度较高,比较刺目,上下模糊程度较高cvSmooth(input,out_g_img,CV_GAUSSIAN,0,0,1,9);/d.param1=param2=0;param3=9;param4=1左右模糊程度较高cvSmooth(input,out_h_img,CV_GAUSSIAN,0,0,9,1);/e.先用c再用d方法平滑.此处将四个参数全部设为0,会出现错误(和E的处理结果一

49、样)cvSmooth(out_g_img,out_g_h_img,CV_GAUSSIAN,0,0,9,1);/第一次设param1=param2=0;param3=param4=9第二次param1=param2=0;param3=param4=0cvSmooth(input,out_m_img,CV_GAUSSIAN,0,0,9,9);cvSmooth(out_m_img,out_n_img,CV_GAUSSIAN);cvNamedWindow("param1=param2=9;param3=1",CV_WINDOW_AUTOSIZE);cvNamedWindow("param1=param2=9;param3=4",CV_WINDOW_AUTOSIZE);cvNamedWindow("param1=param2=9;param3=6",CV_WINDOW_AUTOSIZE);cvNamedWindow("param0=param2=0;param3=1",CV_WINDOW_AUTOSIZE);cvNamedWindow(&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论