Research and Development 1/^Archief/2008-2009/Gezichts detectie/Onderzoek/NNBFD/bestanden/HistogramEqualizer.java
<source lang=java> /**
* */
package nl.ru.rd.facedetection.nnbfd;
/**
* A histrogram equalizer to be used in the preprocessor. * * @author Christiaan Thijssen s0814970 * @author Kevin Reintjes s0814954 * @author Wouter Geraedts (s0814857 - wgeraedts) PGP 66AA5935 * */
public class HistogramEqualizer { final static short Levels = 256;
/** * Equalize the matrix using histogram equalization. * * @param matrix * The matrix to be equalized */ public void equalize(Matrix matrix) { short[] histogram = new short[Levels]; for(int i = 0; i < Levels; i++) { histogram[i] = 0; } for(int y = 0; y < matrix.getHeight(); y++) { for(int x = 0; x < matrix.getWidth(); x++) { histogram[matrix.getValue(x, y)]++; } } short mincdf = cummulate(histogram); modifyImageMatrix(matrix, histogram, mincdf);
}
/** * Calculates the cummalative values of the histogram * * @param histogram * The histogram to be cummulated * @return The minimum value of the histogram */ private short cummulate(short[] histogram) { short mincdf = 255; for(int i = 1; i < Levels; i++) { histogram[i] = (short) (histogram[i] + histogram[i - 1]); if(mincdf == 255 && histogram[i] > 0) { mincdf = histogram[i]; } } return mincdf; }
/** * Changes the matrix according the cummulative values of the histogram * * @param matrix * The matrix to be changed * @param histogram * The cummulative values of the histogram * @param mincdf * The minimum value of the cummulative histogram */ private void modifyImageMatrix(Matrix matrix, short[] histogram, short mincdf) { for(int y = 0; y < matrix.getHeight(); y++) { for(int x = 0; x < matrix.getWidth(); x++) { short newvalue = (short) (Math.round(((histogram[matrix.getValue(x, y)] - mincdf) / (matrix.getHeight() * matrix.getWidth() - mincdf)) * (Levels - 1))); matrix.setValue(x, y, newvalue); } } } }
</source>