예제 #1
0
        // Hair component is a particular component not identified by landmarks
        // This methods extract hair component starting from the bounding box of face and
        // from the bounding box of shape identified by landmarks.
        public Image <Gray, byte> GetHairComponent(Image <Gray, byte> image, PointF[] face, PointF[] shape, BoundingBoxParam hairParam)
        {
            var shapeBox = PointCollection.BoundingRectangle(shape);
            var faceBox  = Rectangle.Ceiling(new RectangleF(face[0].X, face[0].Y, face[1].X - face[0].X, face[1].Y - face[0].Y));

            // calculate height from shape box and top border of image
            var height = image.Size.Height - (image.Size.Height - shapeBox.Top);
            // calculate the normalized height using the desidered aspect ratio
            var normHeight = (int)Math.Floor(faceBox.Width * hairParam.AspectRatio);
            // calculate if the normalized height go out of image border
            var extra = height - normHeight;

            // create the right bounding box, adding on bottom of bounding box the extra height
            var hairBox = (extra < 0) ?
                          new Rectangle(faceBox.X, 0, faceBox.Width, height - extra) :
                          new Rectangle(faceBox.X, extra, faceBox.Width, normHeight);

            var cropHeight = (int)Math.Floor(hairParam.CropWidth * hairParam.AspectRatio);

            return(image.GetSubRect(hairBox).Resize(hairParam.CropWidth, cropHeight, Inter.Area));
        }
예제 #2
0
        // Extract an sub image using creating a bounding box from the landmarks points.
        // Using the BoundingBoxParam, the sub image is resized in order to have same aspect ratio across different subject.
        public Image <Gray, byte> CropToBoundingBox(Image <Gray, byte> image, PointF[] componentPoints, BoundingBoxParam param)
        {
            var bounding   = GetBoundingBox(componentPoints, param.AspectRatio, param.Padding);
            var cropHeight = (int)Math.Ceiling(param.CropWidth * param.AspectRatio);

            return(image.GetSubRect(bounding).Resize(param.CropWidth, cropHeight, Inter.Cubic));
        }
예제 #3
0
        /// <summary>
        /// Align and extract a component from face.
        /// </summary>
        /// <param name="image">Image that contains face.</param>
        /// <param name="component">Landmark points of component. Ex. Mouth, Eyes, Eyebrows, ...</param>
        /// <param name="referenceComponent">Reference landmarks points, which is used for alignement.</param>
        /// <param name="param">Paramters for extract the subimage that contains the aligned component.</param>
        /// <returns>Aligned component.</returns>
        public Image <Gray, byte> AlignComponent(Image <Gray, byte> image, PointF[] component, PointF[] referenceComponent, BoundingBoxParam param)
        {
            var transformation         = Procrustes.OrdinaryAnalysis(component, referenceComponent);
            var alignedImage           = Warp(image, transformation);
            var alignedComponentPoints = CvInvoke.PerspectiveTransform(component, transformation);
            var cropped = CropToBoundingBox(alignedImage, alignedComponentPoints, param);

            return(cropped);
        }