Exporting Cell Detections as Binary Masks

Hi all,

Is it possible to export all my cell detections as a binary mask image? So as in 1 (white) for the cell and 0 (black) everything else? Is there a script available for this use?

Are you looking for a bounding box image per cell, box of a set size, tiles of tissue, whole slide…?
Also, you tagged segmentation but not any particular program.

1 Like

but with what Software/programming language?

1 Like

Sorry I forgot to mention that I’m working in QuPath, with .groovy scripts. I wanted to extract the mask directly from QuPath.
I’ve actually happened to come across another thread that helped solve my problem. I needed to make a few changes to get the result I wanted.

https://groups.google.com/forum/#!topic/qupath-users/EOdNLyP7ixc

  • Repalce getAnnotationObjects() with getDetectionObjects()
  • Change downsample from 10.0 to 1.0

image

Yep, that was one of the ones I was thinking of, glad you are all set.

Hi, I tried to run this script after generating some detections but got an error that said:

ERROR: It looks like you have tried to import a class ‘qupath.lib.roi.PathROIToolsAwt’ that doesn’t exist!

I’m running Qupath version 0.2.1 Has something changed? thanks for your help, John

import static qupath.lib.roi.PathROIToolsAwt.getShape;
import java.awt.image.BufferedImage
import java.awt.Color
import javax.imageio.ImageIO
//
// Get java.awt.Shape objects for each annotation
def shapes = getDetectionObjects().collect({getShape(it.getROI())})
//
// Create a grayscale image, here it’s 10% of the full image size
double downsample = 1.0
def server = getCurrentImageData().getServer()
int w = (server.getWidth() / downsample) as int
int h = (server.getHeight() / downsample) as int
def img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY)
//
// Paint the shapes (this is just ‘standard’ Java - you might want to modify)
def g2d = img.createGraphics()
g2d.scale(1.0/downsample, 1.0/downsample)
g2d.setColor(Color.WHITE)
for (shape in shapes)
g2d.fill(shape)
g2d.dispose()
//
// Save the result
def outputFile = getQuPath().getDialogHelper().promptToSaveFile(“Save binary image”, null, null, “PNG”, “.png”)
ImageIO.write(img, ‘PNG’, outputFile)

Yes, anything from the old QuPath forum is going to be from 0.1.2. A LOT has changed in general, and probably several things in particular for that script. You may want to look into more recent mask script postings.
The official docs: https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_annotations.html#binary-labeled-images
Exporting Detection Labels from QuPath
Exporting detections within selected annotation

Most v0.1.2 won’t work in v0.2, and usually the replacement is much simpler.

You can use the methods to export images here:


but when you see options being added for ‘LabeledServerBuilder’ you would add in useDetections() – or useCells() – among them.

useUniqueLabels() is another useful option:

Thanks! I’ll give this a try. -John