简体   繁体   English

OpenCV:无法通过视频获得帧率

[英]OpenCV: cant get framerate from video

I want to get framerate for video but I always get -nan on linux. 我想获得视频的帧率,但我总是在Linux上获得-nan。

VideoCapture video(input);
if (!video.isOpened())  // zakoncz program w przypadku, problemu z otwarciem
{
    exit(0);
}

double fps = video.get(CV_CAP_PROP_FPS);

My openCv version is 2.4.7. 我的openCv版本是2.4.7。 The same code works fine on windows. 相同的代码在Windows上运行良好。

My guess is that it's camera dependent. 我的猜测是它依赖于相机。 Some (API) functions are sometimes not implemented in OpenCV and/or supported by your camera. 某些(API)功能有时未在OpenCV中实现和/或您的相机支持。 Best would be if you check the code on github. 如果你检查github上的代码,那将是最好的。

Concerning your problem: I am able to get the frame rates with a normal webcam and a XIMEA camera with your code. 关于您的问题:我可以使用普通网络摄像头和XIMEA摄像头获取帧速率。

Tested on: 测试:

  • Ubuntu 15.04 64bit Ubuntu 15.04 64bit
  • OpenCV 3.0.0 compiled with Qt and XIMEA camera support OpenCV 3.0.0编译与Qt和XIMEA相机支持

You could measure your frame rate yourself: 您可以自己测量帧速率:

double t1 = (double)cv::getTickCount();
// do something
t1 = ((double)cv::getTickCount() - t1)/cv::getTickFrequency();

Gives you the time that //do something spent. 给你时间//do something

It is from a file, you can try to estimate it yourself. 它来自一个文件,你可以尝试自己估算一下。

VideoCapture video("name_of_video.format");
int frameCount = (int)video.get(CV_CAP_PROP_FRAME_COUNT) ;
//some times frame count is wrong, so you can verify
video.set(CV_CAP_PROP_POS_FRAMES , frameCount-1);

//try to read the last frame, if not decrement frame count
    while(!(video.read(nextFrame))){

    frameCount--;

    video.set(CV_CAP_PROP_POS_FRAMES , frameCount-1);
}

//it is already set above, but just for clarity
      video.set(CV_CAP_PROP_POS_FRAMES , frameCount-1);
      double fps = (double)(1000*frameCount)/( video.get(CV_CAP_PROP_POS_MSEC));
      cout << "fps: " << fps << endl;

This is how I get framerate when using CV_CAP_PROP_FPS fails 这是我在使用CV_CAP_PROP_FPS失败时获得帧率的方法

The question doesn't clarify if this refers to video from a live source (webcam) or from a video file. 该问题并未说明这是指来自实时来源(网络摄像头)还是来自视频文件的视频。

If the latter, the capabilities of OpenCV will depend on the format and codecs used in the file. 如果是后者,OpenCV的功能将取决于文件中使用的格式和编解码器。 For some file formats, expect to get a 0 or NaN. 对于某些文件格式,期望得到0或NaN。

If the former, the real fps of the source may not be returned, especially if the requested framerate is not supported by the hardware, and a different one is used instead. 如果是前者,则可能不会返回源的实际fps,尤其是如果硬件不支持所请求的帧速率,而是使用不同的帧速率。 For this case I would suggest an approach similar to @holzkohlengrill's, but only do that calculation after an initial delay of say 300ms (YMMV), as grabbing of the first frames and some initialisations happening can mess with that calculation. 对于这种情况,我建议采用类似于@ holzkohlengrill的方法,但只能在初始延迟300ms(YMMV)之后进行计算,因为抓取第一帧和一些初始化可能会弄乱该计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM