数字图像处理 (c++ opencv):通过鼠标点击操作获取图像的像素坐标和像素值

code:

#include 
#include 
using namespace cv;
using namespace std;

void onMouse(int event,int x,int y,int flags,void* param)
{
	Mat* im = reinterpret_cast(param);
	switch(event)
	{
	case EVENT_LBUTTONDOWN:
		//左键按下显示像素值
		if (static_cast(im->channels())==1)
		{
			//若为灰度图,显示鼠标点击的坐标以及灰度值
			cout<<"at("<(im->at(x,y))<(im->channels() == 3))
		{
			//若图像为彩色图像,则显示鼠标点击坐标以及对应的B, G, R值
			cout << "at (" << x << ", " << y << ")"
				<< "  B value is: " << static_cast(im->at(x, y)[0]) 
				<< "  G value is: " << static_cast(im->at(x, y)[1])
				<< "  R value is: " << static_cast(im->at(x, y)[2])
				<< endl;
		}
	}
}
int main()
{
	Mat img = imread("demo1.jpg");
	imshow("image",img);
	setMouseCallback("image",onMouse,reinterpret_cast(&img));//鼠标响应函数
	waitKey(0);
	return 0;
}

运行效果:

数字图像处理 (c++ opencv):通过鼠标点击操作获取图像的像素坐标和像素值_第1张图片

你可能感兴趣的:(OpenCV,大数据)