惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
B
Blog RSS Feed
D
DataBreaches.Net
L
LangChain Blog
月光博客
月光博客
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
美团技术团队
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
小众软件
小众软件
罗磊的独立博客
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta

博客园 - zengqs

很久没有登录了 交警手势信号图解(新)----07年10月1日全国施行 在线幻灯片Slide Show System汇总 - PPT的替代品 转载:高速交警的忠告(为了安全,走高速必看) 转帖:如何替换系统自带的记事本(notepad.exe) 转摘:PCA算法 Haar分类器,ObjectMarker程序源代码 OpenCV训练分类器 - zengqs - 博客园 摘自:Emgu.CV项目,An object recognizer using PCA (Principle Components Analysis) VisualSVN1.6.1破解 视频监控和行为理解的网络资源 高斯背景建模C#版本 高斯背景建模 C#定义一个模板类 转帖:人脸检测概述 C#转换为灰度图的算法 一个人脸检测器 测试用是Windows Live Writer写日志 转帖:关于computer vision的会议及vision guys (zz)
人脸检测的算法 - zengqs - 博客园
zengqs · 2009-02-02 · via 博客园 - zengqs

//静态变量提高访问速度
//Read the HaarCascade objects
static  HaarCascade face = new HaarCascade(".\\haarcascades\\haarcascade_frontalface_alt_tree.xml");
static  HaarCascade eye = new HaarCascade(".\\haarcascades\\haarcascade_frontaleye.xml");
private  static void FaceDetector(Bitmap frame)
{
    //调用OpenCV进行人脸分析与检测

    //转换为8为灰度图像处理

    //首先将图OpenCV的Image格式,允许使用矩阵操作图像
    using (Emgu.CV.Image<Bgr, Byte> image = new Emgu.CV.Image<Bgr, Byte>((Bitmap)frame.Clone()))
    {

        //Emgu.CV.Image<Bgr, Byte> image = new Emgu.CV.Image<Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image 
        Emgu.CV.Image<Gray, Byte> gray = image.Convert<Gray, Byte>(); //Convert it to Grayscale

        //normalizes brightness and increases contrast of the image
        gray._EqualizeHist();
        gray.SmoothMedian(5);//中值滤波

        ////Read the HaarCascade objects
        //HaarCascade face = new HaarCascade(".\\haarcascades\\haarcascade_frontalface_alt_tree.xml");
        //HaarCascade eye = new HaarCascade(".\\haarcascades\\haarcascade_frontaleye.xml");
        //HaarCascade upperBody = new HaarCascade(".\\haarcascades\\haarcascade_upperbody.xml");

        //Detect the faces  from the gray scale image and store the locations as rectangle
        //The first dimensional is the channel
        //The second dimension is the index of the rectangle in the specific channel

        ////检测人体上半部分
        //MCvAvgComp[][] bodysDetected = gray.DetectHaarCascade(upperBody, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));

        Graphics g = Graphics.FromImage(frame);
        //foreach (var cBody in bodysDetected[0])//只考虑一个人的情况
        //{
        //    g.DrawRectangle(penBlue, cBody.rect);
        //    g.DrawString("body detected", font, brush, cBody.rect);

        //检测人脸
        //gray.ROI = gray.rect;
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
        gray.ROI = Rectangle.Empty;

        foreach (MCvAvgComp f in facesDetected[0]) //只有一个人脸
        {
            //Set the region of interest on the faces
            gray.ROI = f.rect;
            MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(eye, 1.1, 1,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,new Size(20, 20));
            gray.ROI = Rectangle.Empty;

            //if there is no eye in the specific region, the region shouldn't contains a face
            //note that we might not be able to recoginize a person who ware glass in this case
            if (eyesDetected[0].Length == 0) continue;

            //draw the face detected in the 0th (gray) channel with blue color
            //image.Draw(f.rect, new Bgr(Color.Blue), 2);

            //foreach (MCvAvgComp e in eyesDetected[0])
            //{
            //    Rectangle eyeRect = e.rect;
            //    eyeRect.Offset(f.rect.X, f.rect.Y);
            //    image.Draw(eyeRect, new Bgr(Color.Red), 2);
            //}

            g.DrawRectangle(penBlue, f.rect);
            g.DrawString("face detected", font, brush, f.rect);

            foreach (MCvAvgComp e in eyesDetected[0])
            {
                Rectangle eyeRect = e.rect;
                eyeRect.Offset(f.rect.X, f.rect.Y);
                //image.Draw(eyeRect, new Bgr(Color.Red), 2);
                g.DrawRectangle(penBlue, eyeRect);
            }
        }
        //}
    }
}