Beispiel #1
0
        public static void Run()
        {
            IImage image;
            Bitmap bmpImg;
            Bitmap croppedBitmap;
            string uniqueImgPath;
            string folderImgSource = @"testImages/SourceImg";
            string folderImgFaces  = @"testImages/GeneratedFaces";

            //Read all files in the source image folder
            var sourceImgFiles = Directory.GetFiles(folderImgSource, "*.*", SearchOption.AllDirectories);            //List<string> imgFiles = new List<string>();

            foreach (string fileName in sourceImgFiles)
            {
                // convert source image to UMat format (an array class)
                image = new UMat(fileName, ImreadModes.Color);

                List <Rectangle> faces = new List <Rectangle>();

                // call FindFace class to find faces in source imgs
                FindFace.FindFaceRegions(image, "haarcascade_frontalface_default.xml", faces);

                // Loop over every face found
                foreach (Rectangle face in faces)
                {
                    // the rectangle is drawn around the detected face
                    CvInvoke.Rectangle(image, face, new Bgr(Color.White).MCvScalar, 3);

                    // Source image converted to bitmap
                    bmpImg = image.Bitmap;

                    // a call to cropBitmap function, input: source and rectangle
                    croppedBitmap = cropBitmap(bmpImg, face);

                    // cropped bitmap converted to image
                    Image <Bgr, Byte> croppedImg = new Image <Bgr, Byte>(croppedBitmap);

                    // display image for testing
                    // CvInvoke.Imshow("Cropped Face Image", croppedImg);

                    // generates unique name for the cropped face image
                    var uniqueImgName = string.Format("/{0}.bmp", Guid.NewGuid());

                    // establishes file path where to save the cropped face image
                    uniqueImgPath = folderImgFaces + uniqueImgName;

                    // saves the cropped face image to a specific directory
                    croppedImg.Save(uniqueImgPath);
                }

                //display image for testing
                // using (InputArray iaImage = image.GetInputArray())
                // ImageViewer.Show(image, String.Format("Recognized Original"));
            }
        }
        public static void processCropImg(string imgName)
        {
            IImage image;
            Bitmap bmpImg;
            Bitmap croppedBitmap;
            string uniqueImgPath      = @"testImages/ShowFaces/main.bmp";
            string fileName           = imgName;
            string processThisImgPath = @"testImages/ShowFaces/main_resized.bmp";
            int    maxHeight          = 80;
            int    maxWidth           = 80;

            // convert source image to UMat format (an array class)
            image = new UMat(fileName, ImreadModes.Color);

            List <Rectangle> faces = new List <Rectangle>();

            // call FindFace class to find faces in source imgs
            FindFace.FindFaceRegions(image, "haarcascade_frontalface_default.xml", faces);

            // Loop over every face found
            foreach (Rectangle face in faces)
            {
                // the rectangle is drawn around the detected face
                CvInvoke.Rectangle(image, face, new Bgr(Color.White).MCvScalar, 3);

                // Source image converted to bitmap
                bmpImg = image.Bitmap;

                // a call to cropBitmap function, input: source and rectangle
                croppedBitmap = cropBitmap(bmpImg, face);

                // cropped bitmap converted to image
                Image <Bgr, Byte> croppedImg = new Image <Bgr, Byte>(croppedBitmap);

                // saves the cropped face image to a specific directory
                croppedImg.Save(uniqueImgPath);

                // calls the resize function
                resizeImage(croppedBitmap, maxHeight, maxWidth);

                // calls for processing functions
                processImage(processThisImgPath);
            }
        }