Last active
July 13, 2023 18:48
-
-
Save itsrifat/66b253db2736b23f247c to your computer and use it in GitHub Desktop.
simple c++ functions to extract frames of a video file into a vector of Mat and saving the vector as jpg images using OpenCV 2.4.9
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
| void extract_frames(const string &videoFilePath,vector<Mat>& frames){ | |
| try{ | |
| //open the video file | |
| VideoCapture cap(videoFilePath); // open the video file | |
| if(!cap.isOpened()) // check if we succeeded | |
| return -1; | |
| //cap.get(CV_CAP_PROP_FRAME_COUNT) contains the number of frames in the video; | |
| for(int frameNum = 0; frameNum < cap.get(CV_CAP_PROP_FRAME_COUNT);frameNum++) | |
| { | |
| Mat frame; | |
| cap >> frame; // get the next frame from video | |
| frames.push_back(frame); | |
| } | |
| } | |
| catch( cv::Exception& e ){ | |
| const char* err_msg = e.what(); | |
| std::cout << "exception caught: " << err_msg << std::endl; | |
| } | |
| } | |
| void save_frames(vector<Mat>& frames, const string& outputDir){ | |
| vector<int> compression_params; | |
| compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); | |
| compression_params.push_back(100); | |
| for(std::vector<Mat>::iterator frame = frames.begin(),frameNumber=0; frame != frame.end(); ++frame){ | |
| string filePath = outputDir + to_string(static_cast<long long>(frameNumber))+ ".jpg"; | |
| imwrite(filePath,*frame,compression_params); | |
| } | |
| } |
what do I need to #include to get this working?
OpenCV library header files to see imwrite method and others, like #include "opencv2/core.hpp"
#include "opencv2/video.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp" and STL containers like vector #include
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to call the function: