O p e n C V 进 行 视 频 读 写 OpenCV进行视频读写 OpenCV

1.视频教程:
B站、网易云课堂、腾讯课堂
2.代码地址:
Gitee
Github
3.存储地址:
Google云
百度云:
提取码:

VideoCapture capture()可以获取1.摄像头、2.视频文件、3.网络视频地址链接


#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

	VideoCapture capture(0);
	if (!capture.isOpened()) {
		printf("could not open the camera...\n");
	}
	namedWindow("frame", WINDOW_AUTOSIZE);
	Mat frame;
	while (true) {
		bool ret = capture.read(frame); // >> 读  <<:写
		if (!ret) break;
		imshow("frame", frame);
		char c = waitKey(50);
		if (c == 27) {//ESC
			break;
		}
	}


	waitKey(0);
	destroyAllWindows();
	return 0;
}

OpenCV进行视频读写_ide

2.读取视频

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

	VideoCapture capture("C:/Users/29939/Desktop/work/test.mp4");
	if (!capture.isOpened()) {
		printf("could not open the camera...\n");
	}
	namedWindow("frame", WINDOW_AUTOSIZE);
	Mat frame;
	while (true) {
		bool ret = capture.read(frame); // >> 读  <<:写
		if (!ret) break;
		imshow("frame", frame);
		char c = waitKey(50);
		if (c == 27) {//ESC
			break;
		}
	}


	waitKey(0);
	destroyAllWindows();
	return 0;
}




OpenCV进行视频读写_ide_02

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

	VideoCapture capture("C:/Users/29939/Desktop/work/test.mp4");
	if (!capture.isOpened()) {
		printf("could not open the camera...\n");
	}
	namedWindow("frame", WINDOW_AUTOSIZE);
	int fps = capture.get(CAP_PROP_FPS);
	int width = capture.get(CAP_PROP_FRAME_WIDTH);
	int height = capture.get(CAP_PROP_FRAME_HEIGHT);
	int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT);
	printf("frame size(w=%d,h=%d),FPS:%d,count:%d", width, height, fps, num_of_frames);
	Mat frame;
	while (true) {
		bool ret = capture.read(frame); // >> 读  <<:写
		if (!ret) break;
		imshow("frame", frame);
		char c = waitKey(50);
		if (c == 27) {//ESC
			break;
		}
	}
	waitKey(0);
	destroyAllWindows();
	return 0;
}




OpenCV进行视频读写_#include_03

3.视频保存

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

	VideoCapture capture("E:/software/opecv4.5.1/opencv/sources/samples/data/vtest.avi");
	if (!capture.isOpened()) {
		printf("could not open the camera...\n");
	}
	namedWindow("frame", WINDOW_AUTOSIZE);
	int fps = capture.get(CAP_PROP_FPS);
	int width = capture.get(CAP_PROP_FRAME_WIDTH);
	int height = capture.get(CAP_PROP_FRAME_HEIGHT);
	int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT);
	int type = capture.get(CAP_PROP_FOURCC);
	printf("frame size(w=%d,h=%d),FPS:%d,count:%d", width, height, fps, num_of_frames);
	Mat frame;
	VideoWriter writer("E:/test001.mp4", type, fps, Size(width, height), true);
	while (true) {
		bool ret = capture.read(frame); // >> 读  <<:写
		if (!ret) break;
		imshow("frame", frame);
		writer.write(frame);
		char c = waitKey(50);
		if (c == 27) {//ESC
			break;
		}
	}

	capture.release();
	writer.release();

	waitKey(0);
	destroyAllWindows();
	return 0;
}





OpenCV进行视频读写_ios_04

保存大小限制,在2G左右