Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you're close, but there's a few errors:

it is at(y,x) , not x,y (row-major)

you have to be strict with the type, xyz.at< short > is only valid, IF xyz.type() == CV_16S . for uchar, CV_8U images, it has to be xyz.at< uchar >, for BGR images xyz.at< Vec3b > , etc.

you're close, but there's a few errors:your callback function is wrong, you must not try to add another Mat param there, but:

it is

at(y,x) , not x,ystatic void onMouse(int event, int x, int y, int flags, void* param)
{
 (row-major)

Mat &xyz = *((Mat*)param); //cast and deref the param if (event == EVENT_LBUTTONDOWN) { short val = xyz.at< short >(y,x); // opencv is row-major ! cout << "x= " << x << " y= " << y << "val= "<<val<< endl; } }

you also have to be strict with the type, xyz.at< short > is only valid, IF xyz.type() == CV_16S . for uchar, CV_8U images, it has to be xyz.at< uchar >, for BGR images xyz.at< Vec3b > , etc.

your callback function is wrong, you must not try to add another Mat param there, but:

Mat xyz;
setMouseCallback("cascade_image", onMouse, &xyz);

static void onMouse(int event, int x, int y, int flags, void* param)
{
    Mat &xyz = *((Mat*)param); //cast and deref the param

    if (event == EVENT_LBUTTONDOWN)
    {
        short val = xyz.at< short >(y,x); // opencv is row-major ! 
        cout << "x= " << x << " y= " << y << "val= "<<val<< endl;
    }
}

you also have to be strict with the type, xyz.at< short > is only valid, IF xyz.type() == CV_16S . for uchar, CV_8U images, it has to be xyz.at< uchar >, for BGR images xyz.at< Vec3b > , etc.

your callback function is wrong, you must not try to add another Mat param there, but:

Mat xyz;
setMouseCallback("cascade_image", onMouse, &xyz);
&xyz); // pass the address

static void onMouse(int event, int x, int y, int flags, void* param)
param) // you'll get it back in param
{
    Mat &xyz = *((Mat*)param); //cast and deref the param

    if (event == EVENT_LBUTTONDOWN)
    {
        short val = xyz.at< short >(y,x); // opencv is row-major ! 
        cout << "x= " << x << " y= " << y << "val= "<<val<< endl;
    }
}

you also have to be strict with the type, xyz.at< short > is only valid, IF xyz.type() == CV_16S . for uchar, CV_8U images, it has to be xyz.at< uchar >, for BGR images xyz.at< Vec3b > , etc.

your callback function is wrong, you must not try to add another Mat param there, but:

Mat xyz;
setMouseCallback("cascade_image", onMouse, &xyz); // pass the address

static void onMouse(int event, int x, int y, int flags, void* param) // you'll get it back now it's in param
{
    Mat &xyz = *((Mat*)param); //cast and deref the param

    if (event == EVENT_LBUTTONDOWN)
    {
        short val = xyz.at< short >(y,x); // opencv is row-major ! 
        cout << "x= " << x << " y= " << y << "val= "<<val<< endl;
    }
}

you also have to be strict with the type, xyz.at< short > is only valid, IF xyz.type() == CV_16S . for uchar, CV_8U images, it has to be xyz.at< uchar >, for BGR images xyz.at< Vec3b > , etc.