HaarDefinition.java

package ac.essex.ooechs.imaging.commons.test; 
 
import ac.essex.ooechs.imaging.commons.HaarRegions; 
 
import java.io.Serializable; 
 
/** 
 * <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: 08-Nov-2006 
 * @version 1.0 
 */ 
public class HaarDefinition implements Comparable, Serializable { 
 
    public static final int ONE_RECT_FEATURE = 1; 
    public static final int HORIZONTAL_TWO_RECT_FEATURE = 2; 
    public static final int VERTICAL_TWO_RECT_FEATURE = 3; 
    public static final int HORIZONTAL_THREE_RECT_FEATURE = 4; 
    public static final int VERTICAL_THREE_RECT_FEATURE = 5; 
    public static final int FOUR_RECT_FEATURE = 6; 
 
    protected int width, height, x, y; 
    protected double stdDeviation; 
 
    protected int type; 
 
    public HaarDefinition(int type, int width, int height, int xPos, int yPos, double stdDeviation) { 
        this.type = type; 
        this.width = width; 
        this.height = height; 
        this.x = xPos; 
        this.y = yPos; 
        this.stdDeviation = stdDeviation; 
    } 
 
    public int compareTo(Object o) { 
        if (o instanceof HaarDefinition) { 
            HaarDefinition other = (HaarDefinition) o; 
            return other.stdDeviation > stdDeviation ? -1 : 1; 
        } 
        return -1; 
    } 
 
    public double getHaarCalculation(HaarRegions image) { 
        switch (type) { 
            case ONE_RECT_FEATURE: 
                return image.getOneRectangleFeature(x, y, width, height); 
            case HORIZONTAL_TWO_RECT_FEATURE: 
                return image.getTwoRectangleFeature(x, y, width, height, HaarRegions.HORIZONTALLY_ADJACENT, HaarRegions.FIRST_SHAPE); 
            case VERTICAL_TWO_RECT_FEATURE: 
                return image.getTwoRectangleFeature(x, y, width, height, HaarRegions.VERTICALLY_ADJACENT, HaarRegions.FIRST_SHAPE); 
            case HORIZONTAL_THREE_RECT_FEATURE: 
                return image.getThreeRectangleFeature(x, y, width, height, HaarRegions.HORIZONTALLY_ADJACENT); 
            case VERTICAL_THREE_RECT_FEATURE: 
                return image.getThreeRectangleFeature(x, y, width, height, HaarRegions.VERTICALLY_ADJACENT); 
            case FOUR_RECT_FEATURE: 
                return image.getFourRectangleFeature(x, y, width, height, HaarRegions.FIRST_SHAPE); 
        } 
        System.err.println("HaarDefinition.java: Invalid type: " + type); 
        return -1; 
    } 
 
 
    public boolean similarTo(HaarDefinition other) { 
        int threshold = 3; 
        boolean similarXPos = Math.abs(other.x - this.x) <= threshold; 
        boolean similarYPos = Math.abs(other.y - this.y) <= threshold; 
        boolean similarWidth = Math.abs(other.width - this.width) <= threshold; 
        boolean similarHeight = Math.abs(other.height - this.height) <= threshold; 
        return similarXPos && similarYPos && similarWidth && similarHeight; 
    } 
 
    public String toString() { 
        switch (type) { 
            case ONE_RECT_FEATURE: 
                return "image.getOneRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ")"; 
            case HORIZONTAL_TWO_RECT_FEATURE: 
                return "image.getTwoRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ", HaarRegions.HORIZONTALLY_ADJACENT, HaarRegions.FIRST_SHAPE)"; 
            case VERTICAL_TWO_RECT_FEATURE: 
                return "image.getTwoRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ", HaarRegions.VERTICALLY_ADJACENT, HaarRegions.FIRST_SHAPE)"; 
            case HORIZONTAL_THREE_RECT_FEATURE: 
                return "image.getThreeRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ", HaarRegions.HORIZONTALLY_ADJACENT)"; 
            case VERTICAL_THREE_RECT_FEATURE: 
                return "image.getThreeRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ", HaarRegions.VERTICALLY_ADJACENT)"; 
            case FOUR_RECT_FEATURE: 
                return "image.getFourRectangleFeature(" + x + ", " + y + ", " + width + ", " + height + ", HaarRegions.FIRST_SHAPE)"; 
        } 
        System.err.println("HaarDefinition.java: Invalid type: " + type); 
        return "-UNKNOWN_TYPE-";         
    } 
 
}