完整工程代碼下載下傳:
https://download.csdn.net/download/luohualiushui1/10949777
之前用過opencv轉換普通視訊為帶人臉檢測效果的視訊
https://blog.csdn.net/luohualiushui1/article/details/86661501
也用過ImageAI(基于tensorflow)轉換普通視訊為帶目标檢測效果的視訊
https://blog.csdn.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到這一步基本寫好測試看看
儲存的轉換的視訊檔案播放如下:
從視訊效果看基本沒有遺漏,效果還算比較好,轉換速度也比較快。