Opencv tutorial, VideoCapture playback, frame skip

Opencv C++ tutorial on how to playback video frames and video loaded into memory, Fast video replay by slider. Simple described and working tutorial working in Visual Studio 2015 with simple installation by Nugets Here or also with classical one.

opencv videocapture playback

This is not something special, but useful. A few days earlier, I posted in memory video access by vector<Mat>. Great, fast approach but the short video fills RAM memory and crashes the program even if you have 8 GB available. 

In this example, I want to access GOPRO HD faster and skip frames of video, and in memory is pure fantasy over minute video length. 

Opencv VideoCapture property CV_CAP_PROP_POS_FRAMES


I don't add extra comments. The code is pretty simple. I only highlight the important parts in RED font color. 

This is the only important part. By the slider_position value set the CV_CAP_PROP_POS_FRAMES value.
cap.set(CV_CAP_PROP_POS_FRAMES, slider_position);

The retrieved frame is the one on the slider position.
cap.retrieve

That's it.



opencv videocapture playback fast frame access

CV_CAP_PROP_POS_FRAMES Code here 



#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include <vector>
#include <stdio.h>
#include <Windows.h>
#include <iostream>


using namespace cv;
using namespace std;


int frame_max = 100;
int slider_position;
int slider_val;


void on_trackbar(int, void*)
{
 // I lost something? no
}

int main(int argc, const char** argv)
{

 
 VideoCapture cap("GO.MP4");
 int counter = 10;
 frame_max = cap.get(CAP_PROP_FRAME_COUNT);


 for (;;)
 {

  bool Is = cap.grab();
  if (Is == false) {

   cout << "Video Capture Fail" << endl;
   break;
  }
  else {
   
   cap.set(CV_CAP_PROP_POS_FRAMES, slider_position);
   
   Mat img;
   Mat original;
   cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);
   
   img.copyTo(original);
   resize(img, img, Size(640, 480));
     
   namedWindow("prew", WINDOW_AUTOSIZE);
   imshow("prew", img);
   createTrackbar("rew", "prew", &slider_position, frame_max, on_trackbar);
   on_trackbar(slider_position, 0);

   int key1 = waitKey(20);

  }
 }
}
Next Post Previous Post
No Comment
Add Comment
comment url