001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.processing.algorithm;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.processor.ImageProcessor;
034import org.openimaj.image.processor.SinglebandImageProcessor;
035
036/**
037 * An {@link ImageProcessor} that performs histogram equalisation (projecting
038 * the colours back into the image).
039 *
040 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 *
043 * @created 31 Mar 2011
044 */
045public class EqualisationProcessor implements SinglebandImageProcessor<Float, FImage> {
046        /**
047         * Equalise the colours in the image. Creates a histogram that contains as
048         * many bins as colours, equalises it, then back-projects it into an image.
049         * The resulting image has equalised values between 0 and 1. It assumes the
050         * image has already been normalised such that its values are also between 0
051         * and 1.
052         *
053         * It is assumed that there are 256 discrete grey-levels.
054         *
055         * @see "http://www.generation5.org/content/2004/histogramEqualization.asp"
056         */
057        @Override
058        public void processImage(FImage image) {
059                // This will be a histogram of all intensities
060                final int[] hg = new int[256];
061
062                // Create the histogram
063                for (int r = 0; r < image.height; r++) {
064                        for (int c = 0; c < image.width; c++) {
065                                final int i = Math.round(255 * image.pixels[r][c]);
066                                hg[i]++;
067                        }
068                }
069
070                // Create cumulative histogram
071                for (int i = 1; i < 256; i++) {
072                        hg[i] += hg[i - 1];
073                }
074
075                // The assumption is that the max value will be 1
076                final float alpha = 255f / (image.getWidth() * image.getHeight());
077
078                // Back-project into the new image
079                for (int r = 0; r < image.height; r++) {
080                        for (int c = 0; c < image.width; c++) {
081                                final int i = Math.round(255 * image.pixels[r][c]);
082                                image.pixels[r][c] = Math.round(hg[i] * alpha) / 255.0f;
083                        }
084                }
085        }
086}