1、VideoCapture的常用属性
#region 常用属性
//_ = videoCapture.CaptureType; // 获取视频的捕获类型(文件或摄像头)
//_ = videoCapture.PosAviRatio; // 获取或设置视频的相对位置(0开头1结尾)
//_ = videoCapture.FourCC; // 获取或设置视频的编解码的4字符代码
//_ = videoCapture.Format; // 获取或设置图像的对象格式
//_ = videoCapture.Format; // 获取或设置后端特定的值(当前捕获模式)
//_ = videoCapture.ConvertRgb; // 是否转化为Rgb图片
//_ = videoCapture.FrameWidth; // 获取或设置视频的宽度
//_ = videoCapture.FrameHeight; // 获取或设置视频的宽度
//_ = videoCapture.FrameCount; // 获取或设置视频的帧数
//_ = videoCapture.Fps; // 获取或设置视频的帧速率
//_ = videoCapture.PosMsec; // 获取视频的当前帧位置
//_ = videoCapture.PosFrames; // 获取或设置视频的下一帧位置
//_ = videoCapture.Brightness; // 获取或设置图像的亮度(仅用于相机)
//_ = videoCapture.Contrast; // 获取或设置图像的对比度(仅用于相机)
//_ = videoCapture.Saturation; // 获取或设置图像的饱和度(仅用于相机)
//_ = videoCapture.Hue; // 获取或设置图像的色调图像(仅用于相机)
//_ = videoCapture.Gain; // 获取或设置图像的图像增益(仅用于相机)
//_ = videoCapture.Exposure; // 获取或设置图像的曝光(仅用于相机)
#endregion 常用属性
2、操作视频文件
/// <summary>
/// 打开视频 By 文件
/// </summary>
/// <param name="fileName">视频文件地址</param>
/// <param name="apiPreference">打开的方式类型</param>
/// <returns></returns>
public static VideoCapture Open_FromFile(string fileName, VideoCaptureAPIs apiPreference = VideoCaptureAPIs.ANY)
{
// 写法一:
//VideoCapture capture = new VideoCapture();
//capture.Open(fileName, apiPreference);
// 写法二:
VideoCapture capture = new VideoCapture(fileName, apiPreference);
return capture;
}
/// <summary>
/// 关闭视频/摄像头
/// </summary>
/// <param name="videoCapture"></param>
public static void Release(VideoCapture videoCapture) => videoCapture?.Release();
/// <summary>
/// 是否成功打开
/// </summary>
/// <param name="videoCapture">视频对象</param>
public static bool IsOpened(VideoCapture videoCapture) => videoCapture.IsOpened();
/// <summary>
/// 读取视频/摄像头中的当前帧 -RetrieveMat
/// </summary>
/// <param name="videoCapture">video对象</param>
public static Mat RetrieveMat(VideoCapture videoCapture)
{
return videoCapture.RetrieveMat();
}
/// <summary>
/// 读取视频/摄像头中的当前帧 -Retrieve
/// </summary>
/// <param name="videoCapture">video对象</param>
/// <param name="mat">图像信息</param>
/// <param name="flag">它可以是帧索引或特定于驱动程序的标志</param>
/// <returns></returns>
public static bool Retrieve(VideoCapture videoCapture, out Mat mat, int flag = 0)
{
mat = new Mat();
return videoCapture.Retrieve(mat, flag);
}
/// <summary>
/// 读取视频/摄像头中的当前帧 -Read
/// </summary>
/// <param name="videoCapture">video对象</param>
/// <param name="mat">图像信息</param>
/// <returns></returns>
public static bool Read(VideoCapture videoCapture, out Mat mat)
{
mat = new Mat();
return videoCapture.Read(mat);
}
3、操作摄像头
/// <summary>
/// 打开视频 By 摄像头
/// </summary>
/// <param name="index">摄像头编号,0代表默认摄像头</param>
/// <param name="apiPreference">打开的方式类型</param>
/// <returns></returns>
public static VideoCapture Open_FromCamera(int index, VideoCaptureAPIs apiPreference = VideoCaptureAPIs.ANY)
{
// 写法一:
//VideoCapture capture = new VideoCapture();
//capture.Open(index, apiPreference);
// 写法二:
VideoCapture capture = new VideoCapture(index, apiPreference);
return capture;
}
/// <summary>
/// 关闭视频/摄像头
/// </summary>
/// <param name="videoCapture"></param>
public static void Release(VideoCapture videoCapture) => videoCapture?.Release();
/// <summary>
/// 是否成功打开
/// </summary>
/// <param name="videoCapture">视频对象</param>
public static bool IsOpened(VideoCapture videoCapture) => videoCapture.IsOpened();
/// <summary>
/// 读取视频/摄像头中的当前帧 -RetrieveMat
/// </summary>
/// <param name="videoCapture">video对象</param>
public static Mat RetrieveMat(VideoCapture videoCapture)
{
return videoCapture.RetrieveMat();
}
/// <summary>
/// 读取视频/摄像头中的当前帧 -Retrieve
/// </summary>
/// <param name="videoCapture">video对象</param>
/// <param name="mat">图像信息</param>
/// <param name="flag">它可以是帧索引或特定于驱动程序的标志</param>
/// <returns></returns>
public static bool Retrieve(VideoCapture videoCapture, out Mat mat, int flag = 0)
{
mat = new Mat();
return videoCapture.Retrieve(mat, flag);
}
/// <summary>
/// 读取视频/摄像头中的当前帧 -Read
/// </summary>
/// <param name="videoCapture">video对象</param>
/// <param name="mat">图像信息</param>
/// <returns></returns>
public static bool Read(VideoCapture videoCapture, out Mat mat)
{
mat = new Mat();
return videoCapture.Read(mat);
}
4、保存录像
/// <summary>
/// 保存视频 -初始化
/// </summary>
/// <param name="fileName">视频名称</param>
/// <param name="apiPreference">允许指定要使用的API后端。可用于强制执行特定读取器</param>
/// <param name="fourcc">用于压缩帧的编解码器的4个字符代码;默认是PIM1,也可以是MJPG、H264、H265</param>
/// <param name="fps">视频流的帧速率</param>
/// <param name="frameSize">视频流的宽高</param>
/// <param name="isColor">是否是彩色</param>
/// <returns></returns>
public static VideoWriter VideoWriter_Init(string fileName, VideoCaptureAPIs apiPreference, FourCC fourcc, double fps, Size frameSize, bool isColor = true)
{
VideoWriter videoWriter = new VideoWriter(fileName, apiPreference, fourcc, fps, frameSize, isColor);
return videoWriter;
}
/// <summary>
/// 保存视频 - 添加帧
/// </summary>
/// <param name="videoCapture">VideoWriter对象</param>
/// <param name="mat">图像信息</param>
/// <returns></returns>
public static void VideoWriter_Write(VideoWriter videoWriter, Mat mat) => videoWriter.Write(mat);
/// <summary>
/// 保存视频 - 保存完成(结尾执行或分段执行)
/// </summary>
/// <param name="videoCapture">VideoWriter对象</param>
/// <param name="mat">图像信息</param>
/// <returns></returns>
public static void VideoWriter_Release(VideoWriter videoWriter) => videoWriter?.Release();
标签:视频,进阶,C#,mat,static,VideoCapture,视频文件,public,videoCapture
From: https://www.cnblogs.com/qq2806933146xiaobai/p/18295792