OpenCVSharp 获取笔记本摄像头 学习笔记

   众所周知,Opencv是一款功能强大的图像智能处理类库,但是对于c++与opecv的初学者来说,是不容易快速掌握的

最近网上搜索资料得知,OpencvSharp类库对于C# 的学习者来说是一个绝对的利好消息。但是查了下这方面的资源网

上并不是很多。有的资源也有许多值得改进的地方。废话不多说 ,转入正题。

1.打开vs2017,菜单:文件》新建

OpenCVSharp 获取笔记本摄像头 学习笔记_第1张图片

2.文件结构如下:

OpenCVSharp 获取笔记本摄像头 学习笔记_第2张图片

3.导入nuget包

3.1打开nuget包管理器

  OpenCVSharp 获取笔记本摄像头 学习笔记_第3张图片

3.2 搜索并安装OpenCvSharp3-AnyCPU

OpenCVSharp 获取笔记本摄像头 学习笔记_第4张图片

4.1添加所需要的控件

一个用于动态显示事件的标签:LabelTime

一个用于动态获取摄像头的图片框:

pictureBox1  宽:640,高:480(注意:要与获取摄像头属性值宽高保持一致,否则会图片显示异常.)

5 为了节省大家的时间,直接上代码

 5.1 MainFrm后台代码

using System;
using System.Windows.Forms;
namespace OpenCameraWithOpencvSharp
{
    public partial class MainFrm : Form
    {
        /// 时间管理变量
        private TimeManager TimeManager_MF;
        /// 打开摄像头并动态显示图片到图片框的业务逻辑类
        private OpenCamera OpenCamera;
        public MainFrm()
        {
            InitializeComponent();
        }
        private void MainFrm_Load(object sender, EventArgs e)
        {
            TimeManager_MF = new TimeManager(LabelTime);
            //更新时间方法
            TimeManager_MF.TimeWorkForFromLoad();

            OpenCamera = new OpenCamera(pictureBox1);
            //动态加载图片到 pictureBox1
            OpenCamera.UpdatePictrueImage();
           
        }  
       
    }
}

5.2时间管理类代码

using System;

using System.Threading;
using System.Windows.Forms;
namespace OpenCameraWithOpencvSharp
{
    /// 
    ///时间管理类
    /// 
    public  class TimeManager
    {
        /// 
        /// 更新时间线程方法的委托
        /// 
        private delegate void SetTimeThreadDelegate();
        /// 
        /// 更新时间线程的回调方法
        /// 
        private SetTimeThreadDelegate SetTimeThreadCallBack;

        private delegate void UpdateTimeDelegate(string text);
        private UpdateTimeDelegate UpdateTimeCallBack;

        /// 
        /// 布尔变量用于更新时间线程方法
        /// 
        internal bool TimeBegin = true;

        /// 
        /// 用于获取时间标签
        /// 
        public Label LabelTime { get; private set; }

        public TimeManager(Label label)
        {
            LabelTime = label;
        }    
        public TimeManager()
        {

        }
        /// 
        /// 用于窗体加载时间中的方法
        /// 
        internal  void TimeWorkForFromLoad()
        {
            //声明更新时间线程回调
            SetTimeThreadCallBack = new SetTimeThreadDelegate(SetTimeForCallBack);
            //
            UpdateTimeCallBack = new UpdateTimeDelegate(UpdateTime);
            //更新时间线程
            Thread thread = new Thread(new ThreadStart(SetTimeThreadCallBack))
            {
                IsBackground = true
            };
            thread.Start();

            Thread.Sleep(40);
        }
        /// 
        /// 更新时间回调方法
        /// 
        private void SetTimeForCallBack()
        {
            
            while (TimeBegin)
            {
                if (LabelTime.InvokeRequired)
                {
                    LabelTime.Invoke(UpdateTimeCallBack, new object[] { DateTime.Now.ToString() });
                                                                                                                                       
                }

            }

        }
        /// 
        /// 更新时间方法
        /// 
        /// 
        private void UpdateTime(string time)
        {
            LabelTime.Text = time;
        }

    }
}

5.3 获取摄像头并更新图片类

using System;

using System.Drawing;
using System.Threading;
using OpenCvSharp;
using OpenCvSharp.Extensions;

using System.Windows.Forms;
namespace OpenCameraWithOpencvSharp
{
    /// 
    /// 打开摄像头并动态显示图片到图片框的业务逻辑类
    /// 
    public class OpenCamera
    {
        //更新图片线程用的委托回调变量
        internal  delegate void UpdateImageDelegate();
        internal  UpdateImageDelegate UpdateImageThreadCallBack_OC;
     
        /// 
        /// 用于获取
        /// 
        internal static PictureBox PictureBox { set; get; }
        /// 
        /// 用于获取或存储摄像头设备变量
        /// 
        internal static VideoCapture VideoCapture_OS;
        public OpenCamera()
        {

        }
        /// 
        /// 构造方法,用于传递参数
        /// 
        /// 
        public OpenCamera(PictureBox pictureBox)
        {
            PictureBox = pictureBox;
            
        }
        public void UpdatePictrueImage()
        {
            try
            {

                UpdateImageThreadCallBack_OC = new UpdateImageDelegate(UpdateImage);
                
            
                //跟新图片线程
                Thread thread = new Thread(new ThreadStart(UpdateImageThreadCallBack_OC))
                {
                    IsBackground = true
                };
                thread.Start();
                Thread.Sleep(40);
            }
            catch (Exception)
            {

                throw new Exception() ;
            }
           
        }
        /// 
        /// 更新图片方法,用于
        /// 
        private void UpdateImage()
        {          
            while (true)
            {
                VideoCapture_OS = InitVideoCapture();
                Mat mat = new Mat();
                
                if (VideoCapture_OS.Read(mat))
                {                  
                    Image image =(Image) mat.ToBitmap();
                    PictureBox.Image = image;                
                }

            }
        }

        /// 
        /// 初始化摄像头并设置摄像头宽高参数
        /// 
        /// 摄像头调用对象
        private static VideoCapture InitVideoCapture()
        {
            if (VideoCapture_OS==null)
            {
                VideoCapture_OS = OpenCvSharp.VideoCapture.FromCamera(CaptureDevice.Any);
                VideoCapture_OS.Set(CaptureProperty.FrameWidth, 640);
                VideoCapture_OS.Set(CaptureProperty.FrameHeight, 480);
            }
            
            return VideoCapture_OS;
        }
    }
}

5.5 MainFrm 前台代码

namespace OpenCameraWithOpencvSharp
{
    partial class MainFrm
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.LabelTime = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // LabelTime
            // 
            this.LabelTime.AutoSize = true;
            this.LabelTime.Font = new System.Drawing.Font("宋体", 14F);
            this.LabelTime.Location = new System.Drawing.Point(204, 10);
            this.LabelTime.Name = "LabelTime";
            this.LabelTime.Size = new System.Drawing.Size(0, 19);
            this.LabelTime.TabIndex = 0;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(46, 45);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(640, 480);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            // 
            // MainFrm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 537);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.LabelTime);
            this.Name = "MainFrm";
            this.Text = "OpenCvSharpOpenCamera";
            this.Load += new System.EventHandler(this.MainFrm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label LabelTime;
        private System.Windows.Forms.PictureBox pictureBox1;
    }
}

6.测试结果

OpenCVSharp 获取笔记本摄像头 学习笔记_第5张图片

7.如果这篇文章对你有所帮助请关注:Csharp天地

OpenCVSharp 获取笔记本摄像头 学习笔记_第6张图片

你可能感兴趣的:(图像处理)