opencv读取图像像素值

用鼠标取图片中的像素值

#include
#include
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("F:\\6.jpg", 1);
 if(!image.data)
 {
  cout<<"the image is error"<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(yvalue, xvalue)[0];
    int g = image1.at(yvalue, xvalue)[1];
    int r = image1.at(yvalue, xvalue)[2];
    cout<<"B:"<' '<<"G:"<' '<<"R:"<break;
 }
}

你可能感兴趣的:(opencv,opencv)