How to Remove EBImage objects that have holes?
1
0
Entering edit mode
atbest09 • 0
@atbest09-15478
Last seen 2.9 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 • 431 views
ADD COMMENT
1
Entering edit mode
@wolfgang-huber-3550
Last seen 4 days ago
EMBL European Molecular Biology Laborat…

There are probably better ways, but how about this:

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))

 

ADD COMMENT
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))
ADD REPLY

Login before adding your answer.

Traffic: 580 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6