#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
cv::Mat org, dst, img, tmp;
void grayValue(Mat& src, int x, int y)
{
if (src.channels() == 3)
{
int b = src.at<Vec3b>(y, x)[0];
int g = src.at<Vec3b>(y, x)[1];
int r = src.at<Vec3b>(y, x)[2];
cout << " b=" << b << " g=" << g << " r=" << r << endl;
}
if (src.channels() == 1)
{
int value = src.at<uchar>(y, x);
cout << "灰度值为" << value << endl;
}
}
void onMouse(int event, int x, int y, int flags, void* ustc)
{
static cv::Point pre_pt = cv::Point(-1, -1);//初始坐标
char temp[16];
if (event == cv::EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆
{
org.copyTo(img);//将原始图片复制到img中
cout << "x= " << x << " y= " << y <<" ";
pre_pt = cv::Point(x, y);
grayValue(org, x, y);
circle(org, pre_pt, 2, cv::Scalar(255, 0, 0, 0),1, 8, 0);//划圆
}
}
int main()
{
org = imread("D://Camera Roll//lenna.tif");
//cvtColor(org, org, COLOR_BGR2GRAY);
namedWindow("img");
setMouseCallback("img", onMouse, 0);//调用回调函数
imshow("img", org);
waitKey(0);
}
灰度图片显示灰度值
彩色图片显示RGB值