清新县城乡规划建设局网站,人力资源公司注册条件,wordpress纯静态,建设银行网站转账一、知识点 1、void setMouseCallback(const String winname, MouseCallback onMouse, void * userdata 0); (1)、为指定窗口设置鼠标事件回调函数。 (2)、参数说明: winname: 窗口名称。 onMouse: 自定义的鼠标事件回调函数#xff0c;函数类型见2。 … 一、知识点 1、void setMouseCallback(const String winname, MouseCallback onMouse, void * userdata 0); (1)、为指定窗口设置鼠标事件回调函数。 (2)、参数说明: winname: 窗口名称。 onMouse: 自定义的鼠标事件回调函数函数类型见2。 userdata: 传递给鼠标事件回调函数的可选参数。 (3)、一旦鼠标事件发生就会自动调用绑定的onMouse函数。 2、typedef void (*MouseCallback)(int event, int x, int y, int flags, void * userdata); (1)、鼠标事件回调函数的类型。 (2)、参数说明: event: MouseEventTypes宏值。 enum MouseEventTypes { EVENT_MOUSEMOVE 0, //! indicates that the mouse pointer has moved over the window. EVENT_LBUTTONDOWN 1, //! indicates that the left mouse button is pressed. EVENT_RBUTTONDOWN 2, //! indicates that the right mouse button is pressed. EVENT_MBUTTONDOWN 3, //! indicates that the middle mouse button is pressed. EVENT_LBUTTONUP 4, //! indicates that left mouse button is released. EVENT_RBUTTONUP 5, //! indicates that right mouse button is released. EVENT_MBUTTONUP 6, //! indicates that middle mouse button is released. EVENT_LBUTTONDBLCLK 7, //! indicates that left mouse button is double clicked. EVENT_RBUTTONDBLCLK 8, //! indicates that right mouse button is double clicked. EVENT_MBUTTONDBLCLK 9, //! indicates that middle mouse button is double clicked. EVENT_MOUSEWHEEL 10,//! positive and negative values mean forward and backward scrolling, respectively. EVENT_MOUSEHWHEEL 11 //! positive and negative values mean right and left scrolling, respectively. }; x: 鼠标事件的x坐标。 y: 鼠标事件的y坐标。 flags: MouseEventFlags宏值。 enum MouseEventFlags { EVENT_FLAG_LBUTTON 1, //! indicates that the left mouse button is down. EVENT_FLAG_RBUTTON 2, //! indicates that the right mouse button is down. EVENT_FLAG_MBUTTON 4, //! indicates that the middle mouse button is down. EVENT_FLAG_CTRLKEY 8, //! indicates that CTRL Key is pressed. EVENT_FLAG_SHIFTKEY 16,//! indicates that SHIFT Key is pressed. EVENT_FLAG_ALTKEY 32 //! indicates that ALT Key is pressed. }; userdata: setMouseCallback()传递的可选参数。 3、Mat Mat::operator()( const Rect roi ) const (1)、从图像中提取感兴趣区域(ROI)。 (2)、提取的方式不是复制而是通过创建指向原图数据的新矩阵指针对新矩阵的操作会直接影响原图像。 二、示例代码
#include iostream
#include opencv2/opencv.hppcv::Point sp(-1, -1);
cv::Point ep(-1, -1);
cv::Mat tempSrc;//鼠标事件处理函数
void onMouseDraw(int event, int x, int y, int flags, void * userdata)
{cv::Mat image *((cv::Mat*)(userdata));if (event cv::EVENT_LBUTTONDOWN){sp.x x;sp.y y;}else if (event cv::EVENT_MOUSEMOVE){if (sp.x 0 sp.y 0){ep.x x;ep.y y;int dx ep.x - sp.x;int dy ep.y - sp.y;if (dx 0 dy 0){//清空图像tempSrc.copyTo(image);cv::Rect box(sp.x, sp.y, dx, dy);cv::rectangle(image, box, cv::Scalar(0, 0, 255), 2, 8, 0);cv::imshow(鼠标绘制ROI, image);cv::imshow(ROI区域, image(box));}}}else if (event cv::EVENT_LBUTTONUP){//标志着鼠标左键已松开不会影响鼠标移动sp.x -1;sp.y -1;}
}int main()
{//本地读取原图cv::Mat src cv::imread(../images/9.png);if (src.empty()){std::cout load src image error... std::endl;return -1;}//全局备份一张原图达到清空ROI目的tempSrc src.clone();//创建一个窗口, 名为鼠标绘制ROIcv::namedWindow(鼠标绘制ROI, cv::WINDOW_AUTOSIZE);//设置鼠标事件回调函数cv::setMouseCallback(鼠标绘制ROI, onMouseDraw, src);cv::imshow(鼠标绘制ROI, src);cv::waitKey(0);return 0;
} 演示结果: