opencv鼠标画矩形
2014-08-14 15:01
176 查看
程序:
核心函数:
回调函数,注册回调函数cvSetMouseCallback
代码:
本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539942
核心函数:
回调函数,注册回调函数cvSetMouseCallback
代码:
#include "cv.h" #include "cxcore.h" #include "highgui.h" #include <iostream> CvRect rect=cvRect(-1,-1,0,0); CvPoint pt1=cvPoint(0,0); CvPoint pt2=cvPoint(0,0); bool draw=false; void DrawRect(IplImage* src,CvPoint pt1,CvPoint pt2) { cvRectangle(src,pt1,pt2,cvScalar(0,0,255,NULL)); } void my_mouse_callback(int event,int x,int y,int flags,void* param) //回调函数 { //static bool draw=false; //static int rectx=-1; //static int recty=-1; //static int rectwidth=0; //static int rectheight=0; switch(event) { case CV_EVENT_LBUTTONDOWN: { std::cout<<"CV_EVENT_LBUTTONDOWN"<<std::endl; draw=true; //rect.x=x; //rect.y=y; pt1.x=x; pt1.y=y; } break; case CV_EVENT_MOUSEMOVE: { pt2.x=x; pt2.y=y; //std::cout<<"CV_EVENT_MOUSEMOVE"<<std::endl; //if(draw) //{ ////std::cout<<"x:"<<x<<std::endl; ////std::cout<<"y:"<<y<<std::endl; ////rect.width=x-rect.x; ////rect.height=y-rect.y; //} } break; case CV_EVENT_LBUTTONUP: { std::cout<<"CV_EVENT_LBUTTONUP"<<std::endl; draw=false; pt2.x=x; pt2.y=y; //if(rect.width<0) //{ //rect.x=rect.x+rect.width; //rect.width=rect.width*(-1); //} //if(rect.height<0) //{ //rect.y=rect.y+rect.height; //rect.height=rect.height*(-1); //} //rect.x=rectx; //rect.y=recty; //rect.width=rectwidth; //rect.height=rectheight; //std::cout<<"rectx: "<<rectx<<std::endl; //std::cout<<"recty: "<<recty<<std::endl; //std::cout<<"rectwidth: "<<rectwidth<<std::endl; //std::cout<<"rectheight: "<<rectheight<<std::endl; DrawRect((IplImage*)param,pt1,pt2); } break; } } int MouseRectangle(int argc,char** argv) //注册回调函数 { IplImage* src=cvLoadImage("e:\\picture\\4.jpg"); IplImage* temp=cvCloneImage(src); cvNamedWindow("src",0); //注意这里第二个参数最好为0,要不大图可能画的矩形坐标不准 //cvShowImage("src",src); cvSetMouseCallback("src",my_mouse_callback,(void*)src); while(1) { cvCopyImage(src,temp); if(draw) { DrawRect(temp,pt1,pt2); //让画的矩形具有连续性 } cvShowImage("src",temp); if(cvWaitKey(15)==27) {break;} } //cvWaitKey(0); cvDestroyWindow("src"); cvReleaseImage(&src); return 0; }
本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539942
相关文章推荐
- opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像
- Opencv图像识别从零到精通(13)----点线圆矩形与鼠标事件
- opencv-- 使用鼠标绘制矩形并截取和保存矩形区域图像
- OpenCV鼠标画图例程,鼠标绘制矩形
- OpenCV学习笔记(18):在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- OpenCV利用鼠标进行矩形ROI选取
- 利用鼠标绘制矩形和截取图像的矩形区域并保存(OpenCV2.4.5)
- OpenCV鼠标画图例程,鼠标绘制矩形
- opencv图像处理之鼠标事件(矩形roi绘图)
- OpenCV鼠标画图例程,鼠标绘制矩形
- OpenCV鼠标画图例程,鼠标绘制矩形
- 基础学习笔记之opencv(5):实现鼠标选定矩形框
- opencv鼠标操作及GUI矩形绘画
- opencv2-基本鼠标操作之画矩形
- opencv3 鼠标响应函数画矩形
- 基于opencv用鼠标在窗口中画矩形和线段的程序
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- OpenCV截取图像的任意形状区域,规则的图形(圆、椭圆、矩形),不规则鼠标自己选择
- opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像