当前位置:网站首页>Mouse operation and response
Mouse operation and response
2022-07-28 03:34:00 【When will we get ashore?】
The sample code is as follows :
Point sp(-1, -1);
Point st(-1, -1);
Mat temp;
static void on_draw(int event, int x, int y, int flags, void* userdata)
{
Mat image = *((Mat*)userdata);
if (event == EVENT_LBUTTONDOWN)
{
sp.x = x;
sp.y = y;
cout << sp << endl;
}
else if (event == EVENT_LBUTTONUP)
{
st.x = x - sp.x;
st.y = y - sp.y;
int x1 = st.x;
int y1 = st.y;
Rect rec(sp.x, sp.y, x1, y1);
if (st.x > 0 && st.y > 0)
{
rectangle(image,rec, Scalar(255, 0, 0), 2, 8, 0);
imshow(" Mouse drawing ", image);
imshow("ROI Area ", image(rec));
}
sp.x = -1;
sp.y = -1;// When the left mouse button pops up , No more rectangles
}
else if (event == EVENT_MOUSEMOVE)
{
if (sp.x > 0 && sp.y > 0)// and 394,395 Code used together
{
st.x = x - sp.x;
st.y = y - sp.y;
int x1 = st.x;
int y1 = st.y;
Rect rec(sp.x, sp.y, x1, y1);
if (st.x > 0 && st.y > 0)
{
temp.copyTo(image);// hold temp Copied to the image
rectangle(image, rec, Scalar(255, 0, 0), 2, 8, 0);
imshow(" Mouse drawing ", image);
}
}
}
}
void demo::mouse_demo(Mat& image)
{
namedWindow(" Mouse drawing ", WINDOW_AUTOSIZE);
temp = image.clone();
setMouseCallback(" Mouse drawing ", on_draw, (void*)(&image));
}
The difficulty here is setMouseCallback() function , The statement in highgui.hpp
CV_EXPORTS void setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0);
1.window_name, The name of the window
2.on_Mouse, Each time a mouse event occurs in the specified window , Pointer to the called function .
The prototype of this function is roughly in the form of void XXX(int event, int x, int y, int flags, void* param) .
among event yes EVENT_XXX Type data , Represents mouse operation events :
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 and y Is the mouse pointer in the image coordinate system ( It's not a window coordinate system ) The coordinate values in ,
flags yes EVENT_FLAG_XXX Type data , It also represents mouse operation events
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.
};param It's user-defined and passed to setMouseCallback The parameters of the function call .
3.userdata, User defined parameters passed to the callback function , Have default values 0. Pay attention to type conversion .
give the result as follows :

over!!!
边栏推荐
- Shell: one click deployment PXE
- [R language] environment specifies to delete RM function
- ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
- VMware虚拟机网络设置
- 超好看的Nteam官网PHP程序源码
- Robot development -- lead screw and guide rail
- 如何一键进行重装Win11系统
- VI command details
- "Xiaodeng" network equipment monitoring in operation and maintenance
- 整合SSM实现增删改查搜索
猜你喜欢

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

20220726 at command test of Bluetooth module hc-05 of Huicheng Technology

过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?

AIRIOT答疑第6期|如何使用二次开发引擎?

如何解决mysql深分页问题

When QML uses layout layout, a large number of < unknown file >: QML qquicklayoutattached: binding loop detected for property circular binding warnings appear

VMware virtual machine network settings

VMware虚拟机网络设置

最新版宝塔安装zip扩展,php -m 不显示的处理方法

max_pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
随机推荐
[5g NR] RRC reject analysis
鼠标操作和响应
每周推荐短视频:如何正确理解“精益”这个词?
Shell: one click deployment PXE
VMware virtual machine network settings
同时导出多个excel,并且一个excel中包含多个sheet
Unity简单实现对话功能
Digital economy has become the biggest attraction
某宝模拟登录,减少二次验证的方法
A treasure simulates login and reduces the method of secondary verification
Four methods of closing forms in C #
LabVIEW加载和使用树型控件项目中的定制符号
The digital twin smart building visualization platform realizes the integration of enterprise and public services in the park
Response to questions about the balanced beacon group of Hubei University of Arts and Sciences
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
Softek Barcode Reader 9.1.5
Contour detection based on OpenCV (3)
ASEMI整流桥GBPC3510在直流有刷电机中的妙用
Methods of SQL server backup database
一键重装win7系统详细教程