此篇文章分享如何使用C# EmguCV来撷取D-Link DCS-930 IP Cam的影像,其实把影像抓回来之后我还想要再做更多的处理以及判断辨识,以便达到某些目的。
但其实因为各家IP CAM支援的串流方式不太一样,所以开发前最好要看一下你买的IP CAM有没有支援。
此篇文章分享如何使用.net EmguCV来撷取D-Link DCS-930 IP Cam的影像,其实把影像抓回来之后我还想要再做更多的处理以及判断辨识,以便达到某些目的。
但其实因为各家IP CAM支援的串流方式不太一样,所以开发前最好要看一下你买的IP CAM有没有支援。至于为什么买这款呢? 痾…因为便宜…对! 这款只要台票$800多而已,当然我相信还有更好的CP值选择,但因为小编我懒得再去找了,所以就直接选择D-Link DCS-930L这款IP CAM。
在写这个范例和文章的时候,我突然想起我一个大学同学叫CCN https://twitter.com/ccn ,这东西在大学的时候他已经有实作过范例了,那个OpenCV不盛行(甚至有没有都不知道)的年代他应该是透过Web Request截图回来变成影像,并且还能控制IP CAM的上下左右旋转机构。
首先你必须照着说明书把DLink-DC930L初始化设定搞定,让他处于可以工作的状态,并使用浏览器测试可正确连到该IP CAM。
然后就准备开专案下载EmguCV,EmguCV 是从OpenCV里包成专给C#用的专门用来处理影像相关的一个函式库,我是透过NuGet直接下载,并解在工具->选择工具箱项目浏览加入Emgu.CV.UI.dll,Visual Studio就会自动帮你加入PictureBox等等的Emgu元件,你就可以在工具箱使用它们了。
程式码的关键其实就是在于 可以使用Capture 这个类别来存放影像,并Show在PictureBox上。
程式码如下
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 System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
namespace EmguCV_10130206
{
public partial class Form1 : Form
{
private Capture _capture = null;
private bool _captureInProgress;
public Form1()
{
InitializeComponent();
CvInvoke.UseOpenCL = false;
try
{
//"http://帳號:密碼@你的IP/video.cgi?.mjpg"
_capture = new Capture("http://admin:MyPW999@192.168.1.110/video.cgi?.mjpg");//
_capture.ImageGrabbed += ProcessFrame;
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ProcessFrame(object sender, EventArgs arg)
{
Mat frame = new Mat();
_capture.Retrieve(frame, 0);
captureImageBox.Image = frame;
}
private void captureButton_Click(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;
}
}
}
}
PS.请忽略我超乱的桌面= =
翻译自:陈二博士