Playback video in the window with the fps from the file metainfo, filename passed as 2'nd cmd argument:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
if(argc > 1) { // application will wait videofilename as the second cmd argument
VideoCapture _vc;
if(_vc.open(argv[1]) == false)
return -2; // Indicates that file could not be opened for the some reasons
double fps = _vc.get(CV_CAP_PROP_FPS); // get fps from the file's metainfo
int delayms = static_cast<int>(1000.0/fps); // get delay between the frames in milliseconds
Mat matframe;
while(_vc.read(matframe)){
imshow("viewport", matframe);
if(waitKey(delayms) == 27) { // 27 is ESCAPE code, so if the user press escape then while will be breaked
break;
}
}
return 0;
} else {
return -1; // means that the user did not provide videofilename
}
}
You want take into account processing time? So, let's slightly change the code:
#include <opencv2/opencv.hpp>
using namespace cv;
double measureframetimems(VideoCapture &_vc, cv::Mat(*_functionpointer)(const Mat &_mat), unsigned int _n) {
if(_vc.isOpened() == false) {
return -1.0;
}
cv::Mat _framemat;
double _totalprocctime = 0.0;
double _timemark;
unsigned int _frames = 0;
for(unsigned int i = 0; i < _n; ++i) {
if(_vc.read(_framemat)) {
_functionpointer(_framemat);
if(i > 0) { // let's drop first frame from the time count
_totalprocctime += (getTickCount() - _timemark)/getTickFrequency();
_frames++;
}
_timemark = getTickCount();
} else {
break; // for the instance all frames in the video file could be finished before _n
}
}
return 1000.0*_totalprocctime/_frames; // in milliseconds
}
cv::Mat processframe(const Mat &_mat) {
// Paste target processing code
return _mat;
}
int main(int argc, char *argv[])
{
if(argc > 1) { // application will wait videofilename as the second cmd argument
VideoCapture _vc;
if(_vc.open(argv[1]) == false)
return -2; // Indicates that file could not be opened for the some reasons
double fps = _vc.get(CV_CAP_PROP_FPS); // get fps from the file's metainfo
int delayms = static_cast<int>(1000.0/fps); // get delay between the frames in milliseconds
delayms -= measureframetimems(_vc, *processframe, 100); // adjust delay
// Reopen video file if first frames are necessary
if(_vc.open(argv[1]) == false)
return -2; // Indicates that file could not be opened for the some reasons
Mat matframe;
while(_vc.read(matframe)){
matframe = processframe(matframe);
imshow("viewport", matframe);
if(waitKey(delayms) == 27) { // 27 is ESCAPE code, so if the user press escape then while will be breaked
break;
}
}
return 0;
} else {
return -1; // means that the user did not provide videofilename
}
}