• opencv鼠标键盘事件处理


    直接上码
    #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 ' ': 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 ' ': 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; }
  • 相关阅读:
    【转】关于Vue打包的一个要注意的地方
    ES6 简介
    java ee / JVM Tuning
    network / ifconfig
    My live Read / Oray.com / huashengke / peanut shell / dnspod.cn
    network / VXLAN spine和bord-leaf
    hd + TP-Link SG2008MP / switch
    elasticSearch+ik_smart 支持 符号检索
    使用java+http+Range头 实现视频分段下载
    springBoot+elasticSearch 使用function_score自定义评分
  • 原文地址:https://www.cnblogs.com/Erdos001/p/5796736.html
Copyright © 2020-2023  润新知