View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.processing.algorithm;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.processor.ImageProcessor;
34  import org.openimaj.image.processor.SinglebandImageProcessor;
35  
36  /**
37   * An {@link ImageProcessor} that performs histogram equalisation (projecting
38   * the colours back into the image).
39   *
40   * @author David Dupplaw (dpd@ecs.soton.ac.uk)
41   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
42   *
43   * @created 31 Mar 2011
44   */
45  public class EqualisationProcessor implements SinglebandImageProcessor<Float, FImage> {
46  	/**
47  	 * Equalise the colours in the image. Creates a histogram that contains as
48  	 * many bins as colours, equalises it, then back-projects it into an image.
49  	 * The resulting image has equalised values between 0 and 1. It assumes the
50  	 * image has already been normalised such that its values are also between 0
51  	 * and 1.
52  	 *
53  	 * It is assumed that there are 256 discrete grey-levels.
54  	 *
55  	 * @see "http://www.generation5.org/content/2004/histogramEqualization.asp"
56  	 */
57  	@Override
58  	public void processImage(FImage image) {
59  		// This will be a histogram of all intensities
60  		final int[] hg = new int[256];
61  
62  		// Create the histogram
63  		for (int r = 0; r < image.height; r++) {
64  			for (int c = 0; c < image.width; c++) {
65  				final int i = Math.round(255 * image.pixels[r][c]);
66  				hg[i]++;
67  			}
68  		}
69  
70  		// Create cumulative histogram
71  		for (int i = 1; i < 256; i++) {
72  			hg[i] += hg[i - 1];
73  		}
74  
75  		// The assumption is that the max value will be 1
76  		final float alpha = 255f / (image.getWidth() * image.getHeight());
77  
78  		// Back-project into the new image
79  		for (int r = 0; r < image.height; r++) {
80  			for (int c = 0; c < image.width; c++) {
81  				final int i = Math.round(255 * image.pixels[r][c]);
82  				image.pixels[r][c] = Math.round(hg[i] * alpha) / 255.0f;
83  			}
84  		}
85  	}
86  }