OpenCV gets each frame of the video and saves it as a.jpg image

  • 2020-06-19 11:12:16
  • OfStack

The example of this paper shares the specific code of OpenCV getting every frame of the video and saving it as an image for your reference. The specific content is as follows


#include<opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>
#include<fstream> 

using namespace std;
using namespace cv;

// Get the face in the video 

int main()
{
 // Open the video file: This is actually the creation 1 a VideoCapture structure  
 VideoCapture capture("1.mp4");
 // Check if it is open normally : When you open it successfully, isOpened return ture 
 if (!capture.isOpened())
 cout << "fail toopen!" << endl;

 // Gets the entire frame number  
 long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
 cout << " The whole video is about " << totalFrameNumber << " frame " << endl;
 // Set the start frame () 
 long frameToStart = 1;
 capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);
 cout << " From the first " << frameToStart << " The frame to start reading " << endl;


 // Set end frame  
 int frameToStop = 30;

 if (frameToStop < frameToStart)
 {
 cout << " End frame less than start frame, program error, about to exit! " << endl;
 return -1;
 }
 else
 {
 cout << " The end frame is: number " << frameToStop << " frame " << endl;
 }

 // For frame rate  
 double rate = capture.get(CV_CAP_PROP_FPS);
 cout << " Frame rate for :" << rate << endl;
 // define 1 Three variables that control the end of the read video loop  
 bool stop = false;

 // Bearing each 1 The frame of the image  
 Mat frame;

 // According to each 1 The window of the frame  
 namedWindow("Extractedframe");

 // The time between frames : 
 //int delay = 1000/rate; 
 double delay = 1000 / rate;


 // using while Cyclic read frame  
 //currentFrame Is the variable in the body of the loop that controls the end of the loop after the specified frame is read  
 long currentFrame = frameToStart;


 // Filter kernel  
 int kernel_size = 3;
 Mat kernel = Mat::ones(kernel_size, kernel_size, CV_32F) / (float)(kernel_size*kernel_size);

 while (!stop)
 {
 // Read the 1 frame  
 if (!capture.read(frame))
 {
  cout << " Failed to read video " << endl;
  return -1;
 }


 cout << " Reading control " << currentFrame << " frame " << endl;
 imshow("Extractedframe", frame);

 cout << " Is writing the first " << currentFrame << " frame " << endl;
 stringstream str;
 str << currentFrame << ".jpg";
 cout << str.str() << endl;
 imwrite(str.str(), frame);

 //waitKey(intdelay=0) when delay Or less  0 Will wait forever; when delay>0 When waiting for delay ms  
 // When no key is pressed before the end of time, the return value is -1 ; Otherwise go back to the button  
 //int c = waitKey(delay);
 int c = waitKey(1000);
 // Press the ESC Or at the end of the specified frame exit to read the video  
 if ((char)c == 27 || currentFrame > frameToStop)
 {
  stop = true;
 }
 // After pressing the button, it will stay in the current frame and wait for the next frame 1 A key  
 if (c >= 0)
 {
  waitKey(0);
 }
 currentFrame++;

 }
 // Close the video file  
 capture.release();
 waitKey(0);
 return 0;
}

Related articles: