Previous article describes OpenCV for image processing methods, but the current processing and analysis of the video is increasingly becoming the mainstream of computer vision, the amount of information included in the video is much larger than the picture, but in essence is a video frame image composition, so the video processing ultimately it comes down to image processing, but in video processing, more information on the time dimension can be utilized. This paper describes OpenCV in dealing with some of the basic functions of video
1, the video read ( CV :: VIdeoCpature )
Video read, the main advantage VideoCapture method in the class and get open the video frames in the video, which have a lot of operation, only the most common usage mentioned herein
Specific Visible: https://docs.opencv.org/3.1.0/d8/dfe/classcv_1_1VideoCapture.html
1.1 constructor takes video files
// In this constructor, the file path inputted to the correct video file, the OpenCV will open the corresponding video file
cv::VideoCapture::VideoCapture(const string& filename);
There are two ways to open the video file
//方法一
VideoCapture cap("E:/myFile/video/ty.mp4");//获取E:/myFile/video路径下的ty.mp4视频文件
//方法二
VideoCapture cap;
cap.open("E:/myFile/video/ty.mp4");//使用open方法
// In this constructor, the input camera ID, corresponding to the camera can be opened, the default camera 0
cv::VideoCapture::VideoCapture(int device);
VideoCapture cap(0);//读取默认摄像头
1.2 verify whether the video reading success
If the video file is successfully read back True, False otherwise
if (!cap.isOpened())
{
cout << "无法打开视频文件!" << endl;
return -1;
}
1.3 acquire video frame
There are a variety of video frame acquisition method
Mat frame;
//方法一
capture.read(frame);
//方法二
capture.grab();
capture.retrieve(frame);
//方法三
capture>>frame;
1.4 Video acquisition parameters
There are a lot of video parameters, such as: a frame rate, total number of frames, size, format, VideoCapture GET methods may get a lot of parameters
Parameter table as shown below
parameter |
The corresponding macro | Explanation |
VideoCapture.get(0) | CV_CAP_PROP_POS_MSEC | The current location of the video file (play) in milliseconds |
VideoCapture.get(1) | CV_CAP_PROP_POS_FRAMES | Based on the starting frame index 0 to be captured or decoded |
VideoCapture.get(2) | CV_CAP_PROP_POS_AVI_RATIO | 0 = end of the movie to start, 1 = movie: the relative position of the video file (play) |
VideoCapture.get(3) | CV_CAP_PROP_FRAME_WIDTH | The width of the frame of the video stream |
VideoCapture.get(4) | CV_CAP_PROP_FRAME_HEIGHT | The height of the frames of the video stream |
VideoCapture.get(5) | CV_CAP_PROP_FPS | Frame rate / frames / FPS |
VideoCapture.get(6) | CV_CAP_PROP_FOURCC | 4 codec word - character code |
VideoCapture.get(7) | CV_CAP_PROP_FRAME_COUNT | The number of frames in the video file |
VideoCapture.get(8) | CV_CAP_PROP_FORMAT | Returns the object format |
VideoCapture.get(9) | CV_CAP_PROP_MODE | Returns rear specific value indicating the current capturing mode |
VideoCapture.get(10) | CV_CAP_PROP_BRIGHTNESS | Brightness of the image (only for cameras) |
VideoCapture.get(11) | CV_CAP_PROP_CONTRAST | Contrast of the image (only for cameras) |
VideoCapture.get(12) | CV_CAP_PROP_SATURATION | Saturation of the image (only for cameras) |
VideoCapture.get(13) | CV_CAP_PROP_HUE | Tone image (only for cameras) |
VideoCapture.get(14) | CV_CAP_PROP_GAIN | Image gain (only for cameras) (Gain indicate that white balance to enhance the photography) |
VideoCapture.get(15) | CV_CAP_PROP_EXPOSURE | Exposure (only for cameras) |
VideoCapture.get(16) | CV_CAP_PROP_CONVERT_RGB | It indicates whether the image should be converted to RGB Boolean flag |
VideoCapture.get(17) | CV_CAP_PROP_WHITE_BALANCE | × is not supported |
VideoCapture.get(18) | CV_CAP_PROP_RECTIFICATION | Correction marked stereo camera (currently only DC1394 v.2.x back-end support this feature) |
The example shown below
1.5 setting the reading position of the video frame
The method set VideoCapture class allows us removed video frame to a location
It has some parameters, by time, by the frame number may be also possible according to the length ratio of the video
//第100帧
double position=100.0;
capture.set(CV_CAP_PROP_POS_FRAMES,position);
//第1e6毫秒
double position=1e6;
capture.set(CV_CAP_PROP_POS_MSEC,position);
//视频1/2位置
double position=0.5;
capture.set(CV_CAP_PROP_POS_AVI_RATIO,position);
The method may also be provided at the same time set the frame rate of the video, luminance
2, the video writing ( CV :: VIdeoWriter )
Similar video write and read, using VideoWriter classes to implement, there are several ways this class, in addition to constructor provides open, IsOpen, write, and overloaded operators <<
cv::VideoWriter(const string& path,int fourcc,double fps, Size framesize, bool isColor=true)
- The first parameter represents the saved file path and file name
- The second parameter indicates a coding format
- The third parameter indicates the frame rate
- The fourth parameter represents a size of the video
- The fifth parameter indicates the type of the video image (true color, false gradation)
Note that fourcc common format
- CV_FOURCC('P','I','M','1') = MPEG-1 codec
- CV_FOURCC('M','J','P','G') = motion-jpeg codec
- CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
- CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
- CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
- CV_FOURCC('U', '2', '6', '3') = H263 codec
- CV_FOURCC('I', '2', '6', '3') = H263I codec
- CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
3、视频读写程序示例
视频读取
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap("E:/myFile/video/ty.mp4");
Mat frame;
if (!cap.isOpened())
{
cout << "无法打开视频文件!" << endl;
return -1;
}
namedWindow("video", CV_WINDOW_AUTOSIZE);
while (cap.read(frame))
{
imshow("video", frame);
waitKey(10);
}
cap.release();
waitKey(0);
return 0;
}
视频写入
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap("E:/myFile/video/ty.mp4");
Mat frame, gray;
if (!cap.isOpened())
{
cout << "无法打开视频文件!" << endl;
return -1;
}
//获取视频帧的长和宽
Size size = Size(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter writer("E:/myFile/video/ty_g.mp4", CV_FOURCC('M', 'J', 'P', 'G'), 24, size, true);
namedWindow("video", CV_WINDOW_AUTOSIZE);
while (cap.read(frame))
{
//imshow("video", frame);
//转换为黑白图像
cvtColor(frame, gray, COLOR_BGR2GRAY);
//二值化处理
threshold(gray, gray, 0, 255, THRESH_BINARY | THRESH_OTSU);
cvtColor(gray, gray, COLOR_GRAY2BGR);
imshow("video", gray);
writer.write(gray);
waitKey(10);
}
waitKey(0);
cap.release();
return 0;
}