C#窗体摄像头应用

请查看原文:http://blog.csdn.net/cooska/article/details/8265884


以前做小车控制的时候,最激动的功能就是人脸识别,小车自动根据人脸的位置来转向

不过那时候用emgu是因为它工作在c#环境下,而且能很方便的控制摄像头。

现在上位机和摄像头不是直接联系,通过了wifi,而且703路由里openwrt系统的mjpg-stream发送出来的要么是图片要么就是mjpg流。

在网上逛了很久也没发现emgu能实时获取视频流。

 

后来发现另外一款关于机器视觉的库  aforge.net能实时获取。小开心了下,把基本代码发上来供有兴趣的人看看。

先从官网下载库文件,有这几个是我目前用到的:

AForge.Controls.dll、AForge.dll、AForge.Imaging.dll、AForge.Video.DirectShow.dll、AForge.Video.dll

其中controls.dll是控件库,可以加载到工具箱里。

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;

然后在窗体上拖进去一个VideoSourcePlayer控件,

在form_load里加上

            if (videoSourcePlayer.VideoSource != null)
            {

                videoSourcePlayer.SignalToStop();

                videoSourcePlayer.WaitForStop();

            }

然后写两个调用函数,

        private void connectVidio()
        {

            MJPEGStream mjpegSource = new MJPEGStream(http://ip:端口/?action=stream);  //这里是从路由发出的视频流的地址,根据实际情况来写

            OpenVideoSource(mjpegSource);
        }

        private void OpenVideoSource(IVideoSource source)
        {

            videoSourcePlayer.SignalToStop();

            videoSourcePlayer.WaitForStop();

            videoSourcePlayer.VideoSource = source;

            videoSourcePlayer.Start();

        }
然后调用connectVidio就能在窗体上看到视频了。

不过刚刚接触aforge,其他很多无比强大的功能都还不会,据说有神经网络,强化算法等,有很多网友用这个库做了物体追踪的功能,佩服啊。

好想念以前的emgu,至少用的还算熟练。

突发奇想,用aforge获取的视频流的帧,供emgu分析处理后放到窗体显示出来。

试试先,呵呵。。。  这里做个人脸识别看效果如何

先引用

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.GPU;
using Emgu.Util;

emgu例子里有个faceditect类,直接拷贝过来用。

videoSourcePlayer的newframe事件里加上以下代码

        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            Image<Bgr, Byte> img = new Image<Bgr, Byte>(image);
            img = img.PyrDown();
            long detectionTime;
            List<Rectangle> faces = new List<Rectangle>();
            List<Rectangle> eyes = new List<Rectangle>();
            DetectFace.Detect(img, "detectorxml/haarcascade_frontalface_default.xml", "detectorxml/haarcascade_eye.xml", faces, eyes, out detectionTime);
            foreach (Rectangle face in faces)
                img.Draw(face, new Bgr(Color.Red), 1);
            foreach (Rectangle eye in eyes)
                img.Draw(eye, new Bgr(Color.Blue),1);
            image=img.ToBitmap();

        }

还真的实现了,只是效率有点低。。。。再慢慢研究下吧

 放上效果图

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值