從這篇文章開始,會介紹一些通過EmguCv實現的一些簡單的功能,這個內容的更新會跟我學習OpenCv的進度有關,最近在看一本關於OpenCv的書——《學習OpenCv》,主要例子還是通過這本書中的C++樣例“翻譯”為EmguCv版本,如果有更有效的“翻譯方式”,也希望大牛們能給指出來。
1.C#創建一個窗體工程
在工程中的“解決方案資源管理器”中右鍵-添加引用,將EmguCv目錄中的動態庫加入。如下圖所示:
![]() |
![]() |
2.在Form中添加一個Button和一個ImageBox
如果在工具箱中未找到imageBox,選擇工具-選擇工具箱項,在彈出的窗口中的.Net Framwork標簽中點擊瀏覽,把EmguCV目錄下的Emgu.CV.UI.dll加入即可。
3.添加點擊事件
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 fnm = capture.GetCaptureProperty(CapProp.FrameCount); 15 fps = (int)capture.GetCaptureProperty(CapProp.Fps); 16 if (capture.Width != 0 && capture.Height != 0) 17 { 18 picWindow.Image = capture.QueryFrame(); 19 } 20 21 } 22 catch (Exception exp) 23 { 24 MessageBox.Show(exp.Message); 25 } 26 27 28 } 29 }
4.運行程序得到的效果如下圖所示: