macos OpenCV视频捕获和fps问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1168214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:17:31  来源:igfitidea点击:

OpenCV Video capture and fps problem

cperformancemacosopencv

提问by Andrea Girardi

I'm capturing video from my webcam using OpenCV on MacOSX. It works fine but when I try to play on QuickTime my captured video it plays too fast. i.e. I capture from camera for 10 seconds but when I play on QuickTime the video is 2 seconds.

我正在 MacOSX 上使用 OpenCV 从我的网络摄像头捕获视频。它工作正常,但是当我尝试在 QuickTime 上播放我捕获的视频时,它播放得太快了。即我从相机捕获 10 秒,但当我在 QuickTime 上播放时,视频是 2 秒。

I've tried to change fps from 25 to 10 and It's works quite fine, but I'm sure it's not the correct process:

我尝试将 fps 从 25 更改为 10 并且效果很好,但我确定这不是正确的过程:

CvVideoWriter *writer = 0;  
int isColor = 1;
int fps     = 25;
int frameW  = 640; // 744 for firewire cameras
int frameH  = 480; // 480 for firewire cameras

The problem is that for now I've to capture with WebCam but the real pourpose of program is to capture image from any external source connected to my Mac.

问题是,现在我必须使用网络摄像头进行捕捉,但程序的真正目的是从连接到我的 Mac 的任何外部源捕捉图像。

I'm using this code to capture:

我正在使用此代码来捕获:

for (;;) {
  cvGrabFrame(capture)
  image = cvRetrieveFrame(capture);
  cvWriteFrame( writer, image );
}

Any hint? I'm also showing webcam output on cvNamedWindow, how can I improve quality in this windows?

任何提示?我还在 cvNamedWindow 上显示网络摄像头输出,如何提高此窗口的质量?

thanks a lot to all!

非常感谢大家!

Andrea!

安德烈!

回答by Inshallah

Could be that compressing the captured video and saving it to a file is too CPU intensive. If that's the case then you really only see 10 FPS in the cvNamedWindow, and only 10 FPS are written to the file. Specifiying 25 FPS in the file will naturally speed playback up some.

可能是压缩捕获的视频并将其保存到文件中过于占用 CPU。如果是这种情况,那么您实际上只能在 cvNamedWindow 中看到 10 FPS,并且只有 10 FPS 写入文件。在文件中指定 25 FPS 自然会加快播放速度。

To see if that's really your problem, you could try to save the image data only in memory. I've not tried it out, but I think you'd do that with cvCloneImage().

要查看这是否真的是您的问题,您可以尝试仅将图像数据保存在内存中。我还没有尝试过,但我认为你会用 cvCloneImage() 来做到这一点。

You could also try some other format with a lower CPU overhead to save your video:

您还可以尝试其他一些 CPU 开销较低的格式来保存视频:

CV_FOURCC('P','I','M','1')    = MPEG-1 codec
CV_FOURCC('M','J','P','G')    = motion-jpeg codec (does not work well)
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec

回答by Saleh

Use cvCreateVideoWriter(filename, fourcc, fps, frame size, is color)with fps = 10, fps = 20and different values. If you already know the fps value of frames coming from camera use that.

使用cvCreateVideoWriter(filename, fourcc, fps, frame size, is color)fps = 10fps = 20和不同的价值观。如果您已经知道来自相机的帧的 fps 值,请使用它。

Problem is that you are getting less frames per second from camera and you're writing more frames to video file

问题是您每秒从相机获得的帧数较少,而您正在将更多帧写入视频文件

Regards, Saleh.

问候,萨利赫。

回答by Ian

I was having similar performance as you mentioned (about 10fps) and found that successive retrieveFrame()calls were taking forever. I found that getCaptureProperty(CV_CAP_PROP_FPS)was at a default value of 0. I changed this value to 25 using setCaptureProperty(CV_CAP_PROP_FPS,25.0)and was able to capture much faster.

我的性能与您提到的相似(大约 10 帧/秒),并发现连续的retrieveFrame()呼叫一直在持续。我发现它getCaptureProperty(CV_CAP_PROP_FPS)的默认值为 0。我将此值更改为 25 使用,setCaptureProperty(CV_CAP_PROP_FPS,25.0)并且能够更快地捕获。

回答by Sam

I confirm that 10 fps is standard, although I believe it may be because you don't have a camera which captures at more than 10 fps (which is likely the issue).

我确认 10 fps 是标准的,尽管我相信这可能是因为您没有以超过 10 fps 的速度捕获的相机(这可能是问题所在)。

This is still strange because I'm using waitkey(30), which should be 33fps, but it's precisely 10.

这仍然很奇怪,因为我使用的是waitkey(30),应该是33fps,但它正好是 10。