Reading, processing and saving of opencv video files

Article directory

Reading, processing and saving of opencv video files

1. Reading of video files:
1. cv::VideoCapture is a class in the OpenCV library used to process video input. It provides a simple way to read frames from a camera, video file, or image sequence;
(1) Turn on the camera:
cv::VideoCapture capture(int index)

参数解释:
index:打开指定编号的摄像头,编号0表示默认摄像头,如果你的计算机连接了多个摄像头,你可以使用不同的编号来选择不同的摄像头;

(2) Open the video file:
cv::VideoCapture capture(const std::string& filename)

参数解释:
filename:打开指定的视频文件,你需要提供视频文件的路径和名称作为参数;

(3) Turn on the webcam:
cv::VideoCapture capture(const std::string& apiKey, const std::string& deviceId)

参数解释:
用于打开网络摄像头,需要提供相应的API密钥和设备ID;

(4) Open the image sequence:
cv::VideoCapture capture(const std::string& pattern, int api)

参数解释:
用于打开图像序列,需要提供文件名模式(通配符)和相应的API;

2. Common methods of cv::VideoCapture class:
(1) capture.get(): used to obtain video attributes, such as frame number, frame rate, etc.:
capture.get(int propId)

参数解释:
propId:是一个整数参数,用于指定你想获取的属性类型(
    	cv::CAP_PROP_FRAME_COUNT:获取视频的帧数;
    	cv::CAP_PROP_FPS:获取视频的帧率(每秒帧数);
	cv::CAP_PROP_FRAME_WIDTH:视频帧的宽度;
	cv::CAP_PROP_FRAME_HEIGHT:视频帧的高度;
)

Example:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 初始化一个cv::VideoCapture对象,打开视频文件
	VideoCapture capture("C:\\cpp\\image\\cayenne.mp4");
	if (!capture.isOpened()) {
    
    
		std::cerr << "Error: 无法打开视频文件." << std::endl;
		return -1;
	}
	// 调用cv::VideoCapture类的get()方法,获取视频宽度,高度,帧数,帧率
	int frameWidth = capture.get(cv::CAP_PROP_FRAME_WIDTH);
	int frameHeight = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
	int frameCount = capture.get(cv::CAP_PROP_FRAME_COUNT);
	double fps = capture.get(cv::CAP_PROP_FPS);

	std::cout << "frame width:" << frameWidth << std::endl;
	std::cout << "frame height:" << frameHeight << std::endl;
	std::cout << "Number of Frames:" << frameCount << std::endl;
	std::cout << "FPS:" << fps << std::endl;

	capture.release(); // 释放VideoCapture对象
}

(2) capture.set(int propId, double value): used to set the properties of the video, such as setting the frame rate, frame size and other properties of the video;
capture.set(int propId, double value)

参数解释:
propId:指定了你想设置的属性类型;
value:要设置的值;

Example:

cv::VideoCapture capture(0); // 打开默认摄像头

if (!capture.isOpened()) {
    
    
    std::cerr << "Error: 无法打开摄像头." << std::endl;
    return -1;
}

capture.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
capture.set(cv::CAP_PROP_FPS, 30);

capture.release(); // 释放VideoCapture对象

(3) capture.read(cv::Mat& frame): used to read a frame of the video and store it in the cv::Mat object;

Example:

cv::VideoCapture capture("video_file.mp4");

if (!capture.isOpened()) {
    
    
    std::cerr << "Error: 无法打开视频文件." << std::endl;
    return -1;
}

cv::Mat frame;
capture.read(frame);

// 在这里可以对 frame 进行处理

capture.release(); // 释放VideoCapture对象

(4) capture.release(): used to release the resources occupied by the VideoCapture object. It is generally called after you have completed the operation of the video stream;
2. Process the video frames:

Loop through each frame of the video, and each frame can be processed inside the loop. Here, I commented out the processing part. You can add various image processing operations as needed, such as filtering, edge detection, etc.;

Code example:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 打开视频文件
	VideoCapture capture("C:\\cpp\\image\\cayenne.mp4");

	// 检查视频是否成功打开
	if (!capture.isOpened()) {
    
    
		std::cerr << "Error: 无法打开视频文件." << std::endl;
		return -1;
	}

	// 获取视频的帧数和帧率
	int frameCount = capture.get(cv::CAP_PROP_FRAME_COUNT);
	double fps = capture.get(cv::CAP_PROP_FPS);

	// 创建一个VideoWriter对象来保存处理后的视频
	cv::VideoWriter outVideo("outVideo.mp4", capture.get(CAP_PROP_FOURCC), fps, cv::Size(capture.get(cv::CAP_PROP_FRAME_WIDTH), capture.get(cv::CAP_PROP_FRAME_HEIGHT)));

	// 循环处理视频的每一帧
	for (int i = 0; i < frameCount; ++i) {
    
    
		cv::Mat frame;
		capture >> frame; // 读取一帧

		if (frame.empty()) {
    
    
			break;
		}

		// 在这里可以对frame进行处理,比如进行滤波、边缘检测等
		// 例如:cv::cvtColor(frame, frame, cv::COLOR_BGR2GRAY);

		// 将处理后的帧写入输出视频文件
		outVideo << frame;
	}

	// 释放VideoCapture和VideoWriter对象
	capture.release();
	outVideo.release();

	std::cout << "视频处理完成." << std::endl;
}

3. Save the processed video:
1. cv::VideoWriter is a class in opencv used to save image frame sequences as video files. It can write the processed frames into a video file, or output the video stream to a camera or network;
(1) Create a cv::VideoWriter object:

Function prototype:

cv::VideoWriter writer(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true)

参数解释:
filename:要保存的视频文件名或者设备地址;
fourcc:FourCC编码,指定视频编解码器的类型(
	cv::VideoWriter::fourcc('X','V','I','D'):Xvid编解码器;
	cv::VideoWriter::fourcc('M','J','P','G'):MJPEG编解码器;
	cv::VideoWriter::fourcc('M','P','4','V'):MPEG-4编解码器;
	cv::VideoWriter::fourcc('H','2','6','4'):H.264编解码器;
);
fps:帧率,即每秒显示的帧数;
frameSize:帧的大小,可以通过cv::Size类指定;
isColor:指定保存的视频是否为彩色,默认为true
2. Common methods of cv::VideoWriter class:
(1) cv::VideoWriter::open(): used to open a video file or device to write video frames;
cv::VideoWriter::open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true)

参数解释:参数和构造函数类似;
filename:要保存的视频文件名或者设备地址;
fourcc:FourCC编码,指定视频编解码器的类型(
	cv::VideoWriter::fourcc('X','V','I','D'):Xvid编解码器;
	cv::VideoWriter::fourcc('M','J','P','G'):MJPEG编解码器;
	cv::VideoWriter::fourcc('M','P','4','V'):MPEG-4编解码器;
	cv::VideoWriter::fourcc('H','2','6','4'):H.264编解码器;
);
fps:帧率,即每秒显示的帧数;
frameSize:帧的大小,可以通过cv::Size类指定;
isColor:指定保存的视频是否为彩色,默认为true
(2) cv::VideoWriter::write(): used to write a frame of image to a video file;
cv::VideoWriter::write(const Mat& image)

参数解释:
image:要写入视频的帧,通常是一个cv::Mat对象;

(3) cv::VideoWriter::isOpened(): used to check whether the VideoWriter object is successfully opened. If the video file or device is successfully opened, this method will return true, otherwise it will return false;
(4) cv::VideoWriter::release(): used to release the resources occupied by the VideoWriter object. It is generally called after you have completed the writing operation to the video;

Example:

We first create a VideoWriter object, specifying the name of the video file, FourCC encoding, frame rate and frame size. Next, we check whether the file was successfully opened. We then created a red frame (640x480 sized solid red image) and wrote it to the video file 10 times. Finally, we free the VideoWriter object.

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	Demo demo;
	// 初始化一个cv::VideoCapture对象,打开视频文件
	VideoCapture capture("C:\\cpp\\image\\cayenne.mp4");
	// 调用cv::VideoCapture类的get()方法,获取视频宽度,高度,帧数,帧率
	int frame_width = capture.get(CAP_PROP_FRAME_WIDTH);
	int frame_height = capture.get(CAP_PROP_FRAME_HEIGHT);
	int count = capture.get(CAP_PROP_FRAME_COUNT);
	double fps = capture.get(CAP_PROP_FPS);
	std::cout << "frame width:" << frame_width << std::endl;
	std::cout << "frame height:" << frame_height << std::endl;
	std::cout << "FPS:" << fps << std::endl;
	std::cout << "Number of Frames:" << count << std::endl;
	// 初始化一个cv::VideoWriter 对象,保存处理后的视频文件
	// capture.get(CAP_PROP_FOURCC):获取原视频的编解码器
	// Size(frame_width, frame_height):原视频的宽高
	VideoWriter writer("C:\\cpp\\image\\test.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
	Mat frame;
	while (true) {
    
    
		// 读取视频的一帧,将其存储在frame对象中
		capture.read(frame);
		// TODO: do something...
		// 对读取到的这帧图像,做flip()翻转处理
		flip(frame, frame, 1);
		if (frame.empty()) {
    
    
			break;
		}
		// 显示这帧图像
		imshow("frame", frame);
		// 对这帧图像,做色彩空间转换
		demo.colorSpace(frame);
		// 将这帧图像写入视频文件
		writer.write(frame);

		int c = waitKey(1);
		if (c == 27) {
    
     // 退出
			break;
		}
	}

	// release
	capture.release();
	writer.release();
}

The demo.colorSpace() function for color space conversion is as follows:

void Demo::colorSpace(Mat &image) {
    
    

	Mat gray, hsv;
	// 转hsv
	cvtColor(image, hsv, COLOR_BGR2HSV);
	// 转灰度
	cvtColor(image, gray, COLOR_BGR2GRAY);

	// 显示这两张图
	imshow("HSV",hsv);
	imshow("GARY", gray);

	// 保存这两张图
	imwrite("C:\\cpp\\vs\\opencv\\hsv.png", hsv);
	imwrite("C:\\cpp\\vs\\opencv\\gray.png", gray);

}

Guess you like

Origin blog.csdn.net/qq_33867131/article/details/133344471