- Enclosing class:
- ImgProc
public class ImgProc.MIPcalcStruc
extends java.lang.Object
public static byte[][][] distTransform (boolean[][][] origBinImg ) {
return distTransform(ArraysI.toByte(origBinImg));
} // 3D distance transform
public static byte[][][] distTransform( byte[][][] origBinImg ) {
int depth = origBinImg.length;
int width = origBinImg[0].length;
int height = origBinImg[0][0].length;
byte[][][] distmap = new byte[depth][width][height];
ArraysI.copy(origBinImg, distmap);
//WO for now invert the image
ArraysI.scale(distmap, 255,0);
// final int a = 3, b = 4, c = 3, d = 4, e = 5; //alternate weights
final int a = 3, b = 4, c = 5, d = 3, e = 7;
final int[] wf = new int[] { e, d, e, d, c, d, e, d, e, b, a, b, a,
255, 255, 255, 255, 255, };
final int[] wb = new int[] { 255, 255, 255, 255, 255, a, b, a, b, e, d,
e, d, c, d, e, d, e, };
int[] slask = new int[2 * 3 * 3];
// Border pixels are ignored to simplify the convolutions below
for (int z = 0; z < depth; z++)
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
if (z == 0 || x == 0 || y == 0 || z == depth - 1
|| y == height - 1 || x == width - 1)
distmap[z][x][y] = (byte) 0x0;
// Forward iteration
for (int z = 1; z < depth-1; z++) {
IJ.showProgress(z, 2 * depth - 2);
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
for (int k = -1; k < 1; k++)
for (int j = -1; j < 2; j++)
for (int i = -1; i < 2; i++) {
int slaskindex = (i+1)+ (j+1)*3 + (k+1)*3*3;
int pixel = distmap[z+k+1][x+i][y+j]&0xff;
// 0xff & ((byte[]) imRef.getStack().getPixels(z+k+1))[(x+i)+ (y+j)* width];
slask[slaskindex] = pixel + wf[slaskindex];
}
int minval = slask[0]; // the lowest value so far
for (int i = 1; i < slask.length; i++)
if ((slask[i]) < minval)
minval = (slask[i]);
// int pixel = 0xff & ((byte[]) imRef.getStack().getPixels(z+1))[(x)+ (y)* width];
int pixel = distmap[z+1][x][y]&0xff;
if (pixel > minval)
// ((byte[]) imRef.getStack().getPixels(z+1))[x + y*width] = (byte) (minval & 0xff);
distmap[z+1][x][y] = (byte) (minval);
} // end height(y)
} // end width(x)
} // end depth(z)
// Backward iteration
for (int z = depth-3; z > 0; z--) { // WO 3/08 changed from depth-2 to depth-3
IJ.showProgress(2 * depth - z, 2 * depth - 2);
for (int x = width-2; x > 0; x--) {
for (int y = height-2; y > 0; y--) {
for (int k = 0; k < 2; k++)
for (int j = -1; j < 2; j++)
for (int i = -1; i < 2; i++) {
int slaskindex = (i+1)+ (j+1)*3+ (k)*3*3;
int pixel = distmap[z+k+1][x+i][y+j]&0xff;
// = 0xff & ((byte[]) imRef.getStack().getPixels(z+k+1))[(x+i)+ (y+j)*width];
slask[slaskindex] = pixel + wb[slaskindex];
}
int minval = slask[0]; // the lowest value so far
for (int i = 1; i < slask.length; i++)
if ((slask[i]) < minval)
minval = (slask[i]);
int pixel = distmap[z+1][x][y]&0xff;
// = 0xff & ((byte[]) imRef.getStack().getPixels(z+1))[(x)+ (y)*width];
if (pixel > minval)
// ((byte[]) imRef.getStack().getPixels(z+1))[x + y*width] = (byte) (minval &0xff);
distmap[z+1][x][y] = (byte) (minval);
} // end height(y)
} // end width(x)
} // end depth(z)
return distmap;
} // 3D distance transform