opencv 创建鼠标消息的问题

来源:互联网 发布:金蝶报表引出数据失败 编辑:程序博客网 时间:2024/06/01 20:30

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

namedWindow(tile);setMouseCallback(tile, Mouse, NULL);//创建鼠标回调函数

这两个函数一定不能写反了,或者省略第一条语句(我们知道,即使不使用nameWindow函数,我们也可以直接调用imshow函数)

一个具体的例子

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

#include <iostream>#include <opencv2/opencv.hpp>#include <sstream>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<Vec3b>(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;}


0 0
原创粉丝点击