OpenCV #002 Read, Write and Display Video
Digital Image Processing using OpenCV (Python & C++)
Highlight: In this blog post we will explain how to read, display and save videos using OpenCV. We will give a code both in Python and C++.
Tutorial Overview:
1. Reading a video
Reading a video frame and displaying it, is not as difficult, as one might think. Using OpenCV it can be done in two ways, either by reading a video file or using the video stream directly from a camera connected to your computer. The first approach towards reading a video file is creating a VideoCapture Object and we need to specify the video filename. The second approach needs cameras parameters to access its real-time video stream.
Let’s imagine that you want to read a video from your camera. As everyone has most likely one camera, it will be indexed with ‘0’ and if more ‘1’, ‘2’ and so on. To make this more clear the code snippet below would clarify any doubts.
Python
C++
// Create a VideoCapture object and pass 0
VideoCapture cap(0);
Code language: JavaScript (javascript)
On the other hand, when we want to load an mp4 file, the parameter you are going to pass in is the absolute or relative path to the file.
In the following lines, you are going to see how to work with Python and C++, when working with video files.
Python
C++
// Create a VideoCapture object and open the input file
VideoCapture cap("game_of_throne_trailer.mp4");
Code language: JavaScript (javascript)
Now, after the VideoCapture object is created, a video can be captured frame by frame.
2. Displaying a video
When working with video files, reading a video file or capturing live stream is not enough. We also want to process the video and display it. To display a video in an open window, OpenCV provides a very simple interface to do this, using the command cv2.imshow in Python or cv::imshow in C++.
Python
C++
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
// Create a VideoCapture object and open the input file
VideoCapture cap("game_of_throne_trailer.mp4");
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c=(char)waitKey(25);
if(c==27)
break;
}
// When everything done, release the video capture object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
Code language: PHP (php)
3. Writing a video
As the next step, we want to save the captured frames as a video file. First, in order to be able to write our file, a Video Writer object must be created. This object takes as input parameters the output file name. It is worth mentioning that Fourcc (4-character code of codec) is used to compress the frames. More details related to this code type you can find here. Another parameter when we save video files is frame per second (fps), which controls how many frames you want to show per second in the output video. Finally, the frame size is also passed, which the width and height have been denoted. Implementations both in Python and C++ are below.
Python
C++
// Define the codec and create VideoWriter object.The output is stored in 'output.avi' file.
// Define the fps to be equal to 10. Also frame size is passed.
VideoWriter video("output_cpp.avi", CV_FOURCC('M','J','P','G'),10, Size(frame_width,frame_height));
// For Windows users use "cv::VideoWritter::fourcc" instead of "CV_FOURCC"
Code language: PHP (php)
So now we will merge everything into one big script.
Python
C++
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
// Create a VideoCapture object and use camera to capture the video
VideoCapture cap(0);
// Check if camera opened successfully
if(!cap.isOpened())
{
cout << "Error opening video stream" << endl;
return -1;
}
// Default resolution of the frame is obtained.The default resolution is system dependent.
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
// Define the codec and create VideoWriter object.The output is stored in 'output_cpp.avi' file.
VideoWriter video("output_cpp.avi",CV_FOURCC('M','J','P','G'),10, Size(frame_width,frame_height)); // For Windows users use "cv::VideoWritter::fourcc" instead of "CV_FOURCC"
while(1)
{
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Write the frame into the file 'output_cpp.avi'
video.write(frame);
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c = (char)waitKey(1);
if( c == 27 )
break;
}
// When everything done, release the video capture and write object
cap.release();
video.release();
// Closes all the windows
destroyAllWindows();
return 0;
}
Code language: PHP (php)
Summary
Congrats! In this post, we have successfully completed how to read, display and write a video file. In the next tutorial, we will focus on other image processing techniques.