OpenCV获取鼠标左键点击位置图像的像素值
时间:2021-01-15 11:13:03|栏目:C代码|点击: 次
本文实现功能:利用opencv获取鼠标左键点击位置图像的像素值(RGB像)
vs2015+opencv3.1
#include<opencv2\opencv.hpp> #include<iostream> using namespace std; using namespace cv; void on_mouse(int EVENT, int x, int y, int flags, void* userdata); void main() { namedWindow("【display】"); Mat src; src = imread("F:\\lena_rgb.jpg"); //cvtColor(src, src, COLOR_RGB2GRAY); setMouseCallback("【display】", on_mouse,&src); //以40ms刷新显示 while (1) { imshow("【display】", src); waitKey(40); } } void on_mouse(int EVENT, int x, int y, int flags, void* userdata) { Mat hh; hh = *(Mat*)userdata; Point p(x, y); switch (EVENT) { case EVENT_LBUTTONDOWN: { printf("b=%d\t", hh.at<Vec3b>(p)[0]); printf("g=%d\t", hh.at<Vec3b>(p)[1]); printf("r=%d\n", hh.at<Vec3b>(p)[2]); circle(hh, p, 2, Scalar(255),3); } break; } }