public List<FaceScored> FindFaces(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image, CascadeClassifier cascadeClassifierFace, CascadeClassifier cascadeClassifierEye)
        {
            List<FaceScored> currentFaces = new List<FaceScored>();
            using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>())
            {
            gray._EqualizeHist();
            Size minFaceSize = new Size(minSizeFace , minSizeFace );
            Size maxFaceSize =  new Size(maxSizeFace , maxSizeFace );
            Size minEyeSize = new Size(minSizeEye , minSizeEye );
            Size maxEyeSize =  new Size(maxSizeEye , maxSizeEye );
            Rectangle[] facesDetected = cascadeClassifierFace.DetectMultiScale(gray, scaleFace , neighborsFace , minFaceSize,maxFaceSize);

            foreach (Rectangle f in facesDetected)
            {
                if (f.Width<35)
                    break;
                gray.ROI = f;

                Rectangle[] eyesDetected = cascadeClassifierEye.DetectMultiScale(gray, scaleEye, neighborsEye, minEyeSize, maxEyeSize);
                if (eyesDetected.Count() >0){
                    FaceScored faceModel = new FaceScored();
                    faceModel.FaceImage = gray.Bitmap;
                    faceModel.FaceImageFullColr = image.GetSubRect(f).Bitmap;
                    faceModel.Height = faceModel.FaceImage.Height;
                    faceModel.Width = faceModel.FaceImage.Width;
                    faceModel.EyesCount = eyesDetected.Count();

                    Gray avgf = new Gray();
                    MCvScalar avstd = new MCvScalar();
                    gray.AvgSdv(out avgf, out avstd);
                    faceModel.StdDev = avstd.V0;

                    currentFaces.Add(faceModel);
                    if(currentFaces.Count%5==0)
                        Console.WriteLine("FaceDetect Add every 5 faceModel" + faceModel.Width);
                    break;
                }
                gray.ROI = Rectangle.Empty;
                }
            }
            return currentFaces;
        }
Пример #2
0
 /// <summary>
 /// Find pattern in image region
 /// </summary>
 /// <param name="img">Image to find pattern in</param>
 /// <param name="roi">Region of interest</param>
 /// <param name="image_points">Image points relative to original image</param>
 /// <returns></returns>
 public bool FindPattern(Emgu.CV.Image<Gray, byte> img, Rectangle roi, out PointF[] image_points) {
   try {
     if (!roi.IsEmpty) {
       Emgu.CV.Image<Gray, byte> selected = img.GetSubRect(roi); // Shares memory with original image
       bool found = this.FindPattern(selected, out image_points);
       // Transform points back to original image coordinates
       PointF origin = roi.Location;
       for (int i = 0; i < image_points.Length; i++) {
         image_points[i] = new PointF(image_points[i].X + origin.X, image_points[i].Y + origin.Y);
       }
       return found;
     } else {
       image_points = new PointF[0];
       return false;
     }
   } catch (Emgu.CV.CvException) {
     image_points = new PointF[0];
     return false;
   }
 }