C++ opencv实现在图片上画一条线示例代码
作者:浅念念52 时间:2021-12-15 12:34:27
1 在图片上用鼠标进行操作,opencv主要用到setMouseCallback()函数。
winname
窗口名称
onMouse
鼠标事件的回调函数
userdata
传递给回调函数
还有onMouse函数
event 鼠标事件
x,y 鼠标在图片上的坐标
flags 鼠标事件标志
这里有一个容易搞混的地方
void跟void*
在函数的返回值中, void 是没有任何返回值, 而 void * 是返回任意类型的值的指针.
划线还需要用到line()函数
img 图片名称
pt1 线段起点
pt2 线段终点
color 颜色
thickness 宽度
lineType 线段类型
shift 移位点坐标中的小数位数。
接下来直接看代码
#include <iostream>
#include<opencv.hpp>
using namespace std;
using namespace cv;
Mat img;
Point p;
void on_monse(int event, int x, int y, int flags, void*)
{
if (event == 1)//1 左键点击
{
p = Point(x, y);
}
else if (event == 0 && flags == 1)//0 滑动 1左键拖曳
{
Point p1(x, y);
line(img, p, p1, Scalar(255, 0, 0), 5);
p = p1;
imshow("www", img);
}
}
int main()
{
img = imread("星空1.png", 1);
imshow("www", img);
setMouseCallback("www", on_monse);
waitKey(0);
}
效果图:
附:
来源:https://blog.csdn.net/Lightismore/article/details/123666740
标签:C++,opencv,图片画线
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java9版本特性资源自动关闭的语法增强
2023-10-30 23:35:24
![](https://img.aspxhome.com/file/2023/9/63449_0s.png)
Java如何实现http接口参数和返回值加密
2023-08-23 12:28:37
为什么不建议使用Java自定义Object作为HashMap的key
2021-09-21 06:15:05
![](https://img.aspxhome.com/file/2023/6/72616_0s.webp)
Springboot 多租户SaaS搭建方案
2022-06-04 00:23:32
java实现简单扫雷游戏
2022-09-15 13:48:11
springboot与vue详解实现短信发送流程
2023-05-24 14:55:29
![](https://img.aspxhome.com/file/2023/5/97935_0s.png)
C# 利用PdfSharp生成Pdf文件的示例
2022-01-18 17:31:30
![](https://img.aspxhome.com/file/2023/0/119530_0s.png)
Android WebView的使用方法总结
2022-08-23 22:14:02
Java多线程之线程同步
2023-01-24 18:39:00
![](https://img.aspxhome.com/file/2023/8/101988_0s.png)
Java ArrayList与LinkedList使用方法详解
2023-08-12 08:23:46
Java基础知识之StringWriter流的使用
2023-01-19 00:47:52
spring boot 常见http请求url参数获取方法
2023-01-26 13:59:43
Spring Boot集成ElasticSearch实现搜索引擎的示例
2021-06-02 05:06:16
![](https://img.aspxhome.com/file/2023/3/63793_0s.png)
C#对JSON与对象的序列化与反序列化
2022-09-07 12:05:08
![](https://img.aspxhome.com/file/2023/7/71067_0s.png)
教你如何使用Java8实现菜单树形数据
2022-09-11 12:29:22
![](https://img.aspxhome.com/file/2023/3/67163_0s.png)
java扩展Hibernate注解支持java8新时间类型
2021-09-12 07:43:38
Java开发利器之Guava Cache的使用教程
2022-03-20 19:22:02
HashSet如何保证元素不重复(面试必问)
2023-04-12 02:16:24
![](https://img.aspxhome.com/file/2023/2/119072_0s.png)
浅析C# 状态机Stateless
2022-03-08 16:39:07
Struts2学习笔记(9)-Result配置全局结果集
2022-04-09 11:33:10