使用opencv处理视频时丢帧

时间:2018-08-03 14:23:22

标签: opencv video-capture video-processing

我正在以30fps的速度捕获视频,但是当我使用openCV处理视频以进行AruCo标记检测时,我丢失了几乎一半的帧。因此,对于5分钟的视频,我期望5x60x30 = 9000帧,但我只能得到大约4500帧。录制时我尝试了不同的分辨率和fps,但问题仍然存在。我的代码如下。后来我想将视频与摄像机录制的音频同步,因此即使我知道丢失的帧也可以解决我的问题。欢迎任何指示或建议。

    #include "opencv2\core.hpp"
    #include "opencv2\imgcodecs.hpp"
    #include "opencv2\imgproc.hpp"
    #include "opencv2\highgui.hpp"
    #include "opencv2\aruco.hpp"
    #include "opencv2\calib3d.hpp"
    #include <time.h>

    #include <sstream>
    #include <iostream>                                           
    #include <fstream>
    #include "stdafx.h"

    using namespace std;
    using namespace cv;

   #define _CRT_SECURE_NO_WARNINGS 1

   int startWebcamMonitoring() //(const Mat& cameraMatrix, const Mat&  
   distanceCoefficients, float arucoSquareDimensions)


  {
  Mat   frame4;
  Scalar_<double> borderColor, borderColor2, borderColor3;

  vector<int> markerIds;
 vector < vector<Point2f>> markerCorners, rejectedCandidates ;
 aruco::DetectorParameters parameters;

Ptr<aruco::Dictionary> makerDiktionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);

VideoCapture cap("sample.mp4");
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frames per second : " << fps << endl;

while (true)
{
    cap >> sample;

    if (!cap.read(frame4))
        break; 


aruco::detectMarkers(frame4, makerDiktionary, markerCorners, markerIds);
aruco::drawDetectedMarkers(frame4, markerCorners, markerIds, borderColor);
aruco::estimatePoseSingleMarkers(markerCorners, arucoSquareDimension, 
cameraMatrix, distanceCoefficients, rotationVectors, translationVectors);
Mat rotationMatrix

for (int i = 0; i < markerIds.size(); i++)
{
    aruco::drawAxis(frame4, cameraMatrix, distanceCoefficients, rotationVectors[i], translationVectors[i], 0.01f);





    Rodrigues(rotationVectors[i], rotationMatrix);
    time_t current = time(0);

    cout <<   " Translation " << translationVectors[i] << " ID" << markerIds[i]  << " Euler angles " << 180 / 3.1415*rotationMatrixToEulerAngles(rotationMatrix) << "current time " << ctime(&current) << endl;

}  

freopen("output_sample", "a", stdout);
imshow("recording", frame4);
    if (waitKey(30) >= 0) break;
}

1 个答案:

答案 0 :(得分:1)

问题是:

cap >> sample;

if (!cap.read(frame4))
    break; 

程序在每次迭代中从源读取两次帧。 您应该取下盖子>>样品;行,就可以了。