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 * </p> 050 * 051 * @see FImage#process(ImageProcessor) 052 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 053 */ 054public class HistogramAnalyser implements ImageAnalyser<FImage> 055{ 056 /** The number of bins in the histogram */ 057 private int nbins; 058 059 /** The histogram being built */ 060 private Histogram histogram; 061 062 /** 063 * Default constructor that builds a histogram processor that will create 064 * histograms with the given number of bins. 065 * 066 * @param nbins 067 * The number of bins. 068 */ 069 public HistogramAnalyser(int nbins) { 070 this.nbins = nbins; 071 } 072 073 /** 074 * Computes the Histogram for this image. The assumption is that the image 075 * has been normalised to the range 0..1. Values greater than 1 will be 076 * placed in the top bin. 077 * 078 * @param image 079 * The image from which to extract histogram 080 */ 081 @Override 082 public void analyseImage(FImage image) { 083 this.histogram = new Histogram(nbins); 084 for (int r = 0; r < image.height; r++) 085 { 086 for (int c = 0; c < image.width; c++) 087 { 088 int bin = (int) (image.pixels[r][c] * nbins); 089 if (bin > (nbins - 1)) 090 bin = nbins - 1; 091 histogram.values[bin]++; 092 } 093 } 094 } 095 096 /** 097 * Returns the histogram that was built having run the processing function. 098 * This will return null if the processing has not yet been run. 099 * 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}