FaceDetector.java

package ac.essex.ooechs.imaging.commons.evolved; 
 
import ac.essex.ooechs.imaging.commons.HaarRegions; 
import ac.essex.ooechs.imaging.commons.Pixel; 
 
import java.awt.*; 
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: 15-Sep-2006 
 * @version 1.1 
 */ 
public abstract class FaceDetector { 
 
    public static final int FACE1 = 1; 
    public static final int FACE2 = 2; 
    public static final int FACE3 = 3; 
    public static final int FACE4 = 4; 
 
    public abstract double calculate(HaarRegions image); 
 
 
    public Vector<Pixel> getObjects(HaarRegions image) { 
 
        Vector<Pixel> objects = new Vector<Pixel>(10); 
 
 
        boolean[][] detectionMap = new boolean[image.getWidth()][image.getHeight()]; 
 
        for (int y = 0; y < (image.getHeight() - image.getWindowHeight()); y += 1) { 
            for (int x = 0; x < (image.getWidth() - image.getWindowWidth()); x += 1) { 
 
                image.setWindowPosition(x, y); 
 
                if ((int) calculate(image) != -1) { 
                    detectionMap[x][y] = true; 
                } 
 
            } 
 
        } 
 
        for (int y = 1; y < (image.getHeight() - image.getWindowHeight()); y += 2) { 
            for (int x = 1; x < (image.getWidth() - image.getWindowWidth()); x += 2) { 
 
                if (detectionMap[x][y]) { 
 
                    int neighbourCount = 0; 
 
                    // count neighbours 
                    for ( int dY = -1; dY <= 1; dY++ ) { 
                        for ( int dX = -1; dX <= 1; dX++ ) { 
                            if (detectionMap[x + dX][y + dY]) neighbourCount++; 
                        } 
                    } 
 
                    if (neighbourCount >= 9) { 
                        objects.add(new Pixel(x, y)); 
                    } 
 
                } 
 
            } 
        } 
 
        return objects; 
 
    } 
 
 
    public void drawObjects(Vector<Pixel> objects, HaarRegions image, Graphics g) { 
        for (int i = 0; i < objects.size(); i++) { 
            Pixel object = objects.elementAt(i); 
            g.setColor(Color.white); 
            g.drawRect(object.x, object.y, image.getWindowWidth(), image.getWindowHeight()); 
        } 
    } 
 
}