ImageBlender.java

package ac.essex.ooechs.imaging.commons; 
 
import ac.essex.ooechs.imaging.commons.StatisticsSolver; 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.fast.FastStatistics; 
import ac.essex.ooechs.imaging.commons.util.ImageFilenameFilter; 
import ac.essex.ooechs.imaging.commons.util.panels.ImagePanel; 
 
import javax.swing.*; 
import java.io.File; 
import java.awt.image.BufferedImage; 
import java.awt.*; 
 
/** 
 * <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: 05-Oct-2006 
 * @version 1.0 
 */ 
public class ImageBlender { 
 
    public static void main(String[] args) throws Exception{ 
        File directory = new File("/home/ooechs/Data/faces/train/face"); 
        new ImageBlender(19, 19, directory); 
 
 
    } 
 
    StatisticsSolver solver; 
 
    public ImageBlender(int width, int height, File directory) throws Exception { 
 
        if (!directory.exists()) { 
            throw new RuntimeException("Directory does not exist."); 
        } 
 
        final int dotsPerLine = 50; 
 
        FastStatistics[][] solvers = new FastStatistics[width][height]; 
 
        File[] files = directory.listFiles(); 
 
        int dotCount = 0; 
 
        for (int i = 0; i < files.length; i++) { 
            File file = files[i]; 
            if (ImageFilenameFilter.isImage(file)) { 
                PixelLoader image = new PixelLoader(file); 
                for (int y = 0; y < height; y++) { 
                    for (int x = 0; x < width; x++) { 
                        if (solvers[x][y] == null) solvers[x][y] = new FastStatistics(); 
                        solvers[x][y].addData(image.getGreyValue(x, y)); 
                    } 
                } 
 
                dotCount++; 
                System.out.print("."); 
                if (dotCount > dotsPerLine) { 
                    System.out.println(); 
                    dotCount = 0; 
                } 
            } 
        } 
 
        // now create the homogenised image 
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
        for (int y = 0; y < height; y++) { 
            for (int x = 0; x < width; x++) { 
                int greyscale = (int) solvers[x][y].getMean(); 
                image.setRGB(x, y, new Color(greyscale, greyscale, greyscale).getRGB()); 
            } 
 
 
        } 
 
        // show it 
        new DisplayFrame(image); 
 
        //new PixelLoader(image).save("/home/ooechs/Data/faces/blended.bmp"); 
 
 
    } 
 
    class DisplayFrame extends JFrame { 
 
        public DisplayFrame(BufferedImage image) { 
 
            super("Blended Image"); 
            Container c = getContentPane(); 
            ImagePanel ip = new ImagePanel(); 
            ip.setImage(image); 
            c.add(ip); 
            setSize(640, 480); 
            setVisible(true); 
 
        } 
 
    } 
 
}