打開視頻的思路跟打開圖片的思路是一樣的,只不過視頻是由一幀幀圖片組成,因此,打開視頻的處理程序有一個連續的獲取圖片並逐幀顯示的處理過程。GUI同《淺入淺出EmguCv(二)EmguCv打開指定圖片》一樣,只不過處理程序編程如下所示:

1 /// <summary> 2 /// 點擊按鈕打開指定圖片 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void OpenImage_Click(object sender, EventArgs e) 7 { 8 OpenFileDialog openFileDialog = new OpenFileDialog(); 9 if (openFileDialog.ShowDialog() == DialogResult.OK) 10 { 11 try 12 { 13 capture = new Capture(openFileDialog.FileName); 14 fps = (int)capture.GetCaptureProperty(CapProp.Fps); 15 Application.Idle += new EventHandler(ProcessFrame); 16 } 17 catch (Exception exp) 18 { 19 MessageBox.Show(exp.Message); 20 } 21 22 23 } 24 } 25 private void ProcessFrame(object sender, EventArgs e) 26 { 27 28 Mat frame = capture.QueryFrame(); 29 if (frame != null) 30 { 31 //為使播放順暢,添加以下延時 32 System.Threading.Thread.Sleep((int)(1000.0 /fps-5)); 33 picWindow.Image = frame; 34 35 } 36 GC.Collect(); 37 }
編譯運行,打開視頻如圖所示: