利用opencv获取鼠标左键点击位置图像的像素值

功能:利用opencv获取鼠标左键点击位置图像的像素值(RGB像)
vs2015+opencv3.1
2016.10.22

#include
#include
using namespace std;
using namespace cv;
void on_mouse(int EVENT, int x, int y, int flags, void* userdata);
void main()
{
    namedWindow("【display】");
    Mat src;
    src = imread("F:\\lena_rgb.jpg");
    //cvtColor(src, src, COLOR_RGB2GRAY);
    setMouseCallback("【display】", on_mouse,&src);
    //以40ms刷新显示
    while (1)
    {
        imshow("【display】", src);
        waitKey(40);
    }



}

void on_mouse(int EVENT, int x, int y, int flags, void* userdata)
    {
        Mat hh;
        hh = *(Mat*)userdata;
        Point p(x, y);
        switch (EVENT)
        {
            case EVENT_LBUTTONDOWN:
            {

                printf("b=%d\t", hh.at(p)[0]);
                printf("g=%d\t", hh.at(p)[1]);
                printf("r=%d\n", hh.at(p)[2]);
                circle(hh, p, 2, Scalar(255),3);
            }
            break;

        }




    }

你可能感兴趣的:(opencv学习)