How to extract integer label matrix from grayscale labeled object image?

Hi!

This may not be a strictly CellProfiler related question but I am having trouble getting the label matrix from a grayscale labeled object image.
I have used ilastik to get a probability map of a cell image that I want to segment in CellProfiler. I have successfully got the grayscale labeled image by using a ConvertObjectsToImage module and save this image as a tiff file. Here is the result:
H3+CD20Test_ProbabilitiesCell_Obj_Image.tiff (51.0 KB)
In the module help, it says that the Grayscale option creates a labeled matrix where each pixel is assigned a number specific to that object. I am wondering is there a way to extract this matrix from the grayscale image?
I have tried to read this image using EBImage package in R but it gives me a matrix with pixel intensity instead of integer labels. Here is the code I used to read in the grayscale image:

objs_img <- readImage(paste(objs_dir, "Cell_Obj_Image.tiff", sep=""))
matrix(objs_img[65:74,120:129],nrow=10,ncol=10)

Here is the matrix of a section of the grayscale image:


and the image of the section:
image

The module help of ConvertImageToObjects reads that "This module is useful for importing a previously segmented or labeled image into CellProfiler, as it will preserve the labels of an integer-labelled input. " This grayscale image can be converted back to objects correctly using the ConvertImageToObjects module, so I take that the input must have been an integer labeled matrix?
I have read in other posts (link follows) that we can use MATLAB to extract the label matrix. I am not really familiar with MATLAB, could anyone point me in a direction?

Export image for each object - Image Analysis - Image.sc Forum

Many thanks

Hi @Claris,

Welcome to the image.sc forum!

Would you mind taking a step back to explain your goal for your CellProfiler pipeline? Are you trying to segment your cells in ilastik and then import the segmented objects into CellProfiler? Or are you trying to create pixel intensity probabilities for each class of interest and then use those probabilities as images that you then use IdentifyPrimaryObjects on to segment the cells?

The workflows are different for these two options. If you’re not already committed to one method, I would recommend the second option - create pixel intensity probability images and then use those for segmentation in CellProfiler (that is our approach in the Broad’s Imaging Platform, as we find CellProfiler to be more customizable than ilastik for segmentation). This video tutorial outlines how to use that approach: CellProfiler Tutorial: pixel-based classification with ilastik - YouTube.

Good luck and please feel free to share more details and your sample pipeline if you need more help!
Best,
Pearl

1 Like

@pearl-ryder Thank you for your reply!
I have taken the second approach where I classified pixels in ilastik and created a pixel intensity probability image for each class of interest. Then I segmented the probability map in CellProfiler using IdentifyPrimaryObjects and then IdentifySecondaryObjects.
Here is the pipeline I have built
2_segment_ilastik.cpproj (575.1 KB)
Here is one probability image I used for the pipeline:
H3+CD20Test_Probabilities.tiff (215.4 KB)
Basically, I am trying to extract the integer label matrix from the grayscale labeled object image that I get from ConvertObjectsToImage. Is there a way to do this using CellProfiler?
Thanks again!

Aha, now I understand!

Yes, you can export this type of file in CellProfiler. In your ConvertObjectsToImage module, you’ll need to select “uint16” instead of Grayscale. Then, when you save it using the SaveImages module, you’ll want to select “tiff” as the format and “16-bit integer” as the bit depth. That should work, let us know if it doesn’t!

Cheers,
Pearl

1 Like

Thank you for the quick reply.
I give it a try but it does not work.
Here is the setup I have for SaveImage and ConvertObjectsToImage:


image

I will let you know if anything works!

Claris

Are you sure this isn’t a function of your loading library? It looks from the documentation for EBImage that images it opens are scaled 0-1.

You have a couple options, in that case- 1) multiply your matrix by your bit depth (255 for 8 bit, 65535 for 16 bit) to return to an integer scaling (0.5764706*255 is 147, 0.6039216 * 255 is 154, etc) OR 2) save out the images from CellProfiler as numpy arrays, and use a suitable library to open those instead (I’ve not done this in R so can’t recommend any in particular, but a google search confirms some exist).

Good luck!

1 Like