ubuntu22.04@laptop OpenCV Get Started: 012_mouse_and_trackbar

ubuntu22.04@laptop OpenCV Get Started: 012_mouse_and_trackbar

  • 1. 源由
  • 2. mouse/trackbar应用Demo
    • 2.1 C++应用Demo
    • 2.2 Python应用Demo
  • 3. 鼠标位置跟踪注释
    • 3.1 注册回调函数
    • 3.2 回调操作
    • 3.3 效果
  • 4. 使用轨迹栏调整图像大小
    • 4.1 初始化轨迹栏&注册回调函数
    • 4.2 回调操作
    • 4.3 效果
  • 4. 总结
  • 5. 参考资料
  • 6. 补充

1. 源由

鼠标指针和轨迹条是图形用户界面(GUI)中的关键组件。

如果没有这些关键交互组件,就无法真正考虑与GUI交互。

因此,结合演示代码了解OpenCV中鼠标和轨迹条的内置功能,对于程序交互来说至关重要。

2. mouse/trackbar应用Demo

012_mouse_and_trackbar是OpenCV通过鼠标指针和轨迹条与用户交互的示例。

2.1 C++应用Demo

C++应用Demo工程结构:

012_mouse_and_trackbar/CPP$ tree .
.
├── Mouse
│   ├── CMakeLists.txt
│   └── mouse.cpp
└── Trackbar├── CMakeLists.txt└── trackbar.cpp2 directories, 4 files

确认OpenCV安装路径:

$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

C++应用Demo工程编译执行:

cd Mouse
mkdir build
cd build
cmake ..
cmake --build . --config Release
cd ..
./build/mouse
cd Trackbar
mkdir build
cd build
cmake ..
cmake --build . --config Release
cd ..
./build/trackbar

2.2 Python应用Demo

Python应用Demo工程结构:

012_mouse_and_trackbar/Python$ tree .
.
├── requirements.txt
├── mouse.py
└── trackbar.py0 directories, 3 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python mouse.py
$ python trackbar.py

3. 鼠标位置跟踪注释

3.1 注册回调函数

OpenCV提供了鼠标事件检测功能,用于检测各种鼠标操作。

代码上采用注册回调函数方式实现:

C++:

// highgui function called when mouse events occur
setMouseCallback("Window", drawRectangle);

Python:

# highgui function called when mouse events occur
cv2.setMouseCallback("Window", drawRectangle)

3.2 回调操作

当有鼠标操作时:

  • EVENT_LBUTTONDOWN:记录左上角坐标
  • EVENT_LBUTTONUP:记录右下角坐标,并更新图像

实现对左上角和右下角的框选,矩形框标注选择范围。

C++:

// Points to store the center of the circle and a point on the circumference
Point top_left_corner, bottom_right_corner;
// image image
Mat image;// function which will be called on mouse input
void drawRectangle(int action, int x, int y, int flags, void *userdata)
{// Mark the center when left mouse button is pressedif( action == EVENT_LBUTTONDOWN ){top_left_corner = Point(x,y);}// When left mouse button is releasedelse if( action == EVENT_LBUTTONUP){bottom_right_corner = Point(x,y);// Draw rectanglerectangle(image, top_left_corner, bottom_right_corner, Scalar(0,255,0), 2, 8 );// Display imageimshow("Window", image);}}

Python:

# Lists to store the points
top_left_corner=[]
bottom_right_corner=[]# Define drawRectangle function
def drawRectangle(action, x, y, flags, *userdata):# Referencing global variables global top_left_corner, bottom_right_corner# Action to be taken when left mouse button is pressedif action == cv2.EVENT_LBUTTONDOWN:top_left_corner = [(x,y)]# Action to be taken when left mouse button is releasedelif action == cv2.EVENT_LBUTTONUP:bottom_right_corner = [(x,y)]    # Draw the rectanglecv2.rectangle(image, top_left_corner[0], bottom_right_corner[0], (0,255,0),2, 8)cv2.imshow("Window",image)

3.3 效果

在这里插入图片描述

4. 使用轨迹栏调整图像大小

4.1 初始化轨迹栏&注册回调函数

创建轨迹栏对象时,代码上采用注册回调函数方式实现:

C++:

int maxScaleUp = 100;
int scaleFactor = 1;string windowName = "Resize Image";
string trackbarValue = "Scale";// Create Trackbars and associate a callback function
createTrackbar(trackbarValue, windowName, &scaleFactor, maxScaleUp, scaleImage);

Python:

maxScaleUp = 100
scaleFactor = 1
windowName = "Resize Image"
trackbarValue = "Scale"# Create trackbar
cv2.createTrackbar(trackbarValue, windowName, scaleFactor, maxScaleUp, scaleImage)

4.2 回调操作

当有拖动轨迹栏滑块时,调用回调函数。根据滑块位置,对图像进行比例缩放。

C++:

// Callback functions
void scaleImage(int, void*)
{// Read the imageMat image = imread("../../Input/sample.jpg");// Get the Scale factor from the trackbardouble scaleFactorDouble = 1 + scaleFactor/100.0;// Set the factor to 1 if becomes 0if (scaleFactorDouble == 0){scaleFactorDouble = 1;}Mat scaledImage;// Resize the imageresize(image, scaledImage, Size(), scaleFactorDouble, scaleFactorDouble, INTER_LINEAR);// Display the imageimshow(windowName, scaledImage);
}

Python:

# Callback functions
def scaleImage(*args):# Get the scale factor from the trackbar scaleFactor = 1+ args[0]/100.0# Resize the imagescaledImage = cv2.resize(image, None, fx=scaleFactor, fy = scaleFactor, interpolation = cv2.INTER_LINEAR)cv2.imshow(windowName, scaledImage)

4.3 效果

通过轨迹栏的拖动,实现图像的放大缩小。

在这里插入图片描述在这里插入图片描述

4. 总结

本文通过设置setMouseCallbackcreateTrackbar注册鼠标操作回调函数和轨迹栏空间回调函数,实现对应的OpenCV图像操作。

  • setMouseCallback(winname, onMouse, userdata)
  • winname Name of the window.
  • onMouse Callback function for mouse events. See OpenCV samples on how to specify and use the callback.
  • userdata The optional parameter passed to the callback.
  • createTrackbar( trackbarName, windowName, value, count, onChange)
  • trackbarname Name of the created trackbar.
  • winname Name of the window that will be used as a parent of the created trackbar.
  • value Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
  • count Maximal position of the slider. The minimal position is always 0.
  • onChange Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
  • userdata User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

6. 补充

学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。

有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/472640.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第七篇【传奇开心果系列】Python微项目技术点案例示例:数据可视化界面图形化经典案例

传奇开心果微博系列 系列微博目录Python微项目技术点案例示例系列 微博目录一、微项目开发背景和项目目标:二、雏形示例代码三、扩展思路介绍四、数据输入示例代码五、数据分析示例代码六、排名统计示例代码七、数据导入导出示例代码八、主题定制示例代码九、数据过…

Matplotlib plt.scatter:从入门到精通,只需一篇文章!

Matplotlib plt.scatter:从入门到精通,只需一篇文章!🚀 利用Matplotlib进行数据可视化示例 🌵文章目录🌵 一、plt.scatter入门:轻松迈出第一步 👣二、进阶探索:plt.scatt…

Vue的一些基础设置

1.浏览器控制台显示Vue 设置找到扩展,搜索Vue 下载这个 然后 点击扩展按钮 点击详细信息 选择这个,然后重启一下就好了 ——————————————————————————————————————————— 2.优化工程结构 src的components里要…

知识图谱:py2neo将csv文件导入neo4j

文章目录 安装py2neo创建节点-连线关系图导入csv文件删除重复节点并连接边 安装py2neo 安装python中的neo4j操作库:pip install py2neo 安装py2neo后我们可以使用其中的函数对neo4j进行操作。 图数据库Neo4j中最重要的就是结点和边(关系)&a…

sql注入猜测字段数的基本方法

示例表; 写sql语句查询结果排序有两种写法, 一是 select * from 表名 where 条件 order by 字段名; 一是 select * from 表名 where 条件 order by 数字; 数字是查询结果中的第几个字段; 下图根据 name 排序查询一次…

Flutter 动画(显式动画、隐式动画、Hero动画、页面转场动画、交错动画)

前言 当前案例 Flutter SDK版本:3.13.2 显式动画 Tween({this.begin,this.end}) 两个构造参数,分别是 开始值 和 结束值,根据这两个值,提供了控制动画的方法,以下是常用的; controller.forward() : 向前…

解线性方程组(二)——Jacobi迭代法求解(C++)

迭代法 相比于直接法求解,迭代法使用多次迭代来逐渐逼近解,其精度比不上直接法,但是其速度会比直接法快很多,计算精度可控,特别适用于求解系数矩阵为大型稀疏矩阵的方程组。 Jacobi迭代法 假设有方程组如下&#xf…

JDBC查询操作

目录 加载驱动获取连接创建会话发送SQL处理结果关闭资源测试 加载驱动 // 加载驱动Class.forName("com.mysql.cj.jdbc.Driver");获取连接 // 获取连接String url "jdbc:mysql://127.0.0.1:3306/book";String username "root" …

第14集《佛说四十二章经》

好!请大家打开讲义第十九面,第三十九章、教诲无差。 佛言:学佛道者,佛所言说,皆应信顺。譬如食蜜,中边皆甜。吾经亦尔。 大智慧的佛陀说,佛弟子们在修学过程中,对佛陀所说的一切佛…

cloudflare更换第三方证书

由于我的网站一直放在腾讯云上,域名和证书也是在腾讯云上解析的,后来将DNS迁移到了cloudflare,最近SSL证书到期了遇到一些麻烦记录一下。 由于服务器上原来是装的腾讯云发的免费证书,所以这次我也是按部就班的先去申请腾讯云的证…

政安晨:【示例演绎】【Python】【Numpy数据处理】快速入门(四)—— 函数方法

准备工作 这是Numpy数据处理的示例演绎系列文章的第四篇,我的前三篇文章为: 政安晨:【示例演绎】【Python】【Numpy数据处理】快速入门(一)https://blog.csdn.net/snowdenkeke/article/details/136125773政安晨&#…

【51单片机】AD模数转换DA数模转换(江科大)

1.AD/DA介绍 AD(Analog to Digital):模拟-数字转换,将模拟信号转换为计算机可操作的数字信号 DA(Digital to Analog):数字-模拟转换,将计算机输出的数字信号转换为模拟信号 AD/DA转换打开了计算机与模拟信号的大门,极大的提高了计算机系统的应用范围,也为模拟信号数字化处理…