1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.openimaj.image.analysis.algorithm.histogram;
31
32 import org.openimaj.image.FImage;
33 import org.openimaj.math.statistics.distribution.Histogram;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class InterpolatedBinnedWindowedExtractor extends BinnedWindowedExtractor {
57
58
59
60
61 float[][] weights;
62
63
64
65
66 boolean wrap = false;
67
68
69
70
71
72
73
74
75 public InterpolatedBinnedWindowedExtractor(int nbins) {
76 super(nbins);
77 }
78
79
80
81
82
83
84
85
86
87
88 public InterpolatedBinnedWindowedExtractor(int nbins, boolean wrap) {
89 super(nbins);
90 this.wrap = true;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public InterpolatedBinnedWindowedExtractor(int nbins, float min, float max) {
105 super(nbins, min, max);
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public InterpolatedBinnedWindowedExtractor(int nbins, float min, float max, boolean wrap) {
122 super(nbins, min, max);
123 this.wrap = wrap;
124 }
125
126
127
128
129 @Override
130 public void analyseImage(FImage image) {
131 final int height = image.height;
132 final int width = image.width;
133
134 binMap = new int[height][width];
135 weights = new float[height][width];
136
137 if (wrap) {
138 for (int y = 0; y < height; y++) {
139 for (int x = 0; x < width; x++) {
140 final float val = ((image.pixels[y][x] - min) / (max - min)) * nbins;
141 final int bin = (int) Math.floor(val);
142 final float delta = val - bin;
143
144 final int lbin = bin % nbins;
145 final float lweight = 1f - delta;
146
147 binMap[y][x] = lbin;
148 weights[y][x] = lweight;
149 }
150 }
151 } else {
152 for (int y = 0; y < height; y++) {
153 for (int x = 0; x < width; x++) {
154 final float val = ((image.pixels[y][x] - min) / (max - min)) * nbins;
155 final int bin = (int) Math.floor(val);
156 final float delta = val - bin;
157
158 int lbin;
159 float lweight;
160 if (delta < 0.5) {
161
162 lbin = bin - 1;
163 lweight = 0.5f + (delta);
164 } else {
165
166 lbin = bin;
167 lweight = 1.5f - (delta);
168 }
169
170 if (lbin < 0) {
171 lbin = 0;
172 lweight = 1;
173 } else if (bin >= nbins) {
174 lbin = nbins - 1;
175 lweight = 1;
176 }
177
178 binMap[y][x] = lbin;
179 weights[y][x] = lweight;
180 }
181 }
182 }
183 }
184
185 @Override
186 public Histogram computeHistogram(int x, int y, int w, int h) {
187 final Histogram hist = new Histogram(nbins);
188
189 final int starty = Math.max(0, y);
190 final int startx = Math.max(0, x);
191 final int stopy = Math.min(binMap.length, y + h);
192 final int stopx = Math.min(binMap[0].length, x + w);
193
194 for (int r = starty; r < stopy; r++) {
195 for (int c = startx; c < stopx; c++) {
196 final int bin = binMap[r][c];
197 hist.values[bin] += weights[r][c];
198
199 if (wrap && bin + 1 == nbins) {
200 hist.values[0] += (1 - weights[r][c]);
201 }
202
203 if (bin + 1 < nbins) {
204 hist.values[bin + 1] += (1 - weights[r][c]);
205 }
206 }
207 }
208
209 return hist;
210 }
211
212 @Override
213 public Histogram computeHistogram(int x, int y, int w, int h, FImage extWeights) {
214 final Histogram hist = new Histogram(nbins);
215
216 final int starty = Math.max(0, y);
217 final int startx = Math.max(0, x);
218 final int stopy = Math.min(binMap.length, y + h);
219 final int stopx = Math.min(binMap[0].length, x + w);
220
221 for (int r = starty; r < stopy; r++) {
222 for (int c = startx; c < stopx; c++) {
223 final int bin = binMap[r][c];
224 hist.values[bin] += (extWeights.pixels[r][c] * weights[r][c]);
225
226 if (wrap && bin + 1 == nbins) {
227 hist.values[0] += (extWeights.pixels[r][c] * (1 - weights[r][c]));
228 }
229
230 if (bin + 1 < nbins) {
231 hist.values[bin + 1] += (extWeights.pixels[r][c] * (1 - weights[r][c]));
232 }
233 }
234 }
235
236 return hist;
237 }
238
239 @Override
240 public Histogram computeHistogram(int x, int y, FImage extWeights, FImage windowWeights)
241 {
242 final Histogram hist = new Histogram(nbins);
243
244 final int starty = Math.max(0, y);
245 final int startx = Math.max(0, x);
246 final int stopy = Math.min(binMap.length, y + windowWeights.height);
247 final int stopx = Math.min(binMap[0].length, x + windowWeights.width);
248
249 final int startwr = y < 0 ? -y : y;
250 final int startwc = x < 0 ? -x : x;
251
252 for (int r = starty, wr = startwr; r < stopy; r++, wr++) {
253 for (int c = startx, wc = startwc; c < stopx; c++, wc++) {
254 final int bin = binMap[r][c];
255 hist.values[bin] += (extWeights.pixels[r][c] * weights[r][c] * windowWeights.pixels[wr][wc]);
256
257 if (wrap && bin + 1 == nbins) {
258 hist.values[0] += (extWeights.pixels[r][c] * (1 - weights[r][c]) * windowWeights.pixels[wr][wc]);
259 }
260
261 if (bin + 1 < nbins) {
262 hist.values[bin + 1] += (extWeights.pixels[r][c] * (1 - weights[r][c]) * windowWeights.pixels[wr][wc]);
263 }
264 }
265 }
266
267 return hist;
268 }
269
270
271
272
273
274
275 public float[][] getWeightsMap() {
276 return weights;
277 }
278 }