I’m using cvBlobs to track some objects in a video. cvBlobs uses the older C interface with types like IplImage, cvMat, .. and I’m using the C++ interface which uses cv::Mat.
So I have to convert between the 2 types in order to use the library. This works but I’m having trouble releasing the memory. The memory used by my program keeps growing.
This is my code and at the bottom you can see my attempt at releasing the memory (compiler error).
void Tracker::LabelConnectedComponents(Mat& Frame, Mat& Foreground)
{
// Convert to old format, this is the method used in the opencv cheatsheet
IplImage IplFrame = Frame;
IplImage IplForeground = Foreground;
IplImage *LabelImg = cvCreateImage(cvGetSize(&IplFrame), IPL_DEPTH_LABEL, 1);
// Make blobs (IplForeground is the segmented frame, 1 is foreground, 0 background)
unsigned int result = cvLabel(&IplForeground, LabelImg, Blobs);
// Remove small blobs
cvFilterByArea(Blobs, 500, 1000000);
// Draw bounding box
cvRenderBlobs(LabelImg, Blobs, &IplFrame, &IplFrame, CV_BLOB_RENDER_BOUNDING_BOX | CV_BLOB_RENDER_CENTROID);
// Convert back to c++ format
Frame = cvarrToMat(&IplFrame);
// Here are the problems
cvReleaseImage(&IplFrame); // error
cvReleaseImage(&IplForeground); // error
cvReleaseImage(&LabelImg); // ok
}
cvReleaseImage has as argument an IplImage** type and this is the compiler error:
tracker.cpp|35 col 33 error| cannot convert ‘IplImage* {aka _IplImage*}’ to ‘IplImage** {aka _IplImage**}’ for argument ‘1’ to ‘void cvReleaseImage(IplImage**)’
I know &IplFrame isn’t the right argument but &&IplFrame doesn’t work. How can I release those 2 IplImages?
The problem is that you have created copies of your objects here:
Therefore, these calls:
wouldn’t release the original images even if this would compile. If you are already deleting the objects (i.e. changing them) why do you send them to the method as references instead of pointers? I am a bit confused as it seems that you are doing something like this:
I have check the documentation, and it says
Mat::operator IplImage() doesn't copy data, which means thay IplFrame doesn’t own the memory, so it is not correct to release it.I think it depends on how
Matinstance was created – if it was created fromIplImage*withcopyDataset tofalse, then you would need to release the originalIplImageinstance. If it was created withcopyDataset totrue, thenMatinstance automatically takes care of it (unless you do it explicitly withMat::release)