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.image.processing.face.alignment; 031 032import java.io.DataInput; 033import java.io.DataOutput; 034import java.io.IOException; 035import java.util.ArrayList; 036import java.util.List; 037 038import org.openimaj.image.FImage; 039import org.openimaj.image.processing.face.detection.CLMDetectedFace; 040import org.openimaj.image.processing.face.detection.CLMFaceDetector.Configuration; 041import org.openimaj.image.processing.face.tracking.clm.CLMFaceTracker; 042import org.openimaj.image.processing.transform.PiecewiseMeshWarp; 043import org.openimaj.io.IOUtils; 044import org.openimaj.math.geometry.shape.Shape; 045import org.openimaj.math.geometry.shape.Triangle; 046import org.openimaj.util.pair.Pair; 047 048/** 049 * An aligner that warps a {@link CLMDetectedFace} to the neutral pose 050 * (reference shape) of the {@link Configuration}. 051 * 052 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 053 */ 054public class CLMAligner implements FaceAligner<CLMDetectedFace> { 055 private Configuration config; 056 private int size = 100; 057 private transient List<Triangle> referenceTriangles; 058 private transient FImage mask; 059 060 /** 061 * Construct a new {@link CLMAligner} using the default 062 * {@link Configuration} and default size of 100 pixels. 063 */ 064 public CLMAligner() { 065 config = new Configuration(); 066 loadReference(); 067 } 068 069 /** 070 * Construct a new {@link CLMAligner} using the default 071 * {@link Configuration} and given size for the aligned output image. 072 * 073 * @param size 074 * the output facial patch size 075 */ 076 public CLMAligner(int size) { 077 this.size = size; 078 config = new Configuration(); 079 loadReference(); 080 } 081 082 private void loadReference() { 083 referenceTriangles = CLMFaceTracker.getTriangles(config.referenceShape, null, this.config.triangles); 084 085 mask = new FImage(size, size); 086 087 for (final Triangle t : referenceTriangles) { 088 // magic numbers chosen to scale and centre the face 089 // with a small border 090 t.scale(0.3f * size); 091 t.translate(0.5f * size, 0.45f * size); 092 093 mask.drawShapeFilled(t, 1f); 094 } 095 } 096 097 @Override 098 public void readBinary(DataInput in) throws IOException { 099 config = IOUtils.read(in); 100 loadReference(); 101 } 102 103 @Override 104 public byte[] binaryHeader() { 105 return this.getClass().getName().getBytes(); 106 } 107 108 @Override 109 public void writeBinary(DataOutput out) throws IOException { 110 IOUtils.write(config, out); 111 } 112 113 @Override 114 public FImage align(CLMDetectedFace face) { 115 if (face == null) 116 return null; 117 118 final List<Triangle> triangles = CLMFaceTracker.getTriangles( 119 face.getShapeMatrix(), face.getVisibility(), this.config.triangles); 120 final List<Pair<Shape>> matches = computeMatches(triangles); 121 122 final PiecewiseMeshWarp<Float, FImage> pmw = new 123 PiecewiseMeshWarp<Float, FImage>(matches); 124 125 return pmw.transform(face.getFacePatch(), size, size); 126 } 127 128 @Override 129 public FImage getMask() { 130 return mask; 131 } 132 133 private List<Pair<Shape>> computeMatches(List<Triangle> triangles) { 134 final List<Pair<Shape>> mtris = new ArrayList<Pair<Shape>>(); 135 136 for (int i = 0; i < triangles.size(); i++) { 137 final Triangle t1 = triangles.get(i); 138 final Triangle t2 = referenceTriangles.get(i); 139 140 if (t1 != null && t2 != null) { 141 mtris.add(new Pair<Shape>(t1, t2)); 142 } 143 } 144 145 return mtris; 146 } 147}