用AForge将普通视频转换为带运动检测效果的视频
之前用过opencv转换普通视频为带人脸检测效果的视频
https://blog.****.net/luohualiushui1/article/details/86661501
也用过ImageAI(基于tensorflow)转换普通视频为带目标检测效果的视频
https://blog.****.net/luohualiushui1/article/details/86735609
现在用AForge将普通视频转换为带运动检测效果的视频
原理就是把普通视频里面的发生变动的部分加上标注重新生成一个视频
这次选用windows环境下vs2015开发
第一步就是下载好AForge安装好,使用相关库,C:\Program Files (x86)\AForge.NET\Framework\Release这个目录下的
dll需要使用到,另外C:\Program Files (x86)\AForge.NET\Framework\Externals\ffmpeg\bin目录下的dll也需要使用到,因为
使用到的AForge.Video.FFMPEG.dll有所依赖。
第二步新建c#项目,但.net平台得选择.net2.0,因为AForge.Video.FFMPEG.dll依赖的avcodec-53.dll等等是基于.net2.0的选择4.0或其他最终运行会提示冲突
第三步现在开始编码
winform开发先布局好
选择视频文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
... ...
}
然后调用AForge的视频相关类做播放处理,并定义好输出转换视频的路径等参数,输出视频的分辨率和帧率参照原视频
FileVideoSource fileSource = new FileVideoSource(openFileDialog.FileName);
VideoFileReader reader = new VideoFileReader();
reader.Open(openFileDialog.FileName);
writer.Open("E:\\video\\m0.avi", reader.Width, reader.Height, reader.FrameRate, VideoCodec.MPEG4);
this.videoSourcePlayer1.VideoSource = fileSource;
this.videoSourcePlayer1.Start();
this.videoSourcePlayer2.VideoSource = fileSource;
this.videoSourcePlayer2.Start();
AForge处理运动检测的初始化定义
MotionDetector detector = new MotionDetector(
new TwoFramesDifferenceDetector(),
new MotionAreaHighlighting());
private int motionDetectionType = 1;
private int motionProcessingType = 1;
private const int statLength = 15;
private int statIndex = 0;
private int statReady = 0;
private int[] statCount = new int[statLength];
private int flash = 0;
private float motionAlarmLevel = 0.015f;
private List<float> motionHistory = new List<float>();
private int detectedObjectsCount = -1;
在播放每一帧的事件方法里面做运动检测的处理,并输出到转换视频
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
lock (this)
{
if (detector != null)
{
float motionLevel = detector.ProcessFrame(image);
if (motionLevel > motionAlarmLevel)
{
flash = 10;
}
if (detector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing)
{
BlobCountingObjectsProcessing countingDetector = (BlobCountingObjectsProcessing)detector.MotionProcessingAlgorithm;
detectedObjectsCount = countingDetector.ObjectsCount;
}
else
{
detectedObjectsCount = -1;
}
motionHistory.Add(motionLevel);
if (motionHistory.Count > 300)
{
motionHistory.RemoveAt(0);
}
writer.WriteVideoFrame(image);
}
}
}
ok到这一步基本写好测试看看
保存的转换的视频文件播放如下:
从视频效果看基本没有遗漏,效果还算比较好,转换速度也比较快。