AForge.NET 入门

来源:互联网 发布:php的mvp 编辑:程序博客网 时间:2024/05/17 01:53


一、AForge.NET简介

       AForge.NET是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,机器人等领域。


       这个框架由一系列的类库组成。主要包括有:

       AForge.Imaging —— 一些日常的图像处理和过滤器

       AForge.Vision —— 计算机视觉应用类库

       AForge.Neuro —— 神经网络计算库AForge.Genetic -进化算法编程库

       AForge.MachineLearning —— 机器学习类库

       AForge.Robotics —— 提供一些机器人的工具类库

       AForge.Video —— 一系列的视频处理类库

       AForge.Fuzzy —— 模糊推理系统类库

       AForge.Controls—— 图像,三维,图表显示控件

 



二、下载与添加AForge引用

 

(1)解压缩下载的AForge类库——AForge.NET Framework-2.2.5.zip(为了便于使用,应放于容易找到的路径下)


(2)在Visual Studio中新建WinForm程序(Windows窗体应用程序),在新建的项目中添加Aforge.NET的引用

        

          Step1:解决方案资源管理器中,右键“引用”--> “添加引用”


 


          Step 2:在“引用管理器”中点击右下角的“浏览”,找到Aforge.NET的路径,进入\Release文件夹下,选择必要的.dll文件添加到项目中(可参照前面介绍过的Aforge.NET类库的组成)

 

 

          Step 3:点击“确定”,完成类库引用的添加

 



(3)窗体的后台代码中添加有关于Aforge必要的using引用

例如:

using AForge.Video;

using AForge.Video.DirectShow;




三、示例之使用pictureBox控件显示获取的位图

(1)在Visual Studio中创建一个WinForm应用程序,界面中添加一个pictureBox控件和一个按钮Button,如下图所示:



(2)后台代码

publicpartialclassForm1 : Form {

        //视频输入设备(摄像头)的集合

        publicFilterInfoCollection Cameras = null;

        //本程序使用的那个摄像头

        publicVideoCaptureDevice Cam =null;

 

        ///<summary>

        ///窗体的构造函数:

        /// Load事件用于在加载窗体时获取摄像头设备

        /// FormClosed事件用于在直接关闭窗体时关闭摄像头,释放资源

        ///</summary>

        public Form1() {

            InitializeComponent();

            this.Load += Form1_Load;

            this.FormClosed +=Form1_FormClosed;

        }

       

        ///<summary>

        ///窗体构造函数的Load事件处理程序

        ///用于获取摄像头设备

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        privatevoid Form1_Load(object sender, EventArgs e) {

            btn1.Enabled =false;

            try {

                //1、获取并枚举所有摄像头设备

                Cameras = newFilterInfoCollection(FilterCategory.VideoInputDevice);

                //2、判断设备个数,选择某一设备

                if (Cameras.Count >0) {

                    btn1.Enabled =true;

                    Cam = newVideoCaptureDevice(Cameras[0].MonikerString);

                    Cam.NewFrame +=Cam_NewFrame;

                } else {

                    MessageBox.Show("未找到视频输入设备!");

                }

            } catch (Exception ex) {

                MessageBox.Show(ex.ToString());

            }

        }

 

        ///<summary>

        ///摄像头设备Cam的NewFrame事件处理程序

        ///用于实时显示捕获的视频流

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="eventArgs"></param>

        privatevoid Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) {

            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();

        }

       

        ///<summary>

        ///点击按钮的事件处理程序

        ///用于控制摄像头的开启、关闭

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        privatevoid btn1_Click(object sender, EventArgs e) {

            if (btn1.Text =="开启摄像头") {

                btn1.Text = "关闭摄像头";

                Cam.Start();

            } else {

                btn1.Text = "开启摄像头";

                Cam.Stop();

            }

        }

 

        ///<summary>

        ///在关闭窗体的事件处理程序中,释放摄像头

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        privatevoid Form1_FormClosed(object sender, FormClosedEventArgs e) {

            if (Cam !=null &&Cam.IsRunning) {

                Cam.Stop();

            }

        }

    }


(3)运行效果


 

 

0 0