Opencv显示图片并监听鼠标坐标

时间:2021-01-24 21:57:21
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h> using namespace cv; Mat img; void onMouse(int event, int x, int y, int flags, void* param)
{
char text[];
Mat img2, img3; img2 = img.clone(); if (event == CV_EVENT_LBUTTONDOWN)
{
Vec3b p = img2.at<Vec3b>(y,x);
sprintf(text, "R=%d, G=%d, B=%d", p[], p[], p[]);
}
else if (event == CV_EVENT_RBUTTONDOWN)
{
cvtColor(img, img3, CV_BGR2HSV);
Vec3b p = img3.at<Vec3b>(y,x);
sprintf(text, "H=%d, S=%d, V=%d", p[], p[], p[]);
}
else
sprintf(text, "x=%d, y=%d", x, y); putText(img2, text, Point(,), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(,,));
imshow("image", img2);
} int main(int argc, char** argv)
{
char *s ="D:\\my_testfiles\\7_Verify_Source_Mask.png";
img = imread(s);
if (img.empty())
return -; namedWindow("image");
setMouseCallback("image", onMouse, );
imshow("image", img);
waitKey(); return ;
}

参考:http://bsd-noobz.com/opencv-guide/45-3-using-mouse