opencv 中通过鼠标和键盘操作实现图像的平移
2012-09-12 19:19
776 查看
通过鼠标和键盘的操作来实现图像的平移,操作方法:用鼠标的话直接拖动图像即可,用键盘的话,使用wsad四个键
来控制图像平移方向,w上,s下,a左,d右。
来控制图像平移方向,w上,s下,a左,d右。
#include <cv.h> #include <highgui.h> IplImage* org = 0; IplImage* tmp = 0; IplImage* dst = 0; CvRect rect; //保存图像显示区域 void moveImage()//实现移动 { cvCopy(tmp,dst); cvSetImageROI(dst,rect); cvCopy(org,dst); cvResetImageROI(dst); cvShowImage("image",dst); } void on_mouse( int event, int x, int y, int flags, void* ustc) { static CvPoint p={-1,-1}; static int xrect; static int yrect; if(event==CV_EVENT_LBUTTONDOWN) { p=cvPoint(x,y); xrect=rect.x; yrect=rect.y; } else if(event==CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { int dx=x-p.x; int dy=y-p.y; if(x>=rect.x && x<=rect.x+rect.width-1 && y>=rect.y && y<=rect.y+rect.height-1) { rect=cvRect(xrect+dx,yrect+dy,org->width,org->height); if(rect.x<0) { rect.x=0; } if(rect.y<0) { rect.y=0; } if(rect.x > dst->width - org->width-1) { rect.x=dst->width-org->width-1; } if(rect.y > dst->height - org->height-1) { rect.y=dst->height-org->height-1; } moveImage(); } } } int main() { org=cvLoadImage("test.bmp",1); rect=cvRect(0,0,org->width,org->height); tmp=cvCreateImage(cvSize(org->width*2,org->height*1.5),org->depth,org->nChannels); cvSet(tmp,CV_RGB(192,192,192)); dst=cvCreateImage(cvSize(org->width*2,org->height*1.5),org->depth,org->nChannels); cvSet(dst,CV_RGB(192,192,192)); cvSetImageROI(dst,cvRect(0,0,org->width,org->height)); cvCopy(org,dst); cvResetImageROI(dst); cvNamedWindow("image",1); cvShowImage("image",dst); cvSetMouseCallback( "image", on_mouse, 0 ); cvWaitKey(0); while (1) { int key = cvWaitKey (10); switch (key) { case 'w': rect=cvRect(rect.x,rect.y-20,org->width,org->height); if (rect.y <= 0) rect.y = 0; moveImage(); break; case 's': rect=cvRect(rect.x,rect.y+20,org->width,org->height); if (rect.y >= org->height*0.5) rect.y = org->height*0.5; moveImage(); break; case 'a': rect=cvRect(rect.x-20,rect.y,org->width,org->height); if (rect.x <= 0) rect.x = 0; moveImage(); break; case 'd': rect=cvRect(rect.x+20,rect.y,org->width,org->height); if (rect.x >= org->width) rect.x = org->width; moveImage(); break; } } cvDestroyAllWindows(); cvReleaseImage(&org); cvReleaseImage(&tmp); cvReleaseImage(&dst); return 0; }
相关文章推荐
- qtp录制鼠标右键单击事件要通过模拟键盘操作来实现
- opencv通过鼠标获取训练图像
- HTML--如何实现通过鼠标事件拖动页面图像(代码)
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- Control Study -> 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- C#通过Windows API 获取键盘和鼠标操作的时间的类
- HTML--如何实现通过鼠标事件拖动页面图像(代码)
- 鼠标右键弹出菜单 上传图片自定义控件 弹出菜单(PopUp 控件) 2 实现数据库插入操作 文件名通过参数传递
- openCV实现鼠标响应裁剪图像
- Opencv中使用Surf特征实现图像配准及对透视变换矩阵H的平移修正
- qt+opencv实现拍照,打开视频,图像处理操作
- HTML--如何实现通过鼠标事件拖动页面图像(代码)
- C语言实现bmp图像的平移操作
- 用Python+Selenium模拟键盘和鼠标操作,实现自动签到
- 图像卷积操作的手动实现(基于opencv的C++编译环境)
- 通过全局鼠标键盘事件实现应用程序的自动锁定
- 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- HTML--如何实现通过鼠标事件拖动页面图像(代码)
- 通过synsergy实现mac和windows共享键盘和鼠标
- HTML--如何实现通过鼠标事件拖动页面图像(代码)