How to Remove EBImage objects that have holes?
1
0
Entering edit mode
atbest09 • 0
@atbest09-15478
Last seen 3.7 years ago

Hi all,

Does any one know how to remove objects that have holes using EBImage? For example, I converted the nuclei.tif file into a mask file using the follow code. Now how can I get rid of the nuclei with holes? Thanks.

x = readImage(system.file(file.path("images","nuclei.tif"), package="EBImage"), all = FALSE)
y=thresh(x, w = 15, h = 15, offset = 0.1)
objects = bwlabel(y)
display(objects)

ebimage • 524 views
1
Entering edit mode
@wolfgang-huber-3550
Last seen 4 months ago
EMBL European Molecular Biology Laborat…

library("EBImage")

x = readImage(system.file(file.path("images", "nuclei.tif"), package = "EBImage"), all = FALSE)
y = thresh(x, w = 15, h = 15, offset = 0.1)
yf = fillHull(y)

objects = bwlabel(yf)
owh = unique(objects[ y != fillHull(y) ])
objects[ objects %in% owh ] = 0

display(tile(combine(objects, bwlabel(y)), nx = 2))

2
Entering edit mode

Thank you Wolfgang for providing a solution to the problem. This is probably the way to go. Note that there is a dedicated function rmObjects to remove objects from images which is the preferred approach for performance reasons. It also provides an easy way to re-label objects such that the maximum index corresponds to the number of objects in the image. Consider the following modified code which additionally reuses the intermediate result yf = fillHull(y) when subsetting objects.

library("EBImage")

x = readImage(system.file(file.path("images", "nuclei.tif"), package = "EBImage"), all = FALSE)
y = thresh(x, w = 15, h = 15, offset = 0.1)
yf = fillHull(y)

objects = bwlabel(yf)
owh = unique(objects[ y != yf ])

objects = rmObjects(objects, owh, reenumerate = FALSE)

display(tile(combine(objects, bwlabel(y)), nx = 2))