opencv 鼠标取图片中的像素坐标和像素值

#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
bool flag = false;
int xvalue = 0;
int yvalue = 0;
Mat image, image1, image2;
void mousecallback(int event, int x, int y, int flags, void* userdata);
int main(int argc, char* argv[])
{
	namedWindow("imageshow", 0);
	Mat image = imread("D:\\001.jpg", 1);
	if (!image.data)
	{
		cout << "the image is error" << endl;
		return 0;
	}
	imshow("imageshow", image);
	image.copyTo(image1);
	cv::setMouseCallback("imageshow", mousecallback, 0);
	waitKey(0);
	return 0;
}
void mousecallback(int event, int x, int y, int flags, void* userdata)
{
	image1.copyTo(image2);
	switch (event)
	{
	case EVENT_LBUTTONDOWN:
	{
		flag = true;
	}
	break;
	case EVENT_LBUTTONUP:
	{
		if (flag)
		{
			xvalue = x;
			yvalue = y;
			flag = 0;
			int b = image1.at<Vec3b>(yvalue, xvalue)[0];
			int g = image1.at<Vec3b>(yvalue, xvalue)[1];
			int r = image1.at<Vec3b>(yvalue, xvalue)[2];
			
			cout <<"X: "<<xvalue<<"Y: "<<yvalue<< " B:" << b << ' ' << "G:" << g << ' ' << "R:" << r << endl;
		}
	}
	break;
	}
}
发布了332 篇原创文章 · 获赞 214 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/qq_33835307/article/details/102933724