码迷,mamicode.com
首页 > 其他好文 > 详细

opencv鼠标键盘事件处理

时间:2016-08-22 20:07:16      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:

直接上码
#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <ctype.h> using namespace cv; using namespace std; CvPoint pt1 = Point(0,0); CvPoint pt2 = Point(0,0); bool is_selecting = false; void cvMouseCallback(int mouseEvent,int x,int y,int flags,void* param) { switch(mouseEvent) { case CV_EVENT_LBUTTONDOWN: pt1 = Point(x,y); pt2 = Point(x,y); is_selecting = true; break; case CV_EVENT_MOUSEMOVE: if(is_selecting) pt2 = Point(x,y); break; case CV_EVENT_LBUTTONUP: pt2 = Point(x,y); is_selecting = false; break; } return; } int main(int argc,char* argv[]) { char* window = "img"; Mat img = imread("D:\\1.jpg",1); Mat img_show,roi; img.copyTo(img_show); namedWindow(window,CV_WINDOW_AUTOSIZE); setMouseCallback(window,cvMouseCallback); bool shift_on = false; while(true) { img.copyTo(img_show); rectangle(img_show,pt1,pt2,Scalar(255,255,255)); imshow(window,img_show); char key = cvWaitKey(10); switch(key) { //ROI平移操作 case ‘\t‘: shift_on = !shift_on; break; case ‘a‘: pt1.x--; pt2.x--; break; case ‘s‘: pt1.y++; pt2.y++; break; case ‘d‘: pt1.x++; pt2.x++; break; case ‘w‘: pt1.y--; pt2.y--; break; //ROI放大和缩小,主要是对初始设置的ROI区域的边缘进行平移操作 case ‘1‘: if(shift_on) pt1.x--; else pt2.x--; break; case ‘2‘: if(shift_on) pt2.y++; else pt1.y++; break; case ‘3‘: if(shift_on) pt2.x++; else pt1.x++; break; case ‘4‘: if(shift_on) pt1.y--; else pt2.y--; break; //回车确定最终ROI区域的截取,并将其保存下来 case ‘\r‘: roi=img(Rect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y))); imwrite("D:\\ROI.jpg",roi); break; }; if(key==27) break; } return 0; }

opencv鼠标键盘事件处理

标签:

原文地址:http://www.cnblogs.com/Erdos001/p/5796736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!