0
点赞
收藏
分享

微信扫一扫

opencv 鼠标点击获取HSV、RGB

鼠标点击获取HSV等信息。

#include<iostream>
#include<opencv2\opencv.hpp>
#include"opencv2\highgui\highgui.hpp"
using namespace std;
using namespace cv;
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
	Mat srcImage = *(Mat*)param;
	switch (event)
	{
		case CV_EVENT_LBUTTONDOWN:
		{
			cout << "B: " << (int)srcImage.at<Vec3b>(y, x)[0] << '\t';
			cout << "G: " << (int)srcImage.at<Vec3b>(y, x)[1] << '\t';
			cout << "R: " << (int)srcImage.at<Vec3b>(y, x)[2] << endl;
			
			Mat dstImage;
			cvtColor(srcImage, dstImage, COLOR_RGB2HSV);
			cout << "H: " << (int)dstImage.at<Vec3b>(y, x)[0] << '\t';
			cout << "S: " << (int)dstImage.at<Vec3b>(y, x)[1] << '\t';
			cout << "V: " <<(int) dstImage.at<Vec3b>(y, x)[2] << endl;

			cvtColor(srcImage, dstImage, COLOR_RGB2GRAY);
			cout << "Gray: " << (int)dstImage.at<uchar>(y, x) << endl;
		}
		break;
	}
}
int main()
{
	Mat srcImage=imread("tbbt.jpg");
	namedWindow("得到RGB & HSV & GRAY");
	imshow("得到RGB & HSV & GRAY", srcImage);
	setMouseCallback("得到RGB & HSV & GRAY", on_MouseHandle, &srcImage);
	waitKey(0);
	
}
举报

相关推荐

0 条评论