Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

using the code from your link, you are already very close.

all it needs now is access to your image in the mouse handler:

// in main:
// Read image from file 
Mat img = imread("MyPic.JPG");
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, &img); // pass the img address to callback


// callback:
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    Mat img = *((Mat*)userdata); // 1st cast , then deref

    if  ( event == EVENT_LBUTTONDOWN )
    {
        Vec3b pixel = img.at<Vec3b>(y,x); // y,x here !
        cout << "Left button clicked - position (" << x << ", " << y << ")" << endl;
        cout << "color (" << int(pixel[0])  << ", " << int(pixel[1])   << ", " << int(pixel[2])  << ")" << endl;
    }
}