c# 远程监控(2) 摄像头调研及模拟

经过N多调研,最终选择了OpenCV(Emgu CV) 

 ** 至于DirectShow, OpenCV等等其他大家可以百度,在这里我就不再赘述

 

环境:vs2010 vs2012 vs2013均可

OpenCV官方网站为:Emgu CV

也可以去我的百度网盘下载安装包:libemgucv-windows-universal-cuda-2.4.10.1940

然后就可以自己想怎么玩,怎么玩了。

 

安装好后:

我的一个Demo,用来打开摄像头:

下载地址:c#调用摄像头

代码结构:

运行效果:

 

核心代码解释:

复制代码
namespace CameraCapture
{
    public partial class CameraCapture : Form
    {
        private readonly Capture _capture;
        private bool _captureInProgress;

        public CameraCapture()//构造函数
        {
            InitializeComponent();
            try
            {
                _capture = new Capture();//构造一个摄像头实例
                _capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();//获取视频帧

            Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
            Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            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 FlipHorizontalButtonClick(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
        }

        private void FlipVerticalButtonClick(object sender, EventArgs e)
        {
            if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
        }
    }
}
复制代码

扩展

emgucv不仅可以控制摄像头,而且可以直接播放本地视频,但是需要一些配置

复制代码
        public CameraCapture()
        {
            InitializeComponent();
            try
            {
                //_capture = new Capture();
                var fileName = "文件地址";
                _capture = new Capture(fileName);
                _capture.ImageGrabbed += ProcessFrame;
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
复制代码

需要下载两个第三方文件:

opencv_ffmpeg.dll

opencv_ffmpeg_64.dll

三方插件可以去 github opencv  下载 三方控件,我的百度网盘:opencv_ffmpeg

 

这两个文件需要再重新改下名字(因为加进去报错,始终用不起,谷歌了好半天):

opencv_ffmpeg.dll opencv_ffmpegVersion.dll -> opencv_ffmpeg2410_64.dll

opencv_ffmpeg_64.dll opencv_ffmpegVersion_64.dll -> opencv_ffmpeg2410_64.dll

最后复制加到bin目录下的 x86,x64 下就可以播放本地视频了

效果如下:

有问题可以站内信

 

posted @   Herenwei_Wayne  阅读(5101)  评论(4编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示