2021-08-18opencv-python视频入门

视频读入:VideoCapture

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

using namespace cv;
using namespace std;

int main() {
	VideoCapture  capture;
	Mat frame;
	frame = capture.open(0);//当0位文件时,可以读取视频文件
	if (!capture.isOpened()) 
	{
		printf("could not find the video");
		return -1;
	}
	namedWindow("video-demo",WINDOW_AUTOSIZE);
	while (capture.read(frame)) 
	{
		imshow("video-demo",frame);	
		waitKey(20);
	}
	
	capture.release();
	return 0;
}

可以使用cap.get(propId)方法访问该视频的某些功能,其中propId是0到18之间的一个数字。每个数字表示视频的属性(如果适用于该视频),并且可以显示完整的详细信息在这里看到:
cv::VideoCapture::get()。其中一些值可以使用cap.set(propId,value)进行修改。value是你想要的新值。

VideoWriter

1.编码格式:

视频常见的编码方式通常有: x264、h264、mpeg-4

音频常见的编码方式通常有: mp3、AAC、flac

编码的目的主要是为了高效存储和传输,如果你不采用编码压缩的话,那么视频可以看做是一系列的图片序列,体积会非常大

VideoWriter(filename, fourcc, fps, frameSize[, isColor])

第一个参数是要保存的文件的路径
fourcc 指定编码器
fps 要保存的视频的帧率
frameSize 要保存的文件的画面尺寸
isColor 指示是黑白画面还是彩色的画面

fourcc 本身是一个 32 位的无符号数值,用 4 个字母表示采用的编码器。 常用的有 “DIVX"、”MJPG"、“XVID”、“X264"

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

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	//VideoCapture capture;
	//capture.open("D:/vcprojects/images/video_006.mp4");
	VideoCapture capture(0);
	if (!capture.isOpened()) {
		printf("could not load video data...\n");
		return -1;
	}
	// 获取帧的属性
	double fps = capture.get(CAP_PROP_FPS);
	Size size = Size(capture.get(CAP_PROP_FRAME_WIDTH), capture.get(CAP_PROP_FRAME_HEIGHT));
	printf("FPS : %f", fps);
	VideoWriter writer("D:/wv_demo.mp4", CAP_OPENCV_MJPEG, 15.0, size, true);

	// create window
	Mat frame, gray, binary;
	namedWindow("video-demo", WINDOW_AUTOSIZE);

	// show each frame and save
	vector<Mat> bgr;
	while (capture.read(frame)) {
		//inRange(frame, Scalar(0, 127, 0), Scalar(127, 255, 127), gray);
		//cvtColor(frame, gray, COLOR_BGR2GRAY);
		//threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
		//bitwise_not(frame, frame);
		flip(frame, frame, 1);
		imshow("video-demo", frame);
		writer.write(frame);
		char c = waitKey(100);
		if (c == 27) {
			break;
		}
	}

	waitKey(0);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值