用鼠标在窗口中画方形
2015-10-11 21:25
393 查看
//用鼠标在窗口中画方形 //作者:sandy //时间:2015-10-7 //user can draw boxes on the screen #include <cv.h> #include <highgui.h> using namespace std; CvRect box; bool drawing_box=false; //定义一个回调函数 //define our callback which we will install for mouse event // void my_mouse_callback(int event ,int x,int y,int flags,void* param); //实现把方形画在一幅画上的小程序 //A little subroutine to draw a box into an image // void draw_box(IplImage* img ,CvRect rect); int main(int argc,char* argv[]){ box=cvRect(-1,-1,0,0);//(x,y,width,height) IplImage* image=cvCreateImage(cvSize(400,400),IPL_DEPTH_8U,3);//创建一个图像(画板) cvZero(image);//将数组中的所有通道的元素的值都设置为0. IplImage* temp=cvCloneImage(image);//image克隆到temp cvNamedWindow("mouse_rect_window");//定义一个视窗 //在opencv中注册回调函数,传入图像参数,以便特定窗口被触发鼠标事件后,opencv可以正确调用 // cvSetMouseCallback("mouse_rect_window",my_mouse_callback,(void*) image);//触发窗口为:mouse_rect_window,触发调用函数为:my_mouse_callback,传给触发调用函数的void * param值为(void*) image //The main program loop.here we copy the working image to the 'temp' image, //and if the user is drawing , then put the currently contemplated box onto the //temp image.Display the temp image,and wait 15ms for a keystroke,then repeat... // while(1){ cvCopyImage(image,temp);//把image复制到temp中 if(drawing_box) draw_box(temp,box); cvShowImage("mouse_rect_window",temp); if(cvWaitKey(15)==27) break; } cvReleaseImage(&image); cvReleaseImage(&temp); cvDestroyWindow("mouse_rect_window"); } //鼠标响应事件 void my_mouse_callback(int event ,int x,int y,int flags,void* param){ IplImage* image=(IplImage*)param; switch(event){ case CV_EVENT_MOUSEMOVE:{ if(drawing_box){ box.width=x-box.x; box.height =y-box.y; //cout<<x<<','<<y<<endl; //cout<<box.x<<','<<box.y<<endl; } } break; case CV_EVENT_LBUTTONDOWN:{ drawing_box=true; box=cvRect(x,y,0,0);//起点坐标,宽,高 //cout<<x<<','<<y<<endl; //cout<<box.x<<','<<box.y<<endl; } break; case CV_EVENT_LBUTTONUP:{ drawing_box=false; if(box.width<0){ box.x +=box.width ; box.width *=-1; } if(box.height<0){ box.y +=box.height ; box.height *=-1; } draw_box(image,box); } break; } } //实现把方形画在一幅画上的小程序 //A little subroutine to draw a box into an image // void draw_box(IplImage* img ,CvRect rect){ cvRectangle( img, cvPoint(box.x ,box.y), cvPoint(box.x +box.width,box.y +box.height), cvScalar(0, 120, 255)//画线的颜色 ); }
相关文章推荐
- 关于最近找工作的反思
- 《编写有效用例》读书笔记1
- MyBatis配置一对一
- php启用zend guard loader扩展问题
- HDU4460(最短路)
- java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10047 nor current process
- 【Python之旅】第六篇(四):Python多线程锁
- 【Python之旅】第六篇(四):Python多线程锁
- c/c++ 引用计数
- Scala——总计架构目录
- The Importance of Algorithms
- 用widthStep的方法来增加某范围的像素----与imageROI对比
- onItemLongClick事件的监听
- 正则表达式(转载)
- LeetCode OJ:Multiply Strings (字符串乘法)
- 程序化交易生态图谱2015年7月版
- 连载《一个程序猿的生命周期》- 37、《从0到1》中提到的4点创业信条! 【含】李彦宏的《开讲啦》
- 列出display的值,并说明它们的作用
- 文件I/O实践(1) --基础API
- word2013中表格文档最后空白页删不掉的问题