Skip to content

Commit 9985665

Browse files
committed
fix computation of 3D distance maps on label images
1 parent 95e7ac7 commit 9985665

4 files changed

Lines changed: 64 additions & 17 deletions

File tree

‎src/main/java/inra/ijpb/label/distmap/ChamferDistanceTransform3DFloat.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private void forwardScan(Image3D labels, Image3D distMap)
226226
if (((int) labels.getValue(x2, y2, z2)) != label)
227227
{
228228
// Update with distance to nearest different label
229-
newDist = offset.weight;
229+
newDist = Math.min(newDist, offset.weight);
230230
}
231231
else
232232
{
@@ -295,7 +295,7 @@ private void backwardScan(Image3D labels, Image3D distMap)
295295
if (((int) labels.getValue(x2, y2, z2)) != label)
296296
{
297297
// Update with distance to nearest different label
298-
newDist = offset.weight;
298+
newDist = Math.min(newDist, offset.weight);
299299
}
300300
else
301301
{

‎src/main/java/inra/ijpb/label/distmap/ChamferDistanceTransform3DShort.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void forwardScan(Image3D labels, Image3D distMap)
224224
if (((int) labels.getValue(x2, y2, z2)) != label)
225225
{
226226
// Update with distance to nearest different label
227-
newDist = offset.weight;
227+
newDist = Math.min(newDist, offset.weight);
228228
}
229229
else
230230
{
@@ -293,7 +293,7 @@ private void backwardScan(Image3D labels, Image3D distMap)
293293
if (((int) labels.getValue(x2, y2, z2)) != label)
294294
{
295295
// Update with distance to nearest different label
296-
newDist = offset.weight;
296+
newDist = Math.min(newDist, offset.weight);
297297
}
298298
else
299299
{

‎src/test/java/inra/ijpb/label/distmap/ChamferDistanceTransform3DFloatTest.java‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ChamferDistanceTransform3DFloatTest
4242
* Test method for {@link inra.ijpb.label.distmap.ChamferDistanceTransform3DFloat#distanceMap(ij.ImageStack)}.
4343
*/
4444
@Test
45-
public void testDistanceMap()
45+
public void test_distanceMap_Borgefors()
4646
{
4747
// create 3D image containing a cube
4848
ImageStack image = ImageStack.create(20, 20, 20, 8);
@@ -62,13 +62,36 @@ public void testDistanceMap()
6262
ImageStack result = algo.distanceMap(image);
6363

6464
assertEquals(32, result.getBitDepth());
65-
// System.out.println("result:");
66-
// for (int x = 0; x < 100; x++)
67-
// {
68-
// System.out.print(((int)result.getVoxel(x, 50, 50)) + " ");
69-
// }
7065
double middle = result.getVoxel(10, 10, 10);
71-
assertEquals(9, middle, .1);
66+
assertEquals(9, middle, 0.1);
67+
}
68+
69+
/**
70+
* Test method for {@link inra.ijpb.label.distmap.ChamferDistanceTransform3DFloat#distanceMap(ij.ImageStack)}.
71+
*/
72+
@Test
73+
public void test_distanceMap_Svensson()
74+
{
75+
// create 3D image containing a cube
76+
ImageStack image = ImageStack.create(20, 20, 20, 8);
77+
for (int z = 2; z < 19; z++)
78+
{
79+
for (int y = 2; y < 19; y++)
80+
{
81+
for (int x = 2; x < 19; x++)
82+
{
83+
image.setVoxel(x, y, z, 255);
84+
}
85+
}
86+
}
87+
88+
ChamferMask3D mask = ChamferMask3D.SVENSSON_3_4_5_7;
89+
DistanceTransform3D algo = new ChamferDistanceTransform3DFloat(mask, true);
90+
ImageStack result = algo.distanceMap(image);
91+
92+
assertEquals(32, result.getBitDepth());
93+
double middle = result.getVoxel(10, 10, 10);
94+
assertEquals(9, middle, 0.1);
7295
}
7396

7497
@Test

‎src/test/java/inra/ijpb/label/distmap/ChamferDistanceTransform3DShortTest.java‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ChamferDistanceTransform3DShortTest
4242
* Test method for {@link inra.ijpb.label.distmap.ChamferDistanceTransform3DShort#distanceMap(ij.ImageStack)}.
4343
*/
4444
@Test
45-
public void testDistanceMap()
45+
public void test_distanceMap_Borgefors()
4646
{
4747
// create 3D image containing a cube
4848
ImageStack image = ImageStack.create(20, 20, 20, 8);
@@ -63,11 +63,35 @@ public void testDistanceMap()
6363
ImageStack result = algo.distanceMap(image);
6464
assertEquals(16, result.getBitDepth());
6565

66-
// System.out.println("result:");
67-
// for (int x = 0; x < 100; x++)
68-
// {
69-
// System.out.print(((int)result.getVoxel(x, 50, 50)) + " ");
70-
// }
66+
double middle = result.getVoxel(10, 10, 10);
67+
assertEquals(9, middle, .1);
68+
}
69+
70+
/**
71+
* Test method for {@link inra.ijpb.label.distmap.ChamferDistanceTransform3DShort#distanceMap(ij.ImageStack)}.
72+
*/
73+
@Test
74+
public void test_distanceMap_Svensson()
75+
{
76+
// create 3D image containing a cube
77+
ImageStack image = ImageStack.create(20, 20, 20, 8);
78+
for (int z = 2; z < 19; z++)
79+
{
80+
for (int y = 2; y < 19; y++)
81+
{
82+
for (int x = 2; x < 19; x++)
83+
{
84+
image.setVoxel(x, y, z, 255);
85+
}
86+
}
87+
}
88+
89+
ChamferMask3D mask = ChamferMask3D.SVENSSON_3_4_5_7;
90+
DistanceTransform3D algo = new ChamferDistanceTransform3DShort(mask, true);
91+
92+
ImageStack result = algo.distanceMap(image);
93+
assertEquals(16, result.getBitDepth());
94+
7195
double middle = result.getVoxel(10, 10, 10);
7296
assertEquals(9, middle, .1);
7397
}

0 commit comments

Comments
 (0)