版权声明:本文为 @iCurious
的原创文章,可以转载,但请务必注明作者和出处!!!
原文链接:|blog.csdn.net/icurious|www.blankspace.cn|www.cnblogs.com/icurious/|
摘要
本文主要介绍如何使用OpenCV打开摄像头并测试,由于只是测试,故除了程序中的注释外,不做更多的解释。
#include <iostream>
#include <opencv2/opencv.hpp>
#include<opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat in_frame, out_frame;
const char win1[] = "Grabbing......", win2[] = "Recording......";
double fps = 50;//每秒的帧数
char file_out[] = "Recorded.avi" ;
VideoCapture inVid(0);//打开默认摄像机
if (!inVid.isOpened())//检查错误
{
cout << "发生错误,摄像机无法打开!" << endl;
return -1;
}
//获取视频的宽度和高度
int width = (int )inVid.get(CAP_PROP_FRAME_WIDTH);
int height = (int )inVid.get(CAP_PROP_FRAME_HEIGHT);
VideoWriter recVid(file_out, VideoWriter ::fourcc('M', 'S', 'V', 'C'), fps, Size(width, height));
if (!recVid.isOpened())
{
cout << "发生错误,视频文件无法打开!" << endl;
return - 1;
}
//为原始视频和最终视频创建两个窗口
namedWindow(win1);
namedWindow(win2);
while (true )
{
//从摄像机读取帧(抓取并解码)
inVid >> in_frame;
//将帧转换为灰度
cvtColor(in_frame, out_frame, COLOR_BGR2GRAY);
//将帧写入视频文件(编码并保存)
recVid << out_frame;
imshow(win1, in_frame);
imshow(win2, out_frame); //在窗口中显示帧
if (waitKey(1000/fps) >= 0)
break;
}
inVid.release(); //关闭摄像机
return 0;
}
效果
版权声明:本文为 @iCurious
的原创文章,可以转载,但请务必注明作者和出处!!!
原文链接:|blog.csdn.net/icurious|www.blankspace.cn|www.cnblogs.com/icurious/|