Very slow processing of my Opencv application

I am creating an OpenCV application that captures video from a camera and overlays it with another video after removing the background.

I canโ€™t reach a reasonable speed, since it reproduces the output at a speed of about 1 frame per second, while my background mode works at a speed of 3 frames per second.

Is there a way to display the background video at its normal speed and overlay the processed video at 3 frames per second?

I tried to comment on my code, and I realized that the problem lies in the very part of the rendering. I tried to display the video along with my webcam tape, and I noticed that when displayed with an open window, the actual frames per second and frames per second decrease.

here is a sample code:

void main() { CvCapture* capture, *Vcap; capture = cvCaptureFromCAM(0); if(!capture) { printf("Video Load Error"); } Vcap = cvCaptureFromAVI("bgDemo.mp4"); //printf("\nEntered BGR"); if(!Vcap) { printf("Video Load Error"); } while(1) { IplImage* src = cvQueryFrame(Vcap); if(!src) { Vcap = cvCaptureFromAVI("bgDemo.mp4"); continue; } IplImage* bck1 = cvCreateImage(cvGetSize(src),8,3); cvResize(src,bck1,CV_INTER_LINEAR); cvShowImage("BCK",bck1); cvWaitKey(1); } } 
+2
source share
1 answer

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.

+4
source

All Articles