001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.facestuff;
031
032import java.io.File;
033import java.io.IOException;
034import java.net.MalformedURLException;
035
036import org.openimaj.image.FImage;
037import org.openimaj.image.ImageUtilities;
038import org.openimaj.image.processing.face.alignment.AffineAligner;
039import org.openimaj.image.processing.face.alignment.FaceAligner;
040import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector;
041import org.openimaj.image.processing.face.detection.keypoints.FacialKeypoint;
042import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace;
043import org.openimaj.math.geometry.point.Point2dImpl;
044import org.openimaj.math.geometry.transforms.TransformUtilities;
045
046public class Alignment {
047        public static void main(String[] args) throws MalformedURLException, IOException {
048                final FImage img = ImageUtilities.readF(new File(
049                                "/Volumes/Raid/face_databases/lfw/Aaron_Peirsol/Aaron_Peirsol_0002.jpg"));
050                final FKEFaceDetector detector = new FKEFaceDetector(1.6f);
051                final FaceAligner<KEDetectedFace> aligner = new AffineAligner(125, 160, 0.1f);
052
053                final KEDetectedFace face = detector.detectFaces(img).get(0);
054
055                final int facePatchSize = Math.max(160, 125);
056                final double size = facePatchSize + 2.0 * facePatchSize * 0.1f;
057                final double sc = 80 / size;
058
059                final float[][] Pmu = {
060                                { 25.0347f, 34.1802f, 44.1943f, 53.4623f, 34.1208f, 39.3564f, 44.9156f, 31.1454f, 47.8747f },
061                                { 34.1580f, 34.1659f, 34.0936f, 33.8063f, 45.4179f, 47.0043f, 45.3628f, 53.0275f, 52.7999f } };
062
063                FImage model = new FImage(125, 160);
064                for (int i = 0; i < Pmu[0].length; i++) {
065                        Point2dImpl pt = new Point2dImpl(Pmu[0][i], Pmu[1][i]);
066
067                        pt = pt.transform(TransformUtilities.scaleMatrixAboutPoint(1 / sc, 1 / sc, new Point2dImpl(Pmu[0][0],
068                                        Pmu[1][0])));
069
070                        model.drawPoint(pt, 1f, 3);
071                }
072
073                for (final FacialKeypoint kpt : face.getKeypoints())
074                        img.drawPoint(
075                                        kpt.position.transform(TransformUtilities.translateMatrix(face.getBounds().x, face.getBounds().y)),
076                                        1f, 3);
077                model = model.inverse();
078                ImageUtilities.write(img, new File("/Users/jsh2/keypoints.png"));
079                ImageUtilities.write(model, new File("/Users/jsh2/model.png"));
080                ImageUtilities.write(aligner.align(face), new File("/Users/jsh2/aligned.png"));
081        }
082}