4

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
notphunny
  • 55
  • 2
  • 5

2 Answers2

3

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);
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 1
    Ty, your answer lead me to solution, IplImage* ipl_img = new IplImage (mat_image); then I was able to use: setMouseCallback( imageName, onMouse, (void*) &ipl_img ); – notphunny Nov 20 '11 at 13:05
  • That was the idea. By the way, there's a little checkbox near my answer, you can click on it to select it as the official answer to your question. Good luck, and welcome to stackoverflow. – karlphillip Nov 20 '11 at 14:52
2

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);
john k
  • 6,268
  • 4
  • 55
  • 59