Main problem is that you select a new image at each iteration of the loop, without releasing it at the end of the loop. In other words, you have a beautiful memory leak .
The best approach is to just grab the frame of the video before starting the loop. This will allow you to create bck1 with the correct size only once.
There are other problems with your code, I use the fixed version below, make sure you pay attention to each line of code to see what has changed. I did not have time to verify this, but I am sure that you will find out:
int main() { // I know what you are doing, just one capture interface is enough CvCapture* capture = NULL; capture = cvCaptureFromCAM(0); if(!capture) { printf("Ooops! Camera Error"); } capture = cvCaptureFromAVI("bgDemo.mp4"); if(!capture) { printf("Ooops! Video Error"); // if it failed here, it means both methods for loading a video stream failed. // It makes no sense to let the application continue, so we return. return -1; } // Retrieve a single frame from the camera IplImage* src = cvQueryFrame(capture); if(!src) { printf("Ooops! #1 cvQueryFrame Error"); return -1; } // Now we can create our backup image with the right dimensions. IplImage* bck1 = cvCreateImage(cvGetSize(src),src->depth, src->nChannels); if(!bck1) { printf("Ooops! cvCreateImage Error"); return -1; } while(1) { src = cvQueryFrame(capture); if(!src) { printf("Ooops! #2 cvQueryFrame Error"); break; } cvResize(src, bck1, CV_INTER_LINEAR); cvShowImage("BCK",bck1); cvWaitKey(10); } cvReleaseImage( &bck1 ); // free manually allocated resource return 0; }
These fixes should speed up your application significantly.
source share