用AForge将普通视频转换为带运动检测效果的视频

本文详细介绍使用AForge库在Windows环境下,通过VS2015进行开发,将普通视频转换为带有运动检测效果的视频的过程。从环境搭建、项目创建到编码实现,最后展示转换后的视频效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完整工程代码下载:

https://download.youkuaiyun.com/download/luohualiushui1/10949777

之前用过opencv转换普通视频为带人脸检测效果的视频

https://blog.youkuaiyun.com/luohualiushui1/article/details/86661501

也用过ImageAI(基于tensorflow)转换普通视频为带目标检测效果的视频

https://blog.youkuaiyun.com/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到这一步基本写好测试看看

保存的转换的视频文件播放如下:

从视频效果看基本没有遗漏,效果还算比较好,转换速度也比较快。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路边草随风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值