Emgu.CV.HaarCascade.Detect C# (CSharp) Method

Detect() public method

Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles. The function scans the image several times at different scales (see cvSetImagesForHaarClassifierCascade). Each time it considers overlapping regions in the image and applies the classifiers to the regions using cvRunHaarClassifierCascade. It may also apply some heuristics to reduce number of analyzed regions, such as Canny prunning. After it has proceeded and collected the candidate rectangles (regions that passed the classifier cascade), it groups them and returns a sequence of average rectangles for each large enough group. The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned for accurate yet slow object detection. For a faster operation on real video images the settings are: scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, min_size=<minimum possible face size> (for example, ~1/4 to 1/16 of the image area in case of video conferencing).
public Detect ( Byte>.Image image, double scaleFactor, int minNeighbors, CvEnum flag, Size minSize, Size maxSize ) : Emgu.CV.Structure.MCvAvgComp[]
image Byte>.Image The image where the objects are to be detected from
scaleFactor double The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%
minNeighbors int Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure
flag CvEnum Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing.
minSize System.Drawing.Size Minimum window size. Use Size.Empty for default, where it is set to the size of samples the classifier has been trained on (~20x20 for face detection)
maxSize System.Drawing.Size Maxumum window size. Use Size.Empty for default, where the parameter will be ignored.
return Emgu.CV.Structure.MCvAvgComp[]
        public MCvAvgComp[] Detect(Image<Gray, Byte> image, double scaleFactor, int minNeighbors, CvEnum.HAAR_DETECTION_TYPE flag, Size minSize, Size maxSize)
        {
            using (MemStorage stor = new MemStorage())
             {
            IntPtr objects = CvInvoke.cvHaarDetectObjects(
                image.Ptr,
                Ptr,
                stor.Ptr,
                scaleFactor,
                minNeighbors,
                flag,
                minSize,
                maxSize);

            if (objects == IntPtr.Zero)
               return new MCvAvgComp[0];

            Seq<MCvAvgComp> rects = new Seq<MCvAvgComp>(objects, stor);
            return rects.ToArray();
             }
        }

Same methods

HaarCascade::Detect ( Byte>.Image image ) : Emgu.CV.Structure.MCvAvgComp[]

Usage Example

Ejemplo n.º 1
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var sourceImage = new Bitmap("C:\\Steve_Wozniak.jpg");

            string haarcascade = "haarcascade_frontalface_default.xml";

            using (HaarCascade face = new HaarCascade(haarcascade))
            {
                var image = new Image<Rgb, Byte>(sourceImage);

                using (var gray = image.Convert<Gray, Byte>())
                {
                    var detectedFaces = face.Detect(
                                            gray,
                                            1.1,
                                            10,
                                            Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                            new System.Drawing.Size(20, 20));

                    var firstFace = detectedFaces[0];
                    System.Drawing.Bitmap bmpImage = image.Bitmap;
                    System.Drawing.Bitmap bmpCrop = bmpImage.Clone(firstFace.rect,
                                                                    bmpImage.PixelFormat);

                    var cropedImage = new Image<Rgb, Byte>(bmpCrop);

                    MainImage.Source = ToBitmapSource(sourceImage);
                    DetectedFaceImage.Source = ToBitmapSource(cropedImage.Bitmap);
                }
            }
        }
All Usage Examples Of Emgu.CV.HaarCascade::Detect