I implemented the imshowCompare() function that switch the output and the original image by the left mouse button click in cv::imshow windows. This is a very useful test function. The code is as follows, but can not solve the problem of memory leak.
typedef struct { String winname; Mat src; Mat dst; } strImshowCompare;
void on_MouseHandle_Compare(int event, int x, int y, int flags, void* param);
void imshowCompare(const String& winname, const Mat& src, const Mat& dst)
{
// src, dst must be cloned, otherwise the external Mat may be modified.
Mat newsrc = src.clone(); // Their memory will leak.
Mat newdst = dst.clone(); // Their memory will leak.
cv::imshow( winname, newdst);
// struct strImshowCompare can't delete in this function! It must delete after the window is closed. But I don't know when the window is closed.
strImshowCompare* str = new strImshowCompare;
str->winname = winname;
str->src = newsrc;
str->dst = newdst;
cv::setMouseCallback(winname, on_MouseHandle_Compare, (void *)str);
}
void on_MouseHandle_Compare(int event, int x, int y, int flags, void* param)
{
strImshowCompare* str = (strImshowCompare*) param;
switch (event) {
case cv::EVENT_LBUTTONDOWN: {
cv::imshow(str->winname, str->src);
break;
}
case cv::EVENT_LBUTTONUP: {
cv::imshow(str->winname, str->dst);
break;
}
}
}