赞
踩
下面这行代码是获取摄像头每帧的时间戳:
double timestamp = cap.get(cv::CAP_PROP_POS_MSEC);
改变帧率的方法是:
cap.set(cv::CAP_PROP_FPS, 30); //帧率改为30
但是实际测试时发现帧率并未被改变,这个可能和VideoCapture cap(cv::CAP_V4L2)有关,cv::CAP_V4L2只是其中一种读取方法,这个参数可能需要和相机采用的驱动方法有关。
编译:
g++ camera_data.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_videoio
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <fstream>
-
- #include <time.h>
-
- using namespace std;
-
- // OpenCV includes
- #include "opencv2/core.hpp"
- #include "opencv2/highgui.hpp"
- using namespace cv;
-
- int main(int argc, const char** argv)
- {
- // 1.创建视频采集对象;
- VideoCapture cap(cv::CAP_V4L);
- // 2.打开默认相机;
- cap.open(0);
-
- int curFPS=cap.get(cv::CAP_PROP_FPS);
- cout<< "FPS: "<< curFPS<<endl;
-
- cap.set(cv::CAP_PROP_FPS, 30);
- int curFpsSet=cap.get(cv::CAP_PROP_FPS);
- cout<< "set FPS: "<< curFpsSet<<endl;
-
- // 3.判断相机是否打开成功;
- if (!cap.isOpened())
- return -1;
-
- namedWindow("Video", 1);
-
-
- for (;;)
- {
- // 获取新的一帧;
- Mat frame;
-
- double timestamp = cap.get(cv::CAP_PROP_POS_MSEC);
- cout<<fixed;
- cout<<"time: "<< timestamp<<endl;
-
- time_t end_time = time(NULL);
- printf("ctime is %s\n",ctime(&end_time)); //得到日历时间
-
- cap >> frame;
- if (frame.empty())
- return 0;
-
- // 显示新的帧;
- imshow("Video", frame);
- // cv::waitKey(1);
-
- // 按键退出显示;
- if (waitKey(3) >= 0) break;
- }
-
- // 5.释放视频采集对象;
- cap.release();
-
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。