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

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AForge.Video.DirectShow; using AForge.Video; namespace AForgeDemo { public partial class Form1 : Form { private bool DeviceExist = false; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { getCamList(); } private void getCamList() { try { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); cbDev.Items.Clear(); if (videoDevices.Count == 0) throw new ApplicationException(); DeviceExist = true; foreach (FilterInfo device in videoDevices) { cbDev.Items.Add(device.Name); } cbDev.SelectedIndex = 0; } catch (ApplicationException) { DeviceExist = false; cbDev.Items.Add("无设备"); } } private void CloseVideoSource() { if (!(videoSource == null)) if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource = null; } } private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); picVideo.Image = img; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseVideoSource(); } private void btnOpen_Click(object sender, EventArgs e) { if (DeviceExist) { videoSource = new VideoCaptureDevice(videoDevices[cbDev.SelectedIndex].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); CloseVideoSource(); videoSource.DesiredFrameSize = new Size(picVideo.Width, picVideo.Height); videoSource.Start(); lbinfo.Text = "设备运行..."; } else { lbinfo.Text = "没有选择设备"; } } private void btnStop_Click(object sender, EventArgs e) { if (videoSource.IsRunning) { CloseVideoSource(); lbinfo.Text = "设备停止"; } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

何以问天涯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值