opencv摄像头速度慢_我的Opencv应用程序处理速度非常慢

本文介绍了一个OpenCV应用中遇到的问题,即从摄像头捕获视频并去除背景后,播放速度慢(约1fps),而背景移除部分能以3fps运行。问题主要出在渲染部分,存在内存泄漏。提供的解决方案是在循环外预先获取帧并创建图像,避免每次迭代时的内存分配,从而提高应用性能。
摘要由CSDN通过智能技术生成

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

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.

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

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.

here is the 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);

}

}

解决方案

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值