Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

actually, your userdata has to stay valid, until destroyWindow() is called. clicking the close button will not remove the internal window management structure, the next call to imshow() with the resp. name will just reopen it, so , careful with that axe..

either favour @eduardo's solution (keep it alive until the end) or simply reverse the logic. maybe you do not need that struct at all ? rather move the processing into the mainloop:

void on_mouse(int event, int x, int y, int flags, void* param) { int clicked = (int)param; *clicked = even; }

int main() { int clicked = 0; // mouse state String winname = "Test"; namedWindow(winname); setMouseCallback(winname, on_mouse, (void *)(&clicked)); while (true) { Mat src, dst = ... // your processing

if (clicked == 1) // LBUTTON
   imshow(winname, src);
if (clicked == 2) // RBUTTON
   imshow(winname, dst);

int c = waitKey(30);
if (c == 27)
  break;

}

actually, your userdata has to stay valid, until destroyWindow() is called. clicking the close button will not remove the internal window management structure, the next call to imshow() with the resp. name will just reopen it, so , careful with that axe..

either favour @eduardo's solution (keep it alive until the end) or simply reverse the logic. maybe you do not need that struct at all ? rather move the processing into the mainloop:

void on_mouse(int event, int x, int y, int flags, void* param)
{
    int clicked = (int)param;
    *clicked = even;
}

(int*)param; *clicked = even; // just pass it on } int main() { int clicked = 0; // mouse state String winname = "Test"; namedWindow(winname); setMouseCallback(winname, on_mouse, (void *)(&clicked)); while (true) { Mat src, dst = ... // your processing

processing

    if (clicked == 1) // LBUTTON
    imshow(winname, src);
 if (clicked == 2) // RBUTTON
    imshow(winname, dst);

 int c = waitKey(30);
 if (c == 27)
   break;
  }

}