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.pixel.statistics;
31  
32  import org.openimaj.feature.FeatureVectorProvider;
33  import org.openimaj.image.MBFImage;
34  import org.openimaj.math.statistics.distribution.MultidimensionalHistogram;
35  import org.openimaj.util.pair.Pair;
36  
37  /**
38   * A multidimensional histogram calculated from image pixels (assumes image is
39   * in 0-1 range)
40   * 
41   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
42   * 
43   */
44  public class HistogramModel extends AbstractPixelStatisticsModel
45  		implements
46  		FeatureVectorProvider<MultidimensionalHistogram>
47  {
48  	private static final long serialVersionUID = 1L;
49  
50  	/**
51  	 * The histogram data
52  	 */
53  	public MultidimensionalHistogram histogram;
54  
55  	/**
56  	 * Construct with the given number of bins per dimension
57  	 * 
58  	 * @param nbins
59  	 *            the number of bins in each dimension for the histograms
60  	 */
61  	public HistogramModel(int... nbins) {
62  		super(nbins.length);
63  
64  		assert (nbins.length > 0);
65  
66  		histogram = new MultidimensionalHistogram(nbins);
67  	}
68  
69  	@Override
70  	public void estimateModel(MBFImage... images) {
71  		reset();
72  		for (final MBFImage im : images) {
73  			accum(im);
74  		}
75  		histogram.normalise();
76  	}
77  
78  	protected void reset() {
79  		for (int i = 0; i < histogram.values.length; i++)
80  			histogram.values[i] = 0;
81  	}
82  
83  	/**
84  	 * For a given index, map to the range of colours which could map to it
85  	 * 
86  	 * @param index
87  	 * @return start/end colour
88  	 */
89  	public Pair<float[]> colourRange(int index) {
90  		final int[] coord = this.histogram.getCoordinates(index);
91  		final float[] start = new float[coord.length];
92  		final float[] end = new float[coord.length];
93  		final int[] nbins = histogram.nbins;
94  		for (int i = 0; i < coord.length; i++) {
95  			start[i] = (float) coord[i] / (float) nbins[i];
96  			end[i] = ((float) coord[i] + 1) / nbins[i];
97  		}
98  		return new Pair<float[]>(start, end);
99  	}
100 
101 	/**
102 	 * For a given index, get the average colour which would map to it
103 	 * 
104 	 * @param index
105 	 * @return start/end colour
106 	 */
107 	public float[] colourAverage(int index) {
108 		final int[] coord = this.histogram.getCoordinates(index);
109 		final float[] average = new float[coord.length];
110 		final int[] nbins = histogram.nbins;
111 		for (int i = 0; i < coord.length; i++) {
112 			final float start = (float) coord[i] / (float) nbins[i];
113 			final float end = ((float) coord[i] + 1) / nbins[i];
114 			average[i] = (start + end) / 2f;
115 		}
116 
117 		return average;
118 	}
119 
120 	protected void accum(MBFImage im) {
121 		final int height = im.getHeight();
122 		final int width = im.getWidth();
123 		final int[] bins = new int[ndims];
124 
125 		final float[][][] bands = new float[im.numBands()][][];
126 		for (int i = 0; i < bands.length; i++)
127 			bands[i] = im.getBand(i).pixels;
128 
129 		final int[] nbins = histogram.nbins;
130 		final double[] values = histogram.values;
131 
132 		for (int y = 0; y < height; y++) {
133 			for (int x = 0; x < width; x++) {
134 				for (int i = 0; i < ndims; i++) {
135 					bins[i] = (int) (bands[i][y][x] * (nbins[i]));
136 					if (bins[i] >= nbins[i])
137 						bins[i] = nbins[i] - 1;
138 				}
139 
140 				int bin = 0;
141 				for (int i = 0; i < ndims; i++) {
142 					int f = 1;
143 					for (int j = 0; j < i; j++)
144 						f *= nbins[j];
145 
146 					bin += f * bins[i];
147 				}
148 
149 				values[bin]++;
150 			}
151 		}
152 	}
153 
154 	@Override
155 	public String toString() {
156 		return histogram.toString();
157 	}
158 
159 	@Override
160 	public HistogramModel clone() {
161 		final HistogramModel model = new HistogramModel();
162 		model.histogram = histogram.clone();
163 		model.ndims = ndims;
164 		return model;
165 	}
166 
167 	@Override
168 	public MultidimensionalHistogram getFeatureVector() {
169 		return histogram;
170 	}
171 }