opencv 创建鼠标消息的问题
发布日期:2021-11-07 18:53:23 浏览次数:1 分类:技术文章

本文共 1126 字,大约阅读时间需要 3 分钟。

1.opencv 中可以创建鼠标消息,但是有一个需要注意的问题

namedWindow(tile);setMouseCallback(tile, Mouse, NULL);//创建鼠标回调函数
这两个函数一定不能写反了,或者省略第一条语句(我们知道,即使不使用nameWindow函数,我们也可以直接调用imshow函数)

一个具体的例子

功能:使用opencv,在鼠标左键按下的时候,显示当前像素的灰度值(B,G,R)

#include 
#include
#include
using namespace std;using namespace cv;Mat g_img;//全局变量,以便在mouse函数中进行使用string tile("窗口");//全局的窗口名字void Mouse(int event, int x, int y, int flag, void *){ static Point Cur;//记录当前影像的灰度值 Mat temp = g_img.clone();//在临时变量中进行绘图操作 char text[100];//用于在图像中显示灰度值 memset(text, 0, sizeof(char)* 100); if (event==EVENT_LBUTTONDOWN) { Cur.x = x; Cur.y = y; Vec3b color = temp.at
(y, x);//行。列 sprintf_s(text, "(%d,%d,%d)", color[0], color[1], color[2]); text[strlen(text)] = '\0'; putText(temp, text, Cur, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0)); imshow(tile, temp); } return;}int main(){ string filename = "C:/Users/Administrator/Desktop/标准测试图片/dota/big32001.jpg"; g_img = imread(filename, IMREAD_COLOR); if (g_img.empty()) { return -1; } namedWindow(tile); setMouseCallback(tile, Mouse, NULL);//创建鼠标回调函数 imshow(tile, g_img); waitKey(0); return 0;}

转载地址:https://blog.csdn.net/zhanggusheng/article/details/70188145 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:opencv 绘制矩形,提取矩形区域的直方图
下一篇:opencv PCA算法

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月13日 15时15分55秒