開發環境 Visual Studio 2010 C# WindowsForm 應用程式

webcam啟動是使用EmguCV 


先介紹EmguCV 可用於人臉辨識和影像, 其實它是基於 OpenCV 所延伸出的一套library,特別為 .Net平台而設計。


 2012/08/06 補充 執行本範例請使用emgucv-windows-x86 2.3.0.1416.exe 版本


EmguCV最新版本

EmguCV

 

本範例EmguCV版本

emgucv-windows-x86 2.3.0.1416.exe


安裝完需再添加環境變數(加入完整路徑 預設C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin),步驟如下

1.我的電腦右鍵內容 ---> 進階系統設定 ---> 進階 ---> 環境變數

1  

2.系統變數 ---> 變數尋找到Path ---> 編輯

2  

3.將EmguCV 安裝完的完整路徑加進去,如為預設請加入C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin ※Windows環境變數需以分號區隔 Linux區隔為冒號

3  


開啟一個Form新專案 加入一個pictureBox 三個button

版面設計

4  

加入四個參考 Emgu.CV.dll,Emgu.CV.ML.dll,Emgu.CV.UI.dll,Emgu.Util.dll (該dll放於EmguCV安裝完的bin底下)

5  

載入所需的

6  


全區域變數宣告

//攝影機的變量
private Capture _capture;
//判斷是否啟動webcam的frame旗標
private bool _captureInProgress = false;


 

private void Form1_Load(object sender, EventArgs e)
{

    //預設使用第一台的webcam
    _capture = new Capture(0);
}


//frame的處裡事件

 

void Application_Idle(object sender, EventArgs e)
{
Image<Bgr, Byte> frame = _capture.QueryFrame();
pictureBox1.Image = frame.ToBitmap();
}


//啟動webcam

private void button1_Click(object sender, EventArgs e)
{
   //如果webcam沒啟動
   if (_capture == null)
   {
   try
   {
   //打開預設的webcam
    _capture = new Capture();
    }
    catch (NullReferenceException excpt)
   {
    MessageBox.Show(excpt.Message);
    }
    }

     //webcam啟動
     if (_capture != null)
    {
     //frame啟動
    if (_captureInProgress)
    { //stop the capture
    _captureInProgress = false;
    button1.Text = "开始";
    Application.Idle -= Application_Idle;
    }
    //frame關閉
    else
    {
     //start the capture
    _captureInProgress = true;
    button1.Text = "结束";
     Application.Idle += Application_Idle;
     }
   }
}


//錄製影像

 

private void button2_Click(object sender, EventArgs e)
{
     _capture = new Capture();
     if (_capture == null)
    {
     MessageBox.Show("can't find a camera", "error");
    }

 

    Image<Bgr, byte> temp = _capture.QueryFrame();

 

     SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
     SaveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
     SaveFileDialog1.Filter = "Image Files(*.avi)|*.avi|All files (*.*)|*.*";
     if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
    {
     MessageBox.Show("開始錄製,按ESC結束錄製");
    }

 

     VideoWriter video = new VideoWriter(SaveFileDialog1.FileName, CvInvoke.CV_FOURCC('X', 'V', 'I', 'D'), 20, 800, 600, true);

 

     while (temp != null)
    {
     CvInvoke.cvShowImage("camera", temp.Ptr);
     temp = _capture.QueryFrame();
     int c = CvInvoke.cvWaitKey(20);
     video.WriteFrame<Bgr, byte>(temp);
    if (c == 27) break;
    }
     video.Dispose();
     CvInvoke.cvDestroyWindow("camera");

     //錄影完需將影像停止不然會出錯

    _captureInProgress = false;
    button1.Text = "開始";
    Application.Idle -= Application_Idle;


}


//關閉程式

private void button3_Click(object sender, EventArgs e)
{
   _capture.Dispose();
   Application.Exit();
}


參考資料:

http://blog.csdn.net/puncsky/article/details/7176810

http://apps.hi.baidu.com/share/detail/59126928

http://en.sourceforge.jp/projects/sfnet_emgucv/downloads/emgucv/2.3.0/libemgucv-windows-x86-2.3.0.1416.exe/

 

範例下載:EmguCV

 

 






arrow
arrow
    文章標籤
    EmguCV webcam
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(17) 人氣()