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 gnu.trove.list.array.TFloatArrayList;
33  import gnu.trove.map.hash.TIntIntHashMap;
34  import gnu.trove.procedure.TIntIntProcedure;
35  
36  import org.openimaj.image.FImage;
37  import org.openimaj.image.MBFImage;
38  
39  import static java.lang.Math.sqrt;
40  
41  /**
42   * A model of the all the values of the pixels in an image or set of images,
43   * using basic descriptive statistics (mean, mode, median, range, variance).
44   * 
45   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
46   */
47  public class BasicDescriptiveStatisticsModel extends AbstractPixelStatisticsModel {
48  	private static final long serialVersionUID = 1L;
49  
50  	/**
51  	 * The mean pixel value
52  	 */
53  	public double [] mean;
54  	
55  	/**
56  	 * The mode of pixel values
57  	 */
58  	public double [] mode;
59  	
60  	/**
61  	 * The median of pixel values
62  	 */
63  	public double [] median;
64  	
65  	/**
66  	 * The range of pixel values
67  	 */
68  	public double [] range;
69  	
70  	/**
71  	 * The variance of pixel values
72  	 */
73  	public double [] variance;
74  	
75  	/**
76  	 * Construct a BasicDescriptiveStatisticsModel with the given
77  	 * number of dimensions. The number of dimensions should normally 
78  	 * be equal to the number of bands in the images from which the model
79  	 * is to be estimated. 
80  	 *  
81  	 * @param ndims number of dimensions
82  	 */
83  	public BasicDescriptiveStatisticsModel(int ndims) {
84  		super(ndims);
85  		
86  		mean = new double[ndims];
87  		mode = new double[ndims];
88  		median = new double[ndims];
89  		range = new double[ndims];
90  		variance = new double[ndims];
91  	}
92  
93  	@Override
94  	public void estimateModel(MBFImage... images) {	
95  		for (int i=0; i<ndims; i++) {
96  			mean[i] = 0;
97  			TFloatArrayList values = new TFloatArrayList();
98  			TIntIntHashMap freqs = new TIntIntHashMap();
99  			
100 			int count = 0;
101 			for (MBFImage im : images) {
102 				FImage band = im.getBand(i);
103 				
104 				for (int r=0; r<band.height; r++) {
105 					for (int c=0; c<band.width; c++) {
106 						float val = band.pixels[r][c];
107 						mean[i] += val;
108 						values.add(val);
109 						freqs.adjustOrPutValue(Math.round(val*255F), 1, 1);
110 						count++;
111 					}
112 				}
113 			}
114 			
115 			//mean
116 			mean[i] /= count;
117 			
118 			//median
119 			values.sort();
120 			int idx = values.size() / 2;
121 			if (values.size() % 2 == 0) {
122 				median[i] = (values.get(idx) + values.get(idx - 1)) / 2.0;
123 			} else {
124 				median[i] = values.get(idx);
125 			}
126 
127 			//mode
128 			HashMax hm = new HashMax();
129 			freqs.forEachEntry(hm);
130 			mode[i] = hm.idx / 255.0;
131 			
132 			//range
133 			range[i] = values.get(values.size() - 1) - values.get(0);
134 			
135 			//variance
136 			variance[i] = 0;
137 			for (int j=0; j<values.size(); j++) {
138 				variance[i] += (values.get(j) - mean[i]) * (values.get(j) - mean[i]);
139 			}
140 			variance[i] = sqrt(variance[i] / values.size());
141 		}
142 	}
143 	
144 	@Override
145 	public String toString() {
146 		String desc = "";
147 		for (int i=0; i<ndims; i++) desc += String.format("%2.2f, ", mean[i]);
148 		for (int i=0; i<ndims; i++) desc += String.format("%2.2f, ", mode[i]);
149 		for (int i=0; i<ndims; i++) desc += String.format("%2.2f, ", median[i]);
150 		for (int i=0; i<ndims; i++) desc += String.format("%2.2f, ", range[i]);
151 		for (int i=0; i<ndims; i++) desc += String.format("%2.2f, ", variance[i]);
152 		return desc.substring(0, desc.length()-2);
153 	}
154 }
155 
156 class HashMax implements TIntIntProcedure {
157 	public int max = 0;
158 	public int idx = -1;
159 	
160 	@Override
161 	public boolean execute(int key, int value) {
162 		if (value > max) {
163 			max = value;
164 			idx = key;
165 		}
166 		return true;
167 	}
168 }