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.analysis.algorithm.histogram;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.analyser.ImageAnalyser;
034import org.openimaj.image.processor.ImageProcessor;
035import org.openimaj.math.statistics.distribution.Histogram;
036
037/**
038 * An {@link ImageAnalyser} that processes an image and generates a
039 * {@link Histogram}.
040 * <p>
041 * You can get the histogram for an image like so: <code><pre>
042 *      {@code
043 *              FImage img = new FImage( ... );
044 *              HistogramProcessor hp = new HistogramProcessor( 64 );
045 *              img.analyse( hp );
046 *              Histogram h = hp.getHistogram();
047 *      }
048 *      </pre></code>
049 * 
050 * @see FImage#process(ImageProcessor)
051 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
052 */
053public class HistogramAnalyser implements ImageAnalyser<FImage>
054{
055        /** The number of bins in the histogram */
056        private int nbins;
057
058        /** The histogram being built */
059        private Histogram histogram;
060
061        /**
062         * Default constructor that builds a histogram processor that will create
063         * histograms with the given number of bins.
064         * 
065         * @param nbins
066         *            The number of bins.
067         */
068        public HistogramAnalyser(int nbins) {
069                this.nbins = nbins;
070        }
071
072        /**
073         * Computes the Histogram for this image. The assumption is that the image
074         * has been normalised to the range 0..1. Values greater than 1 will be
075         * placed in the top bin.
076         * 
077         * @param image
078         *            The image from which to extract histogram
079         */
080        @Override
081        public void analyseImage(FImage image) {
082                this.histogram = new Histogram(nbins);
083                for (int r = 0; r < image.height; r++)
084                {
085                        for (int c = 0; c < image.width; c++)
086                        {
087                                int bin = (int) (image.pixels[r][c] * nbins);
088                                if (bin > (nbins - 1))
089                                        bin = nbins - 1;
090                                histogram.values[bin]++;
091                        }
092                }
093        }
094
095        /**
096         * Returns the histogram that was built having run the processing function.
097         * This will return null if the processing has not yet been run.
098         * 
099         * @return The {@link Histogram} that was built.
100         */
101        public Histogram getHistogram()
102        {
103                return histogram;
104        }
105
106        /**
107         * Quickly create a histogram from an image.
108         * 
109         * @param image
110         *            the image
111         * @param nbins
112         *            the number of bins per band
113         * @return a histogram
114         */
115        public static Histogram getHistogram(FImage image, int nbins) {
116                final HistogramAnalyser p = new HistogramAnalyser(nbins);
117                image.analyseWith(p);
118                return p.getHistogram();
119        }
120}