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.processing.algorithm;
31
32 import java.util.Set;
33
34 import org.openimaj.image.FImage;
35 import org.openimaj.image.analyser.ImageAnalyser;
36 import org.openimaj.image.pixel.Pixel;
37
38
39
40
41
42
43
44
45
46
47
48
49 public class MinMaxAnalyser implements ImageAnalyser<FImage> {
50 private Set<Pixel> support;
51 private int blockWidth = -1;
52 private int blockHeight = -1;
53
54
55
56
57
58 public FImage min;
59
60
61
62
63
64 public FImage max;
65
66
67
68
69
70
71
72
73
74
75 public MinMaxAnalyser(Set<Pixel> support) {
76 this.support = support;
77
78 if (FilterSupport.isBlockSupport(support)) {
79 blockWidth = FilterSupport.getSupportWidth(support);
80 blockHeight = FilterSupport.getSupportHeight(support);
81 }
82 }
83
84 @Override
85 public void analyseImage(FImage image) {
86 min = new FImage(image.width, image.height);
87 max = new FImage(image.width, image.height);
88
89 if (blockHeight >= 1 && blockWidth >= 1) {
90 processBlock(image, blockWidth, blockHeight);
91 } else {
92 for (int y = 0; y < image.height; y++) {
93 for (int x = 0; x < image.width; x++) {
94 float minv = Float.MAX_VALUE;
95 float maxv = -Float.MAX_VALUE;
96
97 for (final Pixel sp : support) {
98 final int xx = x + sp.x;
99 final int yy = y + sp.y;
100
101 if (xx >= 0 && xx < image.width - 1 && yy >= 0 && yy < image.height -
102 1)
103 {
104 minv = Math.min(minv, image.pixels[yy][xx]);
105 maxv = Math.max(maxv, image.pixels[yy][xx]);
106 }
107 }
108
109 min.pixels[y][x] = minv;
110 max.pixels[y][x] = maxv;
111 }
112 }
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127 private void processBlock(FImage image, int width, int height) {
128 final int halfWidth = width / 2;
129 final float buffer[] = new float[image.width + width];
130
131 for (int r = 0; r < image.height; r++) {
132 for (int i = 0; i < halfWidth; i++)
133 buffer[i] = image.pixels[r][0];
134 for (int i = 0; i < image.width; i++)
135 buffer[halfWidth + i] = image.pixels[r][i];
136 for (int i = 0; i < halfWidth; i++)
137 buffer[halfWidth + image.width + i] = image.pixels[r][image.width - 1];
138
139 final int l = buffer.length - width;
140 for (int i = 0; i < l; i++) {
141 float min = Float.MAX_VALUE;
142 float max = -Float.MAX_VALUE;
143
144 for (int j = 0; j < width; j++) {
145 min = Math.min(buffer[i + j], min);
146 max = Math.max(buffer[i + j], max);
147 }
148
149 this.min.pixels[r][i] = min;
150 this.max.pixels[r][i] = max;
151 }
152 }
153
154 final int halfHeight = height / 2;
155
156 final float minbuffer[] = new float[min.height + height];
157 final float maxbuffer[] = new float[max.height + height];
158
159 for (int c = 0; c < min.width; c++) {
160 for (int i = 0; i < halfHeight; i++) {
161 minbuffer[i] = min.pixels[0][c];
162 maxbuffer[i] = max.pixels[0][c];
163 }
164 for (int i = 0; i < min.height; i++) {
165 minbuffer[halfHeight + i] = min.pixels[i][c];
166 maxbuffer[halfHeight + i] = max.pixels[i][c];
167 }
168 for (int i = 0; i < halfHeight; i++) {
169 minbuffer[halfHeight + min.height + i] = min.pixels[min.height - 1][c];
170 maxbuffer[halfHeight + min.height + i] = max.pixels[max.height - 1][c];
171 }
172
173 final int l = minbuffer.length - height;
174 for (int i = 0; i < l; i++) {
175 float minv = Float.MAX_VALUE;
176 float maxv = -Float.MAX_VALUE;
177
178 for (int j = 0; j < height; j++) {
179 minv = Math.min(minbuffer[i + j], minv);
180 maxv = Math.max(maxbuffer[i + j], maxv);
181 }
182
183 minbuffer[i] = minv;
184 maxbuffer[i] = maxv;
185 }
186
187 for (int r = 0; r < min.height; r++) {
188 min.pixels[r][c] = minbuffer[r];
189 max.pixels[r][c] = maxbuffer[r];
190 }
191 }
192 }
193 }