Last active
June 26, 2019 20:35
-
-
Save ank700/bcccab8a6d3561e980341f2dcac90f7f to your computer and use it in GitHub Desktop.
Timelapse using opencv 4.0 and c/c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
int main() | |
{ | |
cv::VideoCapture cap("water.mp4"); | |
if (!cap.isOpened()) | |
{ | |
std::cerr << "Cannot open video file. Check file path!\n"; | |
return -1; | |
} | |
// required parameters of the current video file | |
// get video file fps, total frames, video codec(fourcc) and frame size | |
double fps = cap.get(cv::CAP_PROP_FPS); | |
long int maxframes = (long int)cap.get(cv::CAP_PROP_FRAME_COUNT); | |
int fourCC = (int)cap.get(cv::CAP_PROP_FOURCC); | |
cv::Size framesz = cv::Size((int)cap.get(cv::CAP_PROP_FRAME_WIDTH), (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT)); | |
// set output video parameters | |
int outputFPS = 60; | |
int numOfFramesToSkip = 5; | |
std::string saveFileName = "water_tl.mp4"; | |
//std::cout << "FPS of actual video is: " << fps << "\n"; | |
long int counter = 0; | |
cv::Mat img; | |
cap.read(img); | |
// object of videowriter class to create a video from individual frames | |
cv::VideoWriter vidwriter; | |
if (img.channels() == 3) // bgr | |
vidwriter = cv::VideoWriter(saveFileName, cv::VideoWriter::fourcc('M', 'P', '4', 'V'), outputFPS, framesz, true); | |
else // gray scale | |
vidwriter = cv::VideoWriter(saveFileName, cv::VideoWriter::fourcc('M', 'P', '4', 'V'), outputFPS, framesz, false); | |
cv::Mat frame; | |
while (true) | |
{ | |
cap >> frame; | |
// if frame exists, write to video | |
if (!frame.empty()) | |
{ | |
if (counter % numOfFramesToSkip == 0) | |
{ | |
vidwriter.write(frame); | |
} | |
} | |
else | |
return 0; | |
counter++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment