notphunny
notphunny

Reputation: 55

Mat and setMouseCallback function

I have a my_mouse_callback example function that works with IplImage*:

void my_mouse_callback(int event, int x, int y, int flags, void* param) 
{
IplImage* image = (IplImage*) param;
switch( event ) 
{
    case CV_EVENT_LBUTTONDOWN: 
        drawing_box = true;
        box = cvRect(x, y, 0, 0);
        break;
        ...

        draw_box(image, box);
        break;
}

which is implemented in main like this:

cvSetMouseCallback(Box Example,my_mouse_callback,(void*) image);

The problem is, in my code I'm using Mat object and it can't be transfered this way to the setMouseCallback function.

I'm looking for a solution that doesn't involve transfering Mat to IplImage*.

Or if there is no solution, how can I correctly convert Mat to IplImage*?

I tried that already with this code from the opencv documentation:

Mat I;
IplImage* pI = &I.operator IplImage();

and it didn't work.

Upvotes: 4

Views: 4635

Answers (2)

john k
john k

Reputation: 6615

Why can't you transfer a Mat to your MouseCallback function? You just did it with an IplImage. Mats are just pointers. Here's how I did it.

void onMouse(int event, int x, int y, int flags, void* param)
{
    Mat src;
    src = *((Mat*)param);

   ...

and called it like so in my main loop:

 setMouseCallback("source", onMouse, &src);

Upvotes: 2

karlphillip
karlphillip

Reputation: 93410

There's no equivalent of that function in the C++ interface as far as I can tell.

But you can convert a cv::Mat to an IplImage* like this, and the other way around like this:

cv::Mat mat(my_IplImage);

Upvotes: 3

Related Questions