当前位置:网站首页>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!!!
边栏推荐
- Hotel VR panoramic display shooting provides more opportunities for cooperation and negotiation
- 动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
- 如何解决mysql深分页问题
- redis网络模型解析
- STM32 RT-Thread虚拟文件系统挂载操作
- Uniapp - make phone calls and send text messages
- 如何让外网访问内网IP(esp8266网页使用)
- Detailed tutorial of one click reinstallation of win7 system
- IronOCR for .NET 2022.8
- Redis5种数据结构解析
猜你喜欢

20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration

关于湖北文理学院平衡信标组的疑问回应

Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?

Softek Barcode Reader 9.1.5

AI首席架构师12-AICA-百度OCR垂类规模化落地实践

Redis basic operation

IronOCR for .NET 2022.8
![[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial](/img/7d/c372dba73531f4574ca3d379668b13.jpg)
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial

什么是Tor?Tor浏览器更新有什么用?

收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
随机推荐
Redis 5 kinds of data structure analysis
AI首席架构师12-AICA-百度OCR垂类规模化落地实践
【5G NR】RRC Reject解析
【论文笔记】基于深度学习的移动机器人自主导航实验平台
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
When a dialog box pops up, the following form is not available
什么是虚函数?
颜色的识别方法和探索 基于matlab
⽇志分析⼯具(Splunk)
20条心灵鸡汤唯美句子,句句温情暖心!
玩一玩WolframAlpha计算知识引擎
xctf攻防世界 Web高手进阶区 unserialize3
每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
GNU General Public License v2.0 GNU General Public License
bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
Uniapp - make phone calls and send text messages
Embedded database -- SQLite
golang 获取循环嵌套结构的tag
695. Maximum area of the island