0

I want to implement a mouse callback in OpenCV and print the intensity value to the console. I tried the following:

void mouseHandler( int event, int x, int y, int flags, void* param ) {

if( event != CV_EVENT_LBUTTONDOWN )
    return;

cv::Point point = cv::Point( x, y );
cv::Mat *img = (cv::Mat*) param;
uchar val = img->at< uchar >( point );
std::printf( "x: %i y:%i val: %u\n", x, y, val ); 
}

the call to this is:

cv::setMouseCallback( "imageName", mouseHandler, &img);

When I click in the image, my program crashes. I tried to only print the coordinates and this works just fine. Apparently the program crashes when I try to access the image values...

I have a single channel CV_8U image:

img.convertTo( nImg, CV_32FC3 );

cv::cvtColor( nImg, hsvImg, CV_RGB2HSV_FULL );
cv::split( hsvImg, channels );

channels[2].convertTo( chan, CV_8U );
cv::setMouseCallback( "chan", mouseHandler, &chan );

Any ideas?

5
  • 1
    Could you please post how do you create your image ? Here you access to the image as if the image contains Integer. Most of the time an image contain unsigned char... So how do you create your image ? Nov 6, 2012 at 10:19
  • thanks for pointing this out, I changed my question - the problem is still the same
    – Maecky
    Nov 6, 2012 at 10:26
  • have you tried swapping y for x : cv::Point point = cv::Point( y, x );? Nov 6, 2012 at 10:29
  • 3
    Do you have problem accessing variable chan OUTSIDE mouseHandler? I think the problem is in the construction of chan, if you can show the code snippet on this bit.
    – james
    Nov 6, 2012 at 10:39
  • thank you james - you are right! I contructed chan in a function and therefore the scope of the variable is just in this function - haven't thought about that...
    – Maecky
    Nov 6, 2012 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.