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.analysis.algorithm.histogram;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.analyser.ImageAnalyser;
34  import org.openimaj.image.processor.ImageProcessor;
35  import org.openimaj.math.statistics.distribution.Histogram;
36  
37  /**
38   * An {@link ImageAnalyser} that processes an image and generates a
39   * {@link Histogram}.
40   * <p>
41   * You can get the histogram for an image like so: <code><pre>
42   * 	{@code
43   * 		FImage img = new FImage( ... );
44   * 		HistogramProcessor hp = new HistogramProcessor( 64 );
45   * 		img.analyse( hp );
46   * 		Histogram h = hp.getHistogram();
47   * 	}
48   * 	</pre></code>
49   *  </p>
50   *
51   * @see FImage#process(ImageProcessor)
52   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
53   */
54  public class HistogramAnalyser implements ImageAnalyser<FImage>
55  {
56  	/** The number of bins in the histogram */
57  	private int nbins;
58  
59  	/** The histogram being built */
60  	private Histogram histogram;
61  
62  	/**
63  	 * Default constructor that builds a histogram processor that will create
64  	 * histograms with the given number of bins.
65  	 *
66  	 * @param nbins
67  	 *            The number of bins.
68  	 */
69  	public HistogramAnalyser(int nbins) {
70  		this.nbins = nbins;
71  	}
72  
73  	/**
74  	 * Computes the Histogram for this image. The assumption is that the image
75  	 * has been normalised to the range 0..1. Values greater than 1 will be
76  	 * placed in the top bin.
77  	 *
78  	 * @param image
79  	 *            The image from which to extract histogram
80  	 */
81  	@Override
82  	public void analyseImage(FImage image) {
83  		this.histogram = new Histogram(nbins);
84  		for (int r = 0; r < image.height; r++)
85  		{
86  			for (int c = 0; c < image.width; c++)
87  			{
88  				int bin = (int) (image.pixels[r][c] * nbins);
89  				if (bin > (nbins - 1))
90  					bin = nbins - 1;
91  				histogram.values[bin]++;
92  			}
93  		}
94  	}
95  
96  	/**
97  	 * Returns the histogram that was built having run the processing function.
98  	 * This will return null if the processing has not yet been run.
99  	 *
100 	 * @return The {@link Histogram} that was built.
101 	 */
102 	public Histogram getHistogram()
103 	{
104 		return histogram;
105 	}
106 
107 	/**
108 	 * Quickly create a histogram from an image.
109 	 *
110 	 * @param image
111 	 *            the image
112 	 * @param nbins
113 	 *            the number of bins per band
114 	 * @return a histogram
115 	 */
116 	public static Histogram getHistogram(FImage image, int nbins) {
117 		final HistogramAnalyser p = new HistogramAnalyser(nbins);
118 		image.analyseWith(p);
119 		return p.getHistogram();
120 	}
121 }