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.pixel.statistics;
031
032import gnu.trove.list.array.TFloatArrayList;
033import gnu.trove.map.hash.TIntIntHashMap;
034import gnu.trove.procedure.TIntIntProcedure;
035
036import org.openimaj.image.FImage;
037import org.openimaj.image.MBFImage;
038
039import static java.lang.Math.sqrt;
040
041/**
042 * A model of the all the values of the pixels in an image or set of images,
043 * using basic descriptive statistics (mean, mode, median, range, variance).
044 * 
045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
046 */
047public class BasicDescriptiveStatisticsModel extends AbstractPixelStatisticsModel {
048        private static final long serialVersionUID = 1L;
049
050        /**
051         * The mean pixel value
052         */
053        public double [] mean;
054        
055        /**
056         * The mode of pixel values
057         */
058        public double [] mode;
059        
060        /**
061         * The median of pixel values
062         */
063        public double [] median;
064        
065        /**
066         * The range of pixel values
067         */
068        public double [] range;
069        
070        /**
071         * The variance of pixel values
072         */
073        public double [] variance;
074        
075        /**
076         * Construct a BasicDescriptiveStatisticsModel with the given
077         * number of dimensions. The number of dimensions should normally 
078         * be equal to the number of bands in the images from which the model
079         * is to be estimated. 
080         *  
081         * @param ndims number of dimensions
082         */
083        public BasicDescriptiveStatisticsModel(int ndims) {
084                super(ndims);
085                
086                mean = new double[ndims];
087                mode = new double[ndims];
088                median = new double[ndims];
089                range = new double[ndims];
090                variance = new double[ndims];
091        }
092
093        @Override
094        public void estimateModel(MBFImage... images) { 
095                for (int i=0; i<ndims; i++) {
096                        mean[i] = 0;
097                        TFloatArrayList values = new TFloatArrayList();
098                        TIntIntHashMap freqs = new TIntIntHashMap();
099                        
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
156class 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}