-
Notifications
You must be signed in to change notification settings - Fork 99
/
DistanceTransform.java
278 lines (248 loc) · 9.24 KB
/
DistanceTransform.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Catalano Imaging Library
// The Catalano Framework
//
// Copyright © Diego Catalano, 2012-2016
// diego.catalano at live.com
//
// Code adapted from ImageJ, thanks to Wayne Rasband.
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
package Catalano.Imaging.Filters;
import Catalano.Core.IntPoint;
import Catalano.Imaging.FastBitmap;
/**
* Distance Transform.
*
* <p><li>Supported types: Grayscale.
* <br><li>Coordinate System: Matrix.
*
* @author Diego Catalano
*/
public class DistanceTransform {
/**
* Distance.
*/
public static enum Distance {
/**
* Chessboard.
*/
Chessboard,
/**
* Euclidean.
*/
Euclidean,
/**
* Manhattan.
*/
Manhattan,
/**
* Squared Euclidean.
*/
SquaredEuclidean};
private float[][] image;
private float max = 0;
private IntPoint ued;
private Distance distance = Distance.Euclidean;
/**
* Get Maximum distance from transform.
* @return Maximum distance.
*/
public float getMaximumDistance() {
return max;
}
/**
* Get the Ultimate eroded point.
* @return UED.
*/
public IntPoint getUltimateErodedPoint() {
return ued;
}
/**
* Initialize a new instance of the DistanceTransform class.
* Default distance: Euclidean.
*/
public DistanceTransform() {}
/**
* Initialize a new instance of the DistanceTransform class.
* @param distance Distance.
*/
public DistanceTransform(Distance distance){
this.distance = distance;
}
/**
* Compute Distance Transform.
* @param fastBitmap Image to be processed.
* @return Distance map.
*/
public float[][] Compute(FastBitmap fastBitmap){
if (fastBitmap.isGrayscale()){
int width = fastBitmap.getWidth();
int height = fastBitmap.getHeight();
byte[] bPixels = fastBitmap.getGrayData();
float[] fPixels = new float[bPixels.length];
for (int i=0; i<width*height; i++)
if (bPixels[i]!=0)
fPixels[i] = Float.MAX_VALUE;
int[][] pointBufs = new int[2][width];
// pass 1 & 2: increasing y
for (int x=0; x<width; x++) {
pointBufs[0][x] = -1;
pointBufs[1][x] = -1;
}
for (int y=0; y<height; y++)
edmLine(bPixels, fPixels, pointBufs, width, y*width, y);
//pass 3 & 4: decreasing y
for (int x=0; x<width; x++) {
pointBufs[0][x] = -1;
pointBufs[1][x] = -1;
}
for (int y=height-1; y>=0; y--)
edmLine(bPixels, fPixels, pointBufs, width, y*width, y);
image = new float[height][width];
int p = 0;
if(distance == Distance.Euclidean){
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fPixels[p] < 0f)
image[i][j] = 0;
else
image[i][j] = (float)Math.sqrt(fPixels[p]);
if(image[i][j] > max){
max = image[i][j];
ued = new IntPoint(i, j);
}
p++;
}
}
}
else{
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(fPixels[p] < 0f)
image[i][j] = 0;
else
image[i][j] = fPixels[p];
if(image[i][j] > max){
max = image[i][j];
ued = new IntPoint(i, j);
}
p++;
}
}
}
return image;
}
else{
throw new IllegalArgumentException("Distance Transform only works in grayscale images.");
}
}
// Handle a line; two passes: left-to-right and right-to-left
private void edmLine(byte[] bPixels, float[] fPixels, int[][] pointBufs, int width, int offset, int y) {
int[] points = pointBufs[0]; // the buffer for the left-to-right pass
int pPrev = -1;
int pDiag = -1; // point at (-/+1, -/+1) to current one (-1,-1 in the first pass)
int pNextDiag;
int distSqr = Integer.MAX_VALUE; // this value is used only if edges are not background
for (int x=0; x<width; x++, offset++) {
pNextDiag = points[x];
if (bPixels[offset] == 0) {
points[x] = x | y<<16; // remember coordinates as a candidate for nearest background point
} else { // foreground pixel:
float dist2 = minDist2(points, pPrev, pDiag, x, y, distSqr, distance);
if (fPixels[offset] > dist2) fPixels[offset] = dist2;
}
pPrev = points[x];
pDiag = pNextDiag;
}
offset--; //now points to the last pixel in the line
points = pointBufs[1]; // the buffer for the right-to-left pass. Low short contains x, high short y
pPrev = -1;
pDiag = -1;
for (int x=width-1; x>=0; x--, offset--) {
pNextDiag = points[x];
if (bPixels[offset] == 0) {
points[x] = x | y<<16; // remember coordinates as a candidate for nearest background point
} else { // foreground pixel:
float dist2 = minDist2(points, pPrev, pDiag, x, y, distSqr, distance);
if (fPixels[offset] > dist2) fPixels[offset] = dist2;
}
pPrev = points[x];
pDiag = pNextDiag;
}
}
private float minDist2 (int[] points, int pPrev, int pDiag, int x, int y, int distSqr, Distance distance) {
int p0 = points[x]; // the nearest background point for the same x in the previous line
int nearestPoint = p0;
if (p0 != -1) {
int x0 = p0& 0xffff; int y0 = (p0>>16)&0xffff;
int dist1Sqr = calcDistance(x, y, x0, y0, distance);
if (dist1Sqr < distSqr)
distSqr = dist1Sqr;
}
if (pDiag!=p0 && pDiag!=-1) {
int x1 = pDiag&0xffff; int y1 = (pDiag>>16)&0xffff;
int dist1Sqr = calcDistance(x, y, x1, y1, distance);
if (dist1Sqr < distSqr) {
nearestPoint = pDiag;
distSqr = dist1Sqr;
}
}
if (pPrev!=pDiag && pPrev!=-1) {
int x1 = pPrev& 0xffff; int y1 = (pPrev>>16)&0xffff;
int dist1Sqr = calcDistance(x, y, x1, y1, distance);
if (dist1Sqr < distSqr) {
nearestPoint = pPrev;
distSqr = dist1Sqr;
}
}
points[x] = nearestPoint;
return (float)distSqr;
}
private int calcDistance(int x, int y, int x0, int y0, Distance distance){
int v = 0;
switch(distance){
case Euclidean:
v = (x-x0)*(x-x0)+(y-y0)*(y-y0);
break;
case Manhattan:
v = Math.abs(x-x0) + Math.abs(y-y0);
break;
case Chessboard:
v = Math.max(Math.abs(x-x0), Math.abs(y-y0));
break;
case SquaredEuclidean:
v = (x-x0)*(x-x0)+(y-y0)*(y-y0);
break;
}
return v;
}
/**
* Convert Distance map to FastBitmap.
* @return FastBitmap.
*/
public FastBitmap toFastBitmap(){
int width = image[0].length;
int height = image.length;
FastBitmap fb = new FastBitmap(width, height, FastBitmap.ColorSpace.Grayscale);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
fb.setGray(i, j, (int)Catalano.Math.Tools.Scale(0, max, 0, 255, image[i][j]));
}
}
return fb;
}
}