adi1992
adi1992

Reputation: 95

Frames Lost while processing video with opencv

I am capturing a video with 30fps, but when I process the video with openCV for AruCo marker detection I am loosing almost half of the frames. So for a 5 min video I expect 5x60x30 = 9000 frames but I am getting only about 4500 frames. I tried different resolution and fps while recording but the problem still persists. My code is as follows. I later want to sync the video with the audio recorded from the camera, so even if I could know the frames which are being lost can solve my problem. Any pointers or suggestions are welcome.

    #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;
}

Upvotes: 0

Views: 636

Answers (1)

Sane_or_not
Sane_or_not

Reputation: 146

The problem is:

cap >> sample;

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

the program is reading a frame from source twice in every iteration. You should remove the cap >> sample; line and it will be fine.

Upvotes: 1

Related Questions