saving an image sequence from video using opencv2

前端 未结 3 1000
青春惊慌失措
青春惊慌失措 2021-01-15 16:48

Newbie question and yes I have spent a lot of time sifting through similar questions and Answers with no luck.

What I am trying to do is save frames from a video fil

相关标签:
3条回答
  • 2021-01-15 17:21

    This is my code... I tryed a lot and finally made it this is c++ using opencv 3... hope it works

    #include "opencv2/opencv.hpp"
    #include <sstream>
    #include <iostream>
    
    
    using namespace cv;
    using namespace std;
    
     Mat frame,img;
        int counter;
    
    int main(int,char**)
       {
            VideoCapture vid("video3.avi");
    
    
    while (!vid.isOpened())
    {
        VideoCapture vid("video2.MOV");
        cout << "charging" << endl;
        waitKey(1000);
    
    }
    
    cout << "Video opened!" << endl;
    
    while(1)
    {
        stringstream file;
    
        vid.read(frame);
        if(frame.empty()) break;
        file << "/home/pedro/workspace/videoFrame/Debug/frames/image" << counter << ".jpg";
        counter++;
        imwrite(file.str(),frame);
    
        char key = waitKey(10);
     if ( key == 27)
     {break;}
    
    
    }
    
    
    } 
    
    0 讨论(0)
  • 2021-01-15 17:24

    Use an index that will keep track of the number part in the filename. In the image capturing loop, add the index with the filename and build the final filename.

    here is an example :

    while(1)
    {
         cap.read ( frame);
         if( frame.empty()) break;
         imshow("video", frame);
         char filename[80];
         sprintf(filename,"C:/Users/cssc/Desktop/testFolder/test_%d.png",i);
         imwrite(filename, frame);
         i++;
         char key = waitKey(10);
         if ( key == 27) break;
    }
    
    0 讨论(0)
  • 2021-01-15 17:33

    This is my way to do in Python3.0. Have to have CV2 3+ version for it to work. This function saves images with frequency given.

    import cv2
    import os
    print(cv2.__version__)
    
    # Function to extract frames 
    def FrameCapture(path,frame_freq): 
    
        # Path to video file 
        video = cv2.VideoCapture(path) 
        success, image = video.read()
    
        # Number of frames in video
        fps = int(video.get(cv2.CAP_PROP_FPS))
        length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    
        print('FPS:', fps)
        print('Extracting every {} frames'.format(frame_freq))
        print('Total Frames:', length)
        print('Number of Frames Saved:', (length // frame_freq) + 1)
    
        # Directory for saved frames
        try:
            frame_dir = path.split('.')[0]
            os.mkdir(frame_dir)
        except FileExistsError:
            print('Directory ({}) already exists'.format(frame_dir))
    
        # Used as counter variable 
        count = 0
    
        # checks whether frames were extracted 
        success = 1
    
        # vidObj object calls read 
        # function extract frames 
    
        while count < length :
            video.set(cv2.CAP_PROP_POS_FRAMES , count)
            success, image = video.read()
            # Saves the frames with frame-count 
            cv2.imwrite(frame_dir + "/frame%d.jpg" % count, image)
            count = count + frame_freq
    
    0 讨论(0)
提交回复
热议问题