예제 #1
0
        /// <summary>
        /// Run over directory, get images, detech faces, resize and storeagain
        /// </summary>
        /// <param name="directory"></param>
        public static void CreateTrainingSet(String mainDirectory, int newSize, bool equalize, int rotation, bool flip)
        {
            string[] subdirEntries = Directory.GetDirectories(mainDirectory);
            foreach (var directory in subdirEntries)
            {
                string[] fileEntries = Directory.GetFiles(directory);
                foreach (var file in fileEntries.Where(x => (!x.Contains("_"))))
                {
                    Image <Gray, byte> image = new Image <Gray, byte>(file);


                    //the images are big - reduce the size to the half
                    image = image.Resize(0.5, INTER.CV_INTER_CUBIC);

                    var haar  = new HaarCascade(FileAccessUtil.GetHaarCascade());
                    var faces = haar.Detect(image);

                    if (faces.Count() == 1)
                    {
                        var face = faces[0];

                        //resize all images to 100
                        var    faceImg = image.Copy(face.rect).Resize(newSize, newSize, INTER.CV_INTER_CUBIC);
                        String imgName = file.Insert(file.IndexOf("."), "_" + newSize.ToString());
                        if (equalize)
                        {
                            imgName = imgName.Insert(file.IndexOf("."), "_N");
                            var equalized = EqualizeHist(faceImg);
                            faceImg = equalized;
                        }

                        faceImg.Save(imgName);

                        //create rotated image if it was demanded
                        if (rotation != 0)
                        {
                            var rotated     = faceImg.Rotate(rotation, new Gray(0.3));
                            var rotatedName = imgName.Insert(file.IndexOf("."), "_R");
                            rotated.Save(rotatedName);
                        }

                        if (flip)
                        {
                            var fliped     = faceImg.Flip(FLIP.HORIZONTAL);
                            var flipedName = imgName.Insert(file.IndexOf("."), "_F");
                            fliped.Save(flipedName);
                        }
                    }
                }
            }
        }
예제 #2
0
        public static Image <Gray, byte> DetectAndTrimFace(int[] pixels, int initialSize, int outputSize)
        {
            var inBitmap = ConvertToBitmap(pixels, initialSize);
            //inBitmap.Save(@"E:\data\phototest\received.bmp");
            var grayframe = new Image <Gray, byte>(inBitmap);

            var haar  = new HaarCascade(FileAccessUtil.GetHaarCascade());
            var faces = haar.Detect(grayframe,
                                    1.2,
                                    3,
                                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new Size(30, 30));


            if (faces.Count() != 1)
            {
                return(null);
            }
            var face = faces[0];

            var returnImage = grayframe.Copy(face.rect).Resize(outputSize, outputSize, INTER.CV_INTER_CUBIC);

            return(returnImage);
        }