Ask Your Question
1

how to get the pixel value by clicking on image?

asked 2017-08-02 02:58:26 -0600

vps gravatar image

updated 2017-08-02 06:15:47 -0600

Hi all, I am able to get the pixel coordinate by clicking on image with setmousecallback function. I want to return the pixel value of that coordinate. But I am getting some error. Please, check the below code. xyz matrix contain the depth information. Thank you.

setMouseCallback("cascade_image", onMouse, &rxyz);

static void onMouse(int event, int i, int j, int flags, void* param, Mat &xyz)
{

    if (event == EVENT_LBUTTONDOWN)
    {
        short val = xyz.at< short >(i,j); 
        cout << "x= " << i << " y= " << j << "val= "<<val<< endl;
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-08-02 03:06:36 -0600

berak gravatar image

updated 2017-08-02 03:19:34 -0600

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) // 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.

edit flag offensive delete link more

Comments

hi, thank for your response. But I am getting the error below error for this line setMouseCallback("cascade_image", onMouse, &xyz)

Severity Code Description Project file line suppression state (Cv :: setMouseCallback (const cv :: String &, cv :: MouseCallback, void *) ": Conversion of argument 2 from" void (__cdecl *) "(int, int, int, void *, cv :: mat &) "in" cv :: MouseCallback "not possible

vps gravatar imagevps ( 2017-08-02 03:24:26 -0600 )edit
2

again, you must use the callback function like in the answer, without an additional cv::Mat param

can you look at the answer, again, please, it seems, you missed it.

berak gravatar imageberak ( 2017-08-02 03:27:37 -0600 )edit
1

yeah. got successful result. thank you

vps gravatar imagevps ( 2017-08-02 04:31:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-02 02:58:26 -0600

Seen: 8,456 times

Last updated: Aug 02 '17