package ac.essex.ooechs.imaging.commons.apps.training.strategies; import ac.essex.ooechs.imaging.commons.Pixel; import ac.essex.ooechs.imaging.commons.util.Region; import ac.essex.ooechs.imaging.commons.apps.training.strategies.CroppingStrategy; import ac.essex.ooechs.imaging.commons.apps.training.strategies.StrategyException; import java.util.Vector; /** * <p/> * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version, * provided that any use properly credits the author. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details at http://www.gnu.org * </p> * * @author Olly Oechsle, University of Essex, Date: 17-Oct-2006 * @version 1.0 */ public class MouthCroppingStrategy implements CroppingStrategy { public String getName() { return "Mouth Cropper"; } public String getDescription() { return "Select the outer edges of the mouth on the left and right to create the cropped region"; } public int countNodes() { return 2; } public double getAspectRatio() { return 1; } public Region getCropRegion(Vector<Pixel> nodes) throws StrategyException { if (nodes.size() != countNodes()) { throw new StrategyException("Cannot crop, I need two eye positions to create a face!"); } Pixel node1 = nodes.elementAt(0); Pixel node2 = nodes.elementAt(1); // put eyes in correct order Pixel leftSide, rightSide; Pixel upperSide, lowerSide; if (node1.x < node2.x) { leftSide = node1; rightSide = node2; } else { leftSide = node2; rightSide = node1; } if (node1.y < node2.y) { upperSide = node1; lowerSide = node2; } else { upperSide = node2; lowerSide = node1; } // and find center point double eyeDistX = rightSide.x - leftSide.x; double eyeDistY = lowerSide.y - upperSide.y; double centerX = leftSide.x + (eyeDistX / 2); double centerY = upperSide.y + (eyeDistY / 2); // build crop region around the centerY; // width is going to be twice the eye distance. // height is width / aspect ratio double width = eyeDistX * 1d; double height = width / getAspectRatio(); double left = centerX - (width / 2); double top = centerY - (height * 0.4); return new Region((int) left, (int) top, (int) width, (int)height); } }