Skip to content

Instantly share code, notes, and snippets.

@itsrifat
Last active July 13, 2023 18:48
Show Gist options
  • Save itsrifat/66b253db2736b23f247c to your computer and use it in GitHub Desktop.
Save itsrifat/66b253db2736b23f247c to your computer and use it in GitHub Desktop.

Revisions

  1. itsrifat revised this gist Nov 11, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions extract_frames.cpp
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,11 @@ This functions opens a video file and extracts the frames and put them into a ve
    void extract_frames(const string &videoFilePath,vector<Mat>& frames){

    try{
    //open the video file
    //open the video file
    VideoCapture cap(videoFilePath); // open the video file
    if(!cap.isOpened()) // check if we succeeded
    return -1;
    CV_Error(CV_StsError, "Can not open Video file");

    //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++)
    {
    @@ -18,8 +18,8 @@ void extract_frames(const string &videoFilePath,vector<Mat>& frames){
    }
    }
    catch( cv::Exception& e ){
    const char* err_msg = e.what();
    std::cout << "exception caught: " << err_msg << std::endl;
    cerr << e.msg << endl;
    exit(1);
    }

    }
  2. itsrifat revised this gist Nov 7, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion extract_frames.cpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /*
    This functions opens a video file and extracts the frames and put them into a vector of Mat(its the class for representing an img)
    */
    void extract_frames(const string &videoFilePath,vector<Mat>& frames){

    try{
    @@ -21,7 +24,9 @@ void extract_frames(const string &videoFilePath,vector<Mat>& frames){

    }


    /*
    It saves a vector of frames into jpg images into the outputDir as 1.jpg,2.jpg etc where 1,2 etc represents the frame number
    */
    void save_frames(vector<Mat>& frames, const string& outputDir){
    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
  3. itsrifat revised this gist Nov 7, 2014. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions extract_frames.cpp
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,18 @@ void extract_frames(const string &videoFilePath,vector<Mat>& frames){
    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);
    }


    }
  4. itsrifat created this gist Nov 7, 2014.
    22 changes: 22 additions & 0 deletions extract_frames.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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;
    }

    }