標籤:空閑 資訊 src gre arp explore abs idle from
原文:C# 利用 OpenCV 進行視頻捕獲 (筆記)
| 簡介 這個項目是關於如何從網路攝影機或者視頻檔案(*.AVI)中捕獲視頻的,這個項目是用C#和OPENCV編寫的。 這將有助於那些喜歡C#和OpenCV環境的人。這個程式完全基於Visual Studio 2010 version C#.NET環境。這個程式展示了怎樣用C#.NET環境的Visual Studio 2010 IDE編寫OpenCV,這個程式是一個怎樣用Visual Studio 2010,C#.NET建立程式的例子。 在這篇文章中,我解釋了怎樣配置Visual Studio 2010,一種配置電腦環境變數EmguCV2.4.9以便運行OpenCV程式的步驟。 EmguCV:讓我們開始工作吧。。。 EmguCV 是一個跨平台的運行OpenCV圖形庫的殼。它允許從.NET語言例如C#,VB,VC++中調用OpenCV函數,這個殼可以用Mono編譯,在Windows,Linux,Mac OS X,iPhone,iPad 和Android 裝置中運行。 EmguCV是用C#編寫的。可以在Mono中編譯,所以它可以在任何Mono支援的平台上運行,包括 Linux, Mac and Android。 |
pou 翻譯於 2年前2人頂 頂 翻譯的不錯哦! |
| 準備 Visual Studio 2010 第 1 步: 安裝 EmguCV 2.4.9 下載 EmguCV 2.4.9 版本. 將它安裝在 c:\ 磁碟 位置, 不要改變路徑, 使用預設的路徑“C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847”. 安裝路徑 – “C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847”. 所有的框框都選上,全包安裝. 第 2 步: 設定環境變數: 在使用者和系統變數中設定如下的三個路徑.
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin;
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin\x64;
C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin\x86;
第 3 步: 配置 Visual Studio 2010:
建立一個新的 Windows Application 項目,命名為‘Two Layer’.
選擇引用, 右鍵點擊添加引用.
選擇 Browse 選項卡, 找到“C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847\bin”, 選擇“Emgu.CV.dll”,”Emgu.CV.UI.dll”,”Emgu.Util.dll” 三個檔案, 並點擊ok.
引用就會在Solution Explorer中出現.
|
leoxu 翻譯於 2年前0人頂 頂 翻譯的不錯哦! |
| 捕獲視頻 捕獲視頻功能,捕獲視頻有兩種方式,一種是從攝像機捕獲,第二種是從視頻檔案捕獲。接下來的部分,代碼會向你展示如果從攝像機捕獲視屏. 在這一節中, 捕獲, FRAME PER SECOND 設定為 30 FPS, 視頻檔案捕獲的高和寬分別設定為 240, 320 . 然後video_seek被初始化為零 ‘0‘, 這個視頻搜尋控制會在視頻限制的低位和高位之間搜尋視頻. 下面的語句在應用程式中最有用. 它有點像多線程. 當應用程式進入到空閑狀態,"ProcessFrame" 就會一直調用直到視訊框架結束或者直到幀不為 ‘null‘.
| 1 |
Application.Idle += ProcessFrame; |
在從視頻檔案捕獲的代碼中,我們需要總幀數來設定視頻搜尋控制的上限. FOURCC 被用來找到多媒體的編碼解碼器名稱. |
leoxu 翻譯於 2年前0人頂 頂 翻譯的不錯哦! |
從攝像機捕獲的代碼
#region cameracapture if (comboBox1.Text == "Capture From Camera") { try { _capture = null; _capture = new Capture(0); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, 30); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320); Time_Label.Text = "Time: "; Codec_lbl.Text = "Codec: "; Frame_lbl.Text = "Frame: "; webcam_frm_cnt = 0; cam = 1; Video_seek.Value = 0; Application.Idle += ProcessFrame; button1.Text = "Stop"; comboBox1.Enabled = false; } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } }#endregion cameracapture |
從視頻檔案捕獲的代碼
#region filecapture if (comboBox1.Text == "Capture From File") { openFileDialog1.Filter = "MP4|*.mp4"; openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { _capture = null; _capture = new Capture(openFileDialog1.FileName); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320); FrameRate = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS); TotalFrames = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT); codec_double = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FOURCC); string s = new string(System.Text.Encoding.UTF8.GetString (BitConverter.GetBytes(Convert.ToUInt32(codec_double))).ToCharArray()); Codec_lbl.Text = "Codec: " + s; cam = 0; Video_seek.Minimum = 0; Video_seek.Maximum = (int)TotalFrames - 1; Application.Idle += ProcessFrame; button1.Text = "Stop"; comboBox1.Enabled = false; } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } }#endregion filecapture |
處理映像
下面的函數用來處理幀. 幀處理可以提取出一些詳細資料,如幀的編號,時間軸,總的幀數等. 這個函數展示了圖片盒子中的映像序列. 幀可以被轉換成位元組數組. 這個位元組資料可以被轉換成每一幀的16進位值. 然後那些十六進位值被存到數組中做進一步的處理. 從裝置或者視頻檔案捕獲的視頻中提取當前幀.
frame = _capture.QueryFrame(); |
幀被轉換成 bitmap 並被賦值給圖片盒子用於展示 .
pictureBox1.Image = frame.ToBitmap(); |
函數在按幀率劃分的特定時間休眠.
?
| 1 |
Thread.Sleep((int)(1000.0 / FrameRate)); |
幀被轉換成位元組數組. 這個位元組資料被轉換成每一幀的16進位值. 然後那些十六進位值被存到數組中做進一步的處理.
private void ProcessFrame(object sender, EventArgs arg){ try { Framesno = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES); frame = _capture.QueryFrame(); if (frame != null) { pictureBox1.Image = frame.ToBitmap(); if (cam == 0) { Video_seek.Value = (int)(Framesno); double time_index = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_MSEC); Time_Label.Text = "Time: " + TimeSpan.FromMilliseconds(time_index).ToString().Substring(0, 8); double framenumber = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES); Frame_lbl.Text = "Frame: " + framenumber.ToString(); Thread.Sleep((int)(1000.0 / FrameRate)); } if (cam == 1) { Frame_lbl.Text = "Frame: " + (webcam_frm_cnt++).ToString(); } } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }} |
釋放資料
這個方法用來釋放資料. 它同時也釋放了捕獲變數所需要的一些資源.
private void ReleaseData(){ if (_capture != null) _capture.Dispose();} |
C# 利用 OpenCV 進行視頻捕獲 (筆記)