Reputation: 330
I'm running OpenCV on a RaspberryPi and use OpenCVs C interface.
I need to resize the dimensions of an image from a webcam, therefore I used the cvResize()
function.
It works fine but after a few seconds I run out of memory, since I'm executing the code inside a while(1)
-Loop and read about other people having this problem, I suspect a memory leak.
Here is my code:
IplImage *frame;
IplImage *frameRaw;
main() {
CvCapture *capture = cvCreateCameraCapture(-1);
while (1) {
frameRaw = cvQueryFrame(capture);
frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);
cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST
// Do something with "frame"
}
}
I already tried to free the reserved memory at the end of each iteration using cvReleaseImage(&frameRaw)
(or &frame
) but that always caused a segmentation fault. Using cvReleaseImageHeader()
caused no segmentation fault but also didn't free any memory.
Also I tried to change the capture-resolution of the frames via cvSetCaptureProperty()
but that did nothing.
Can someone help me understand what's going wrong here?
Thanks in advance
Upvotes: 1
Views: 663
Reputation: 330
Thanks to @DanMašek I found the solution:
After each while
-loop iteration the allocated memory for the frame has to be freed. That can be accomplished using cvReleaseImage(&frame)
.
The complete code now looks like this:
IplImage *frame;
IplImage *frameRaw;
main() {
CvCapture *capture = cvCreateCameraCapture(-1);
while (1) {
frameRaw = cvQueryFrame(capture);
frame = cvCreateImage(cvSize(WIDTH, HEIGHT), frameRaw->depth, frameRaw->nChannels);
cvResize(frameRaw, frame, 0); // 0 = CV_INTER_NEAREST
// Do something with "frame"
cvReleaseImage(&frame);
}
}
Upvotes: 1