C#使用Capture摄像头

先在Form上拉两个控制项,分別是 Button ( Name : captureButton )与 ImageBox ( Name : captureImageBox )、ImageBox ( Name : grayscaleImageBox )、ImageBox ( Name : smoothedGrayscaleImageBox )、ImageBox ( Name : cannyImageBox )。

这里写图片描述

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 Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;

namespace Emgu_CameraCapture
{
    public partial class Form1 : Form
    {
        private Capture _capture = null;
        private bool _captureInProgress;

        public Form1()//构造函数
        {
            InitializeComponent();
            CvInvoke.UseOpenCL = false; //不使用OpneCL  
            try
            {
                _capture = new Capture();//构造一个摄像头实例
                _capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();//获取视频帧
            _capture.Retrieve(frame, 0);
            Mat grayFrame = new Mat();
            CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
            Mat smallGrayFrame = new Mat();
            CvInvoke.PyrDown(grayFrame, smallGrayFrame);
            Mat smoothedGrayFrame = new Mat();
            CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

            //Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            //Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            Mat cannyFrame = new Mat();
            CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);

            //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);

            captureImageBox.Image = frame;//转成图片并显示在主界面上
            grayscaleImageBox.Image = grayFrame;
            smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
            cannyImageBox.Image = cannyFrame;
        }

        private void captureButtonClick(object sender, EventArgs e)
        {
            if (_capture != null)
            {
                if (_captureInProgress)
                {  //stop the capture
                    captureButton.Text = "Start Capture";
                    _capture.Pause();
                }
                else
                {
                    //start the capture
                    captureButton.Text = "Stop";
                    _capture.Start();
                }

                _captureInProgress = !_captureInProgress;
            }
        }
        private void ReleaseData()//释放资源
        {
            if (_capture != null)
                _capture.Dispose();
        }

        private void FlipHorizontalButton_Click(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
        }

        private void FlipVerticalButton_Click(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
        }

    }
}

播放本地视频

这里写图片描述

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 Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;

namespace Emgu_CameraCapture
{
    public partial class Form1 : Form
    {
        private Capture _capture = null;
        private bool _captureInProgress;

        public Form1()//构造函数
        {
            InitializeComponent();
            CvInvoke.UseOpenCL = false; //不使用OpneCL  
            try
            {
                //_capture = new Capture();//构造一个摄像头实例
                var fileName = "E:\\Homework\\Video\\0.avi";//文件地址就这里改动
                _capture = new Capture(fileName);//就这里改动
                _capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();//获取视频帧
            _capture.Retrieve(frame, 0);
            Mat grayFrame = new Mat();
            CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
            Mat smallGrayFrame = new Mat();
            CvInvoke.PyrDown(grayFrame, smallGrayFrame);
            Mat smoothedGrayFrame = new Mat();
            CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

            //Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            //Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            Mat cannyFrame = new Mat();
            CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);

            //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);

            captureImageBox.Image = frame;//转成图片并显示在主界面上
            grayscaleImageBox.Image = grayFrame;
            smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
            cannyImageBox.Image = cannyFrame;
        }

        private void captureButtonClick(object sender, EventArgs e)
        {
            if (_capture != null)
            {
                if (_captureInProgress)//一开始为0,所以此句不执行
                {  //stop the capture
                    captureButton.Text = "Start Capture";
                    _capture.Pause();
                }
                else
                {
                    //start the capture
                    captureButton.Text = "Stop";
                    _capture.Start();
                }

                _captureInProgress = !_captureInProgress;
            }
        }

        private void ReleaseData()//释放资源
        {
            if (_capture != null)
                _capture.Dispose();
        }

        private void FlipHorizontalButton_Click(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;//水平翻转
        }

        private void FlipVerticalButton_Click(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;//竖直翻转
        }

    }
}

参考

【-1】c# 远程监控(2) 摄像头调研及模拟 - Herenwei_Wayne - 博客园
http://www.cnblogs.com/herenwei-wayne/p/4375784.html
【0】EmguCV的配置和获取摄像头画面 - CSDN博客
http://blog.csdn.net/qq_22033759/article/details/47299269
【1】VS2010、C#、Emgu CV配置 ; 在C#下使用OpenCV ; C#中使用OpenCV(Emgu CV) - yimingsilence的专栏 - CSDN博客
http://blog.csdn.net/yimingsilence/article/details/49587641
【2】VS2010平台下的OpenCV、EmguCV( C#)安装、使用配置 - Mr. Liu - 博客园
http://www.cnblogs.com/idaidai/archive/2012/09/02/VS2010-OpenCV-EmguCV.html
【3】C#操作摄像头 实现拍照功能 - CSDN博客
http://blog.csdn.net/xblywl/article/details/51792951
【4】C#调用 opencv cv::Mat 图像, 采用折中方法 - CSDN博客
http://blog.csdn.net/yeyang911/article/details/51158621

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值