Question: How to Remove EBImage objects that have holes?
0
gravatar for atbest09
14 months ago by
atbest090
atbest090 wrote:

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 • 258 views
ADD COMMENTlink modified 14 months ago by Wolfgang Huber13k • written 14 months ago by atbest090
Answer: How to Remove EBImage objects that have holes?
1
gravatar for Wolfgang Huber
14 months ago by
EMBL European Molecular Biology Laboratory
Wolfgang Huber13k wrote:

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 COMMENTlink modified 14 months ago • written 14 months ago by Wolfgang Huber13k
2

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 REPLYlink modified 14 months ago • written 14 months ago by Andrzej Oleś720
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 334 users visited in the last hour