美文网首页
opencv 鼠标取图片中的像素值

opencv 鼠标取图片中的像素值

作者: hirolin | 来源:发表于2017-11-28 23:01 被阅读0次

    用鼠标取图片中的像素值

    #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("F:\\6.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<<"B:"<<b<<' '<<"G:"<<g<<' '<<"R:"<<r<<endl;
       }
      }
      break;
     }
    }
    

    相关文章

      网友评论

          本文标题:opencv 鼠标取图片中的像素值

          本文链接:https://www.haomeiwen.com/subject/sasybxtx.html