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

时间:2024-01-17 18:44:20

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

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

环境:vs2010 vs2012 vs2013均可

OpenCV官方网站为:Emgu CV

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

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

安装好后:

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

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

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

代码结构:

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

运行效果:

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

核心代码解释:

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(, ); 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_ffmpeg_64.dll

opencv_ffmpeg_64.dll opencv_ffmpegVersion_64.dll -> opencv_ffmpeg_64.dll

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

效果如下:

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

有问题可以站内信