在OpenCV中对视频的读写操作与图像的读写操作非常相似。视频不过是一系列通常被称为帧的图像。所以,所需要做的就是在视频序列中的所有帧上循环,然后一次处理一帧。
接下来研读下:
002_reading_writing_videos是OpenCV读写、显示视频文件例程。
确认OpenCV安装路径:
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake
$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_file$ tree .
.
├── CMakeLists.txt
├── Resources
│ └── Cars.mp4
└── video_read_from_file.cpp
1 directory, 3 files
C++应用Demo工程编译执行:
$ cd video_read_from_file
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_from_file
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py
2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_file.py
C++:
# Create a video capture object, in this case we are reading the video from a file
VideoCapture vid_capture("Resources/Cars.mp4");
Python:
# Create a video capture object, in this case we are reading the video from a file
vid_capture = cv2.VideoCapture('Resources/Cars.mp4')
C++:
if (!vid_capture.isOpened())
{
cout << "Error opening video stream or file" << endl;
}
else
{
// Obtain fps and frame count by get() method and print
int fps = vid_capture.get(5):
cout << "Frames per second :" << fps;
frame_count = vid_capture.get(7);
cout << "Frame count :" << frame_count;
}
Python:
if (vid_capture.isOpened() == False):
print("Error opening the video file")
else:
# Get frame rate information
fps = int(vid_capture.get(5))
print("Frame Rate : ",fps,"frames per second")
# Get frame count
frame_count = vid_capture.get(7)
print("Frame count : ", frame_count)
C++:
while (vid_capture.isOpened())
{
// Initialize frame matrix
Mat frame;
// Initialize a boolean to check if frames are there or not
bool isSuccess = vid_capture.read(frame);
// If frames are present, show it
if(isSuccess == true)
{
//display frames
imshow("Frame", frame);
}
// If frames are not there, close it
if (isSuccess == false)
{
cout << "Video camera is disconnected" << endl;
break;
}
//wait 20 ms between successive frames and break the loop if key q is pressed
int key = waitKey(20);
if (key == 'q')
{
cout << "q key is pressed by the user. Stopping the video" << endl;
break;
}
}
Python:
while(vid_capture.isOpened()):
# vCapture.read() methods returns a tuple, first element is a bool
# and the second is frame
ret, frame = vid_capture.read()
if ret == True:
cv2.imshow('Frame',frame)
k = cv2.waitKey(20)
# 113 is ASCII code for q key
if k == 113:
break
else:
break
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_image_sequence$ tree . -L 2
.
├── CMakeLists.txt
├── Resources
│ └── Image_Sequence
└── video_read_from_image_sequence.cpp
2 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_read_from_image_sequence
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_is
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py
2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_image_sequence.py
读取系列照片文件
C++:
VideoCapture vid_capture("Resources/Image_sequence/Cars%04d.jpg");
Python:
vid_capture = cv2.VideoCapture('Resources/Image_sequence/Cars%04d.jpg')
注:Cars%04d.jpg: Cars0001.jpg, Cars0002.jpg, Cars0003.jpg, etc
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_read_from_webcam$ tree .
.
├── CMakeLists.txt
└── video_read_from_webcam.cpp
0 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_read_from_webcam
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_read_from_webcam
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py
2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_read_from_webcam.py
C++:
VideoCapture vid_capture(0);
Python:
vid_capture = cv2.VideoCapture(0)
注:cv2.CAP_DSHOW不要使用,有时会导致vid_capture.isOpened()
返回false。
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_write_from_webcam$ tree .
.
├── CMakeLists.txt
└── video_write_from_webcam.cpp
0 directories, 2 files
C++应用Demo工程编译执行:
$ cd video_write_from_webcam
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_write_from_webcam
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py
2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_write_from_webcam.py
C++:
// Obtain frame size information using get() method
Int frame_width = static_cast(vid_capture.get(3));
int frame_height = static_cast(vid_capture.get(4));
Size frame_size(frame_width, frame_height);
int fps = 20;
Python:
# Obtain frame size information using get() method
frame_width = int(vid_capture.get(3))
frame_height = int(vid_capture.get(4))
frame_size = (frame_width,frame_height)
fps = 20
- filename: pathname for the output video file
- apiPreference: API backends identifier
- fourcc: 4-character code of codec, used to compress the frames fourcc
- fps: Frame rate of the created video stream
- frame_size: Size of the video frames
- isColor: If not zero, the encoder will expect and encode color frames. Else it will work with grayscale frames (the flag is currently supported on Windows only).
C++:
//Initialize video writer object
VideoWriter output("Resources/output.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'),frames_per_second, frame_size);
Python:
# Initialize video writer object
output = cv2.VideoWriter('Resources/output_video_from_file.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 20, frame_size)
保存视频文件格式可以选择:
- AVI: cv2.VideoWriter_fourcc(‘M’,‘J’,‘P’,‘G’)
- MP4: cv2.VideoWriter_fourcc(*‘XVID’)
C++:
while (vid_capture.isOpened())
{
// Initialize frame matrix
Mat frame;
// Initialize a boolean to check if frames are there or not
bool isSuccess = vid_capture.read(frame);
// If frames are not there, close it
if (isSuccess == false)
{
cout << "Stream disconnected" << endl;
break;
}
// If frames are present
if(isSuccess == true)
{
//display frames
output.write(frame);
// display frames
imshow("Frame", frame);
// wait for 20 ms between successive frames and break
// the loop if key q is pressed
int key = waitKey(20);
if (key == ‘q’)
{
cout << "Key q key is pressed by the user.
Stopping the video" << endl;
break;
}
}
}
Python:
while(vid_capture.isOpened()):
# vid_capture.read() methods returns a tuple, first element is a bool
# and the second is frame
ret, frame = vid_capture.read()
if ret == True:
# Write the frame to the output files
output.write(frame)
else:
print(‘Stream disconnected’)
break
C++应用Demo工程结构:
002_reading_writing_videos/CPP/video_write_to_file$ tree -L 2
.
├── CMakeLists.txt
├── Resources
│ └── Cars.mp4
└── video_write_to_file.cpp
1 directory, 4 files
C++应用Demo工程编译执行:
$ cd video_write_to_file
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/video_write_to_file
Python应用Demo工程结构:
002_reading_writing_videos/Python$ tree . -L 2
.
├── requirements.txt
├── Resources
│ ├── Cars.mp4
│ └── Image_sequence
├── video_read_from_file.py
├── video_read_from_image_sequence.py
├── video_read_from_webcam.py
├── video_write_from_webcam.py
└── video_write_to_file.py
2 directories, 7 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python video_write_to_file.py
整合了以下章节重点过程:
主要通过以下三个函数API实现:
其他API函数:
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装