我的Opencv应用程序处理速度非常慢 [英] Very Slow Processing of my Opencv Application

查看:2135
本文介绍了我的Opencv应用程序处理速度非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个OpenCV应用程序,该应用程序从相机捕获视频,并在删除背景后将其覆盖在另一个视频上。

I am building an OpenCV application which captures a video from camera and overlays it on another video after removing the background.

我无法达到合理的速度,因为它以大约1 fps的速度播放输出,而我的背景移除工作速度为3fps。

I am not able to achieve a reasonable speed as it is playing the output at about 1 fps, whereas my background removal is working at 3fps.

有没有办法以正常速度显示背景视频并以3fps覆盖已处理的视频?

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

我试过评论我的代码,我意识到问题主要在于渲染部分本身。我尝试显示视频和我的网络摄像头,我注意到用openCV显示的实际fps和视频的fps有所下降。

I tried commenting out my code and I realized that the problem lies majorly with the Rendering part itself. I tried displaying the video along with my web cam feed and I noticed that there is a drop in the actual fps and the fps of video when displayed with openCV.

这里是示例代码:

 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);
    }
}


推荐答案

主要问题是您在循环的每次迭代中分配新图像而不在循环结束时释放它。换句话说,你有一个漂亮的内存泄漏

The main problem is that you are allocating a new image at every iteration of the loop without releasing it at the end of the loop. In other words, you have a beautiful memory leak.

更好的方法是在循环开始之前简单地抓取一帧视频。这样你就可以用正确的尺寸创建 bck1

A better approach is to simply grab a frame of the video before the loop starts. This will let you create bck1 with the right size just once.

你的代码还有其他问题,我我在下面共享一个固定版本,请确保您注意每行代码以查看更改的内容。我没时间测试它,但我相信你会弄明白:

There are other problems with your code, I'm sharing a fixed version below, make sure you pay attention to every line of code to see what changed. I haven't had time to test it, but I'm sure you'll figure it 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 considerably.

这篇关于我的Opencv应用程序处理速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆