Example #1
0
 public void DetectObj(Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> image)
 {
     if (m_harrDetector == null)
     {
         throw new ArgumentNullException("HaarCascade", "haarCascade not initilized.");
     }
     m_NoseDetected = image.DetectHaarCascade(
         m_harrDetector,
         1.1,
         10,
         Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
         m_MinNoseSize);
 }
Example #2
0
        public void DetectObj(Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> image)
        {
            if (m_harrDetector == null)
            {
                throw new ArgumentNullException("HaarCascade", "haarCascade not initilized.");
            }

            MCvAvgComp[][] EyeDetected      = null;
            MCvAvgComp[][] LeftEyeDetected  = null;
            MCvAvgComp[][] RightEyeDetected = null;


            EyeDetected = image.DetectHaarCascade(
                m_harrDetector,
                1.1,
                10,
                Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                m_MinEyeSize);

            if (EyeDetected.Length > 1)
            {
                Rectangle rcROI = image.ROI;

                MCvAvgComp eyepair = EyeDetected[0][0];

                int width  = Convert.ToInt32(eyepair.rect.Width * 1.5);
                int height = Convert.ToInt32(eyepair.rect.Height * 1.5);

                Rectangle rectEye = new Rectangle(eyepair.rect.X - 10,
                                                  eyepair.rect.Y - 10, width, height);
                Rectangle rectLeftEye = new Rectangle(eyepair.rect.X - 10,
                                                      eyepair.rect.Y - 10, width, height);
                Rectangle rectRightEye = new Rectangle(eyepair.rect.X - 10 + width / 2,
                                                       eyepair.rect.Y - 10, width, height);

                Rectangle rcLeft  = Rectangle.Empty;
                Rectangle rcRight = Rectangle.Empty;

                if (rcROI != Rectangle.Empty)
                {
                    rectEye.X += rcROI.X;
                    rectEye.Y += rcROI.Y;

                    rectLeftEye.X += rcROI.X;
                    rectLeftEye.Y += rcROI.Y;

                    rectRightEye.X += rcROI.X;
                    rectRightEye.Y += rcROI.Y;
                }
                image.ROI = rectLeftEye;

                if (m_LeftEyeDetector != null)
                {
                    LeftEyeDetected = image.DetectHaarCascade(
                        m_LeftEyeDetector,
                        1.1,
                        10,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        m_MinEyeSize);

                    //Project into no ROI space
                    for (int i = 0; i < LeftEyeDetected[0].Length; i++)
                    {
                        LeftEyeDetected[0][i].rect.X += (rectLeftEye.X);
                        LeftEyeDetected[0][i].rect.Y += (rectLeftEye.Y);
                    }
                }

                image.ROI = rectEye;
                if (LeftEyeDetected[0].Length > 1)
                {// if already detected more than one eyes, then both eye should already be detected
                    foreach (MCvAvgComp eye in LeftEyeDetected[0])
                    {
                        if (rcLeft == Rectangle.Empty || rcLeft.X > eye.rect.X)
                        {
                            rcLeft = eye.rect;
                        }
                        if (rcRight == Rectangle.Empty || rcRight.X < eye.rect.X)
                        {
                            rcRight = eye.rect;
                        }
                    }
                }
                else if (m_RightEyeDetector != null)
                {
                    RightEyeDetected = image.DetectHaarCascade(
                        m_RightEyeDetector,
                        1.1,
                        10,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        m_MinEyeSize);
                    //Project into no ROI space
                    for (int i = 0; i < RightEyeDetected[0].Length; i++)
                    {
                        RightEyeDetected[0][i].rect.X += rectEye.X;
                        RightEyeDetected[0][i].rect.Y += rectEye.Y;
                    }
                    // we only need the right eye
                    rcRight = Rectangle.Empty;
                    foreach (MCvAvgComp eye in RightEyeDetected[0])
                    {
                        if (rcRight == Rectangle.Empty || rcRight.X < eye.rect.X)
                        {
                            rcRight = eye.rect;
                        }
                    }
                }
                m_rcBestLeftEye     = rcLeft;
                m_rcBestRightEye    = rcRight;
                m_rcBestEyeDetected = EyeDetected[0][0].rect;

                //Project into the original ROI
                if (rcROI != Rectangle.Empty)
                {
                    m_rcBestLeftEye.X -= rcROI.X;
                    m_rcBestLeftEye.Y -= rcROI.Y;

                    m_rcBestRightEye.X -= rcROI.X;
                    m_rcBestRightEye.Y -= rcROI.Y;
                }
                image.ROI = rcROI;
            }
        }