使用opencv处理视频时丢帧

Frames Lost while processing video with opencv

我正在以 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;
}

问题是:

cap >> sample;

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

程序在每次迭代中从源读取帧两次。 你应该取下盖子 >> 样品;线,它会没事的。