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  
36  
37  /**
38   * An array of multidimensional histograms calculated from image pixels
39   * (assumes image is in 0-1 range)
40   * 
41   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
42   *
43   */
44  public class BlockHistogramModel extends AbstractPixelStatisticsModel implements FeatureVectorProvider<MultidimensionalHistogram> {
45  	private static final long serialVersionUID = 1L;
46  	
47  	/**
48  	 * The histogram data 
49  	 */
50  	public MultidimensionalHistogram [][] histograms;
51  	
52  	int blocks_x;
53  	int blocks_y;
54  	int [] dims;
55  	
56  	/**
57  	 * Construct with the given parameters
58  	 * @param blocks_x the number of blocks in the x-direction
59  	 * @param blocks_y the number of blocks in the y-direction
60  	 * @param nbins the number of bins in each dimension for the histograms
61  	 */
62  	public BlockHistogramModel(int blocks_x, int blocks_y, int... nbins) {
63  		super(nbins.length);
64  		this.dims = nbins;
65  		this.blocks_x = blocks_x;
66  		this.blocks_y = blocks_y;
67  		this.histograms = new MultidimensionalHistogram[blocks_y][blocks_x];
68  		
69  		for (int y=0; y<blocks_y; y++)
70  			for (int x=0; x<blocks_x; x++)
71  				histograms[y][x] = new MultidimensionalHistogram(dims);
72  	}
73  
74  	/**
75  	 * @return a flattened version of the histogram array formed by 
76  	 * concatenating the histograms of all the blocks in scan order.
77  	 */
78  	public MultidimensionalHistogram toSingleHistogram() {
79  		int [] newdims = new int[dims.length + 2];
80  		
81  		for (int i=0; i<dims.length; i++)
82  			newdims[i] = dims[i];
83  		newdims[dims.length] = blocks_x;
84  		newdims[dims.length+1] = blocks_y;
85  		
86  		MultidimensionalHistogram h = new MultidimensionalHistogram(newdims);
87  		
88  		for (int y=0; y<blocks_y; y++) {
89  			for (int x=0; x<blocks_x; x++) {
90  				int blkid = x + y*blocks_x;
91  				for (int i=0; i<histograms[y][x].values.length; i++) {
92  					h.values[i + blkid*histograms[y][x].values.length] = histograms[y][x].values[i];
93  				}
94  			}
95  		}
96  		
97  		return h;
98  	}
99  	
100 	protected void reset(MultidimensionalHistogram histogram) {
101 		for (int i=0; i<histogram.values.length; i++)
102 			histogram.values[i] = 0;
103 	}
104 	
105 	@Override
106 	public void estimateModel(MBFImage... images) {
107 		//reset histograms
108 		for (int y=0; y<blocks_y; y++)
109 			for (int x=0; x<blocks_x; x++)
110 				reset(histograms[y][x]);
111 		
112 		//accumulate
113 		for (MBFImage img : images) {
114 			for (int y=0; y<blocks_y; y++) {
115 				for (int x=0; x<blocks_x; x++) {
116 					accum(img, x, y);
117 				}
118 			}
119 		}
120 		
121 		//normalise
122 		for (int y=0; y<blocks_y; y++)
123 			for (int x=0; x<blocks_x; x++)
124 				histograms[y][x].normalise();
125 	}
126 	
127 	protected void accum(MBFImage im, int bx, int by) {
128 		assert (im.numBands() == ndims);
129 
130 		MultidimensionalHistogram histogram = histograms[by][bx];
131 		int height = im.getHeight();
132 		int width = im.getWidth();
133 		
134 		int cols_per_block = width / blocks_x;		
135 		int startx = bx*cols_per_block;
136 		int stopx = (1+bx)*cols_per_block;
137 
138 		int rows_per_block = height / blocks_y;
139 		int starty = by*rows_per_block;
140 		int stopy = (1+by)*rows_per_block;
141 		
142 		if (stopx >= width) stopx = width;
143 		if (stopy >= height) stopy = height;
144 		
145 		for (int y=starty; y<stopy; y++) {
146 			for (int x=startx; x<stopx; x++) {
147 				int [] bins = new int[ndims];
148 				
149 				for (int i=0; i<ndims; i++) {
150 					bins[i] = (int)(im.getBand(i).pixels[y][x] * (histogram.nbins[i]));
151 					if (bins[i] >= histogram.nbins[i]) bins[i] = histogram.nbins[i] - 1;
152 				}
153 				
154 				int bin = 0;
155 				for (int i=0; i<ndims; i++) {
156 					int f = 1;
157 					for (int j=0; j<i; j++)
158 						f *= histogram.nbins[j];
159 					
160 					bin += f * bins[i];
161 				}
162 				
163 				histogram.values[bin]++;
164 			}
165 		}
166 	}
167 	
168 	@Override
169 	public String toString() {
170 		String s = "LocalHistogram[\n";
171 		
172 		for (int y=0; y<blocks_y; y++)
173 			for (int x=0; x<blocks_x; x++)
174 				s += "\t(" + x + ", " + y + ") = " + histograms[y][x].toString() + "\n";
175 				
176 		s += "]\n";
177 		return s;
178 	}
179 	
180 	@Override
181 	public BlockHistogramModel clone() {
182 		BlockHistogramModel model = new BlockHistogramModel(blocks_x, blocks_x, dims);
183 		model.histograms = new MultidimensionalHistogram[blocks_y][blocks_x];
184 		
185 		for (int y=0; y<blocks_y; y++)
186 			for (int x=0; x<blocks_x; x++)
187 				model.histograms[y][x] = histograms[y][x].clone();
188 		
189 		return model;
190 	}
191 
192 	@Override
193 	public MultidimensionalHistogram getFeatureVector() {
194 		return toSingleHistogram();
195 	}
196 }