-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add label/filter/ChamferLabelErosion2DShort.java
- Loading branch information
Showing
5 changed files
with
500 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/inra/ijpb/label/filter/ChamferLabelErosion2DShort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* | ||
*/ | ||
package inra.ijpb.label.filter; | ||
|
||
import ij.process.ImageProcessor; | ||
import inra.ijpb.algo.AlgoStub; | ||
import inra.ijpb.binary.distmap.ChamferMask2D; | ||
import inra.ijpb.morphology.Strel; | ||
import inra.ijpb.morphology.strel.ChamferStrel; | ||
|
||
/** | ||
* Implementation of morphological erosion for 2D label images / label maps. | ||
* | ||
* Can be applied to label maps encoded with 8 or 16 bits integers, or 32 bit | ||
* floats. | ||
* | ||
* | ||
* @author dlegland | ||
* | ||
*/ | ||
public class ChamferLabelErosion2DShort extends AlgoStub | ||
{ | ||
/** | ||
* The chamfer mask used to propagate distances. | ||
*/ | ||
ChamferMask2D mask; | ||
|
||
/** | ||
* The radius of dilation of labels. In practice, the distance is propagated | ||
* up to radius + 0.5. | ||
*/ | ||
double radius; | ||
|
||
/** | ||
* For erosion, we can work only with the structuring element. | ||
*/ | ||
Strel strel; | ||
|
||
public ChamferLabelErosion2DShort(ChamferMask2D mask, double radius) | ||
{ | ||
this.mask = mask; | ||
this.radius = radius; | ||
|
||
this.strel = new ChamferStrel(mask, radius); | ||
} | ||
|
||
public ImageProcessor process(ImageProcessor image) | ||
{ | ||
// retrieve image size | ||
int sizeX = image.getWidth(); | ||
int sizeY = image.getHeight(); | ||
|
||
// allocate memory for output | ||
ImageProcessor res = image.createProcessor(sizeX, sizeY); | ||
|
||
// pre-compute strel shifts | ||
int[][] shifts = strel.getShifts(); | ||
|
||
// iterate over pixels within output image | ||
for (int y = 0; y < sizeY; y++) | ||
{ | ||
for (int x = 0; x < sizeX; x++) | ||
{ | ||
int label = (int) image.getf(x, y); | ||
|
||
// no need to process background pixels | ||
if (label == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
// iterate over neighbors defined by strel | ||
for (int[] shift : shifts) | ||
{ | ||
// position of neighbor | ||
int x2 = x + shift[0]; | ||
int y2 = y + shift[1]; | ||
|
||
// do not process pixels outside of image bounds | ||
if (x2 < 0 || x2 >= sizeX) continue; | ||
if (y2 < 0 || y2 >= sizeY) continue; | ||
|
||
// check if neighbor is background | ||
int label2 = (int) image.getf(x2, y2); | ||
if (label2 == 0) | ||
{ | ||
label = 0; | ||
break; | ||
} | ||
} | ||
|
||
res.setf(x, y, label); | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/inra/ijpb/label/filter/ChamferLabelErosion3DShort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* | ||
*/ | ||
package inra.ijpb.label.filter; | ||
|
||
import ij.ImageStack; | ||
import inra.ijpb.algo.AlgoStub; | ||
import inra.ijpb.binary.distmap.ChamferMask3D; | ||
import inra.ijpb.morphology.Strel3D; | ||
import inra.ijpb.morphology.strel.ChamferStrel3D; | ||
|
||
/** | ||
* Implementation of morphological erosion for 3D label images / label maps. | ||
* | ||
* Can be applied to label maps encoded with 8 or 16 bits integers, or 32 bit | ||
* floats. | ||
* | ||
* | ||
* @author dlegland | ||
* | ||
*/ | ||
public class ChamferLabelErosion3DShort extends AlgoStub | ||
{ | ||
/** | ||
* The chamfer mask used to propagate distances. | ||
*/ | ||
ChamferMask3D mask; | ||
|
||
/** | ||
* The radius of dilation of labels. In practice, the distance is propagated | ||
* up to radius + 0.5. | ||
*/ | ||
double radius; | ||
|
||
/** | ||
* For erosion, we can work only with the structuring element. | ||
*/ | ||
Strel3D strel; | ||
|
||
public ChamferLabelErosion3DShort(ChamferMask3D mask, double radius) | ||
{ | ||
this.mask = mask; | ||
this.radius = radius; | ||
|
||
this.strel = new ChamferStrel3D(mask, radius); | ||
} | ||
|
||
public ImageStack process(ImageStack image) | ||
{ | ||
// retrieve image size | ||
int sizeX = image.getWidth(); | ||
int sizeY = image.getHeight(); | ||
int sizeZ = image.getSize(); | ||
|
||
// allocate memory for output | ||
ImageStack res = ImageStack.create(sizeX, sizeY, sizeZ, image.getBitDepth()); | ||
|
||
// pre-compute strel shifts | ||
int[][] shifts = strel.getShifts3D(); | ||
|
||
// iterate over pixels within output image | ||
for (int z = 0; z < sizeZ; z++) | ||
{ | ||
for (int y = 0; y < sizeY; y++) | ||
{ | ||
for (int x = 0; x < sizeX; x++) | ||
{ | ||
int label = (int) image.getVoxel(x, y, z); | ||
|
||
// no need to process background pixels | ||
if (label == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
// iterate over neighbors defined by strel | ||
for (int[] shift : shifts) | ||
{ | ||
// position of neighbor | ||
int x2 = x + shift[0]; | ||
int y2 = y + shift[1]; | ||
int z2 = z + shift[2]; | ||
|
||
// do not process pixels outside of image bounds | ||
if (x2 < 0 || x2 >= sizeX) continue; | ||
if (y2 < 0 || y2 >= sizeY) continue; | ||
if (z2 < 0 || z2 >= sizeZ) continue; | ||
|
||
// check if neighbor is background | ||
int label2 = (int) image.getVoxel(x2, y2, z2); | ||
if (label2 == 0) | ||
{ | ||
label = 0; | ||
break; | ||
} | ||
} | ||
|
||
res.setVoxel(x, y, z, label); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.