OpenCV:VideoCapture :: get(CV_CAP_PROP_FPS)返回0 FPS [英] OpenCV: VideoCapture::get(CV_CAP_PROP_FPS) returns 0 FPS

查看:5502
本文介绍了OpenCV:VideoCapture :: get(CV_CAP_PROP_FPS)返回0 FPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的相机获取fps,以便我可以将其传递到 VideoWriter 以输出视频。然而,我得到0 fps通过调用 VideoCapture :: get(CV_CAP_PROP_FPS)从我的相机。如果我硬编码,我的视频可能太慢或太快。

I am trying to get the fps from my camera so that I can pass it to the VideoWriter for outputting the video. However, I am getting 0 fps by calling VideoCapture::get(CV_CAP_PROP_FPS) from my camera. If I hardcode it, my video may be too slow or too fast.

#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    cv::VideoCapture cap;
    int key = 0;

    if(argc > 1){
        cap.open(string(argv[1]));
    }
    else
    {
        cap.open(CV_CAP_ANY);
    }
    if(!cap.isOpened())
    {
        printf("Error: could not load a camera or video.\n");
    }

    Mat frame;
    cap >> frame;
    waitKey(5);

    namedWindow("video", 1);
    double fps = cap.get(CV_CAP_PROP_FPS);
    CvSize size = cvSize((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));

    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    if(!codec){ waitKey(0); return 0; }
    std::cout << "CODEC: " << codec << std::endl;
    std::cout << "FPS: " << fps << std::endl;
    VideoWriter v("Hello.avi",-1,fps,size);
    while(key != 'q'){
        cap >> frame;
        if(!frame.data)
        {
            printf("Error: no frame data.\n");
            break;
        }
        if(frame.empty()){ break; }
        v << frame;
        imshow("video", frame);
        key = waitKey(5);
    }
    return(0);
}

如何获得 VideoCapture :: get(CV_CAP_PROP_FPS )以返回正确的fps或给所有网络摄像机普遍使用的 VideoWriter 提供fps?

How can I get VideoCapture::get(CV_CAP_PROP_FPS) to return the right fps or give a fps to the VideoWriter that works universally for all webcams?

推荐答案

CV_CAP_PROP_FPS只适用于我所知道的视频。如果你想从网络摄像头捕获视频数据,你必须自己正确的时间。例如,使用计时器每40ms从网络摄像头捕获一帧,然后保存为25fps视频。

CV_CAP_PROP_FPS only works on videos as far as I know. If you want to capture video data from a webcam you have to time it correctly yourself. For example use a timer to capture a frame from the webcam every 40ms and then save as 25fps video.

这篇关于OpenCV:VideoCapture :: get(CV_CAP_PROP_FPS)返回0 FPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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