openCV::视频的加载/摄像头的调用

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

using namespace cv;
using namespace std;


int main()
{
	Mat frame;
	VideoCapture video(0);//打开摄像头(摄像头ID)
	if (!video.isOpened())//判断是否打开
	{
		cout << "请检查视频" << endl;
		return -1;

	}
	video >> frame;//把第一帧的图像传给Mat对象
	if (frame.empty())//判断摄像头是否获取到数据
	{
		cout << "没有获取到图像" << endl;
		return -1;
	}

	bool isColor = (frame.type() == CV_8UC3);
	VideoWriter write;
	int codec = VideoWriter::fourcc('M', 'J', 'P', 'G');
	double fps = 25.0;

	string filename = "D:/DownLoad/live.avi";

	write.open(filename, codec, fps, frame.size());//创建打开要存储的位置和文件

	if (!write.isOpened())//确认是否打开
	{
		cout << "打开视频文件失败" << endl;
		return -1;
	}
	video >> frame;//有时候不加这一句下面会判空,我也不知道为什么,原来的教学视频中没有这一句
	while (1)
	{
		
		if (!video.read(frame))//判断是否读到图像
		{
			cout << "摄像头打开错误" << endl;
			break;
		}
		
		
		write.write(frame);//写道打开的文件中
		imshow("live", frame);
		char c = waitKey(50);
//watkey函数显示显示一个窗口50ms,如果没有输入则返回-1,读取从键盘中输入的字符,如果为esc则退出

		if (c == 27)
		{
			break;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值