AForge.NET_開啟攝像頭功能_視訊處理在WindowsForm上的配置教學

在一般影像處理的開發上常用的opencv,通常是搭配C++語言去做撰寫。

但是GUI上不是很方便去做設置

可能要搭配到 「Qt」  或者  「MFC」等框架來做輔助


========================================================

最近我的同學想要做在WindowsForm上的開發

去做相關的影像(視訊)處理


結合  Kinect V2 SDK

當然首先會遇到的就是

opencv 沒有支援 C# 阿!!!!

所以這裡推薦一個好用的庫給大家



AForge.NET


AForge.net 是一個用於處理圖像方面的框架

常用在追蹤 、 運動識別 、 圖像比較等等功能

AForge.NET is an open source C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence 

ex:

image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc.     




優點在於  配置比 opencv (C++) 和 Emgucv (C#)  都來的快和方便

非常好上手



【第一階段 - 下載&環境配置】





以下是它的官網載點 : http://www.aforgenet.com/framework/downloads.html


選擇

[ Download Full ZIP Archive ]

做下載



下載下來進行解壓縮

這裡我就先放置好在C槽目錄下


之後請去新增一個  WindowsForm  專案


檔案 --> 新增 --> 專案



開好之後就會像下方的顯示

可視化視窗介面








接下來  去 右側

「參考」 --> 右鍵 --> 「加入參考」--> 瀏覽(Browse)-->去找你安置AForge.Net的目錄

在 Release裏頭



加入AForge.Video.DirectShow.dll、AForge.Video.dll

(開啟攝像頭只需要使用到這兩個dll檔)




【第二階段 - 視窗介面佈局設計】


一個  button  、  一個 pictureBox





【第三階段 - 功能程式撰寫】



引用所需的兩項命名空間

using AForge.Video;
using AForge.Video.DirecShow;





宣告變數

public FilterInfoCollection  USB_Webcams = null ;//FilterInfoCollection類別實體化

public VideoCaptureDevice  cam;//攝像頭的初始化





關於  FilterInfoCollection Class
這個類別
更詳細可參考這個網站
http://www.aforgenet.com/framework/docs/html/8d13e608-64f7-8c03-84b1-1bce74dabecd.htm








全部代碼


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;


namespace AForge.Net_ex1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public FilterInfoCollection USB_Webcams = null;
        public VideoCaptureDevice Cam = null;


        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                button1.Text = "Stop";
                Cam.Start();   // WebCam starts capturing images.
            }
            else
            {
                button1.Text = "Start";
                Cam.Stop();  // WebCam stops capturing images.
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            USB_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (USB_Webcams.Count > 0)  // The quantity of WebCam must be more than 0.
            {
                button1.Enabled = true;
                Cam = new VideoCaptureDevice(USB_Webcams[0].MonikerString);
                
                Cam.NewFrame += Cam_NewFrame;//Press Tab  to   create
            }
            else
            {
                button1.Enabled = false;
                MessageBox.Show("No video input device is connected.");
            }
        }

        void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            //throw new NotImplementedException();
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Cam != null)
            {
                if (Cam.IsRunning)  // When Form1 closes itself, WebCam must stop, too.
                {
                    Cam.Stop();   // WebCam stops capturing images.
                }
            }
        }


    }
}



實現畫面效果








留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題