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 * <p> 050 * An aligner that warps a {@link CLMDetectedFace} to the neutral pose 051 * (reference shape) of the {@link Configuration}. 052 * </p> 053 * <p> 054 * Implementors of subclasses of this should note that if the triangles of the 055 * configuration are changed that the reference triangles must be recomputed. 056 * </p> 057 * 058 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 059 */ 060public class CLMAligner implements FaceAligner<CLMDetectedFace> { 061 protected Configuration config; 062 protected int size = 100; 063 protected transient List<Triangle> referenceTriangles; 064 protected transient FImage mask; 065 066 /** 067 * Construct a new {@link CLMAligner} using the default 068 * {@link Configuration} and default size of 100 pixels. 069 */ 070 public CLMAligner() { 071 config = new Configuration(); 072 loadReference(); 073 } 074 075 /** 076 * Construct a new {@link CLMAligner} using the default 077 * {@link Configuration} and given size for the aligned output image. 078 * 079 * @param size 080 * the output facial patch size 081 */ 082 public CLMAligner(int size) { 083 this.size = size; 084 config = new Configuration(); 085 loadReference(); 086 } 087 088 /** 089 * Construct a new {@link CLMAligner} using the provided 090 * {@link Configuration} and default size of 100 pixels. 091 * 092 * @param size 093 * the output facial patch size 094 * @param config 095 * the configuration 096 */ 097 public CLMAligner(int size, Configuration config) { 098 this.size = size; 099 this.config = config; 100 loadReference(); 101 } 102 103 protected void loadReference() { 104 referenceTriangles = CLMFaceTracker.getTriangles(config.referenceShape, null, this.config.triangles); 105 106 mask = new FImage(size, size); 107 108 for (final Triangle t : referenceTriangles) { 109 // magic numbers chosen to scale and centre the face 110 // with a small border 111 t.scale(0.3f * size); 112 t.translate(0.5f * size, 0.45f * size); 113 114 mask.drawShapeFilled(t, 1f); 115 } 116 } 117 118 @Override 119 public void readBinary(DataInput in) throws IOException { 120 config = IOUtils.read(in); 121 loadReference(); 122 } 123 124 @Override 125 public byte[] binaryHeader() { 126 return this.getClass().getName().getBytes(); 127 } 128 129 @Override 130 public void writeBinary(DataOutput out) throws IOException { 131 IOUtils.write(config, out); 132 } 133 134 @Override 135 public FImage align(CLMDetectedFace face) { 136 if (face == null) 137 return null; 138 139 final List<Triangle> triangles = CLMFaceTracker.getTriangles( 140 face.getShapeMatrix(), face.getVisibility(), this.config.triangles); 141 final List<Pair<Shape>> matches = computeMatches(triangles); 142 143 final PiecewiseMeshWarp<Float, FImage> pmw = new PiecewiseMeshWarp<Float, FImage>(matches); 144 145 return pmw.transform(face.getFacePatch(), size, size); 146 } 147 148 @Override 149 public FImage getMask() { 150 return mask; 151 } 152 153 private List<Pair<Shape>> computeMatches(List<Triangle> triangles) { 154 final List<Pair<Shape>> mtris = new ArrayList<Pair<Shape>>(); 155 156 for (int i = 0; i < triangles.size(); i++) { 157 final Triangle t1 = triangles.get(i); 158 final Triangle t2 = referenceTriangles.get(i); 159 160 if (t1 != null && t2 != null) { 161 mtris.add(new Pair<Shape>(t1, t2)); 162 } 163 } 164 165 return mtris; 166 } 167}