Numbered cell masks



I would like to know the best way to save ‘numbered mask’ images. That is, an image that consists of masks, where each mask is numbered differently for each cell (similar to running bwlabel() on an image of discrete masks). However I require that the numbering of the masks be consistent with the numbering of the cells as done by the cellprofiler analysis. My objective is to use the masks to identify the same cells and to run secondary analyses to be used in conjunction with the cellprofiler metrics.

Currently I have been saving the cytoplasm masks as grayscale. This produces an image of masks where each cell is numbered differently. However I need to divide by 768 to convert the mask numbers into consecutive integers (likely a bit-depth issue I imagine). Moreover, I have found that the numbers skip values. As an example, in an image for which cellprofiler identifies 65 cells, the mask numbers go up to 85, with 20 skipped values. My guess is that discarded (e.g. edge) cells are the reason for the skipped values. It seems however that if I follow the masked cells consecutively, they correspond exactly to the numbering of the cells by cellprofiler. Hence it seems that I can run secondary analyses and match them with the cellprofiler results.

However this process I have described is awkward and requires some coding to produce matching numbers in the masks. As well, I am not 100% certain that the consecutive mask numbers will always correspond to the cell numbering system in cellprofiler. Could there be a better, more direct way to obtain numbered cell masks where the numbers correspond to those in the cellprofiler output?

Thanks very much,



Hi Ernest,

This is a little odd, but it works. If you relate your nuclei to themselves, and then in ‘DisplayDataOnImage’, select Nuclei as your object, Parent as your measurement category and Nuclei as your feature, the object numbers of the nuclei (as Cellprofiler sees them) will display on the image.

This is because Relate ‘measures’ a child object’s parent objectnumber to allow for linking between parents and children. This is the ‘measurement’ you select to Display on the image; however since your parents and children are the same object, this will in fact be your object number.




Thanks very much for the reply.

I am having a bit of difficulty implementing the proposed solution. I have added the module ‘Relate’, in which I specify ‘Nuclei’ for both the children objects and the parent objects. Next I add the module ‘DisplayDataOnImage’ – I choose ‘Nuclei’ for object, but for category, there is no ‘Parent’ option. Should I choose ‘Children’ instead? Then for feature, I assume that I should manually type in ‘Nuclei’? I have experimented a little bit and the pipeline crashes, so I think that perhaps I am not using one of these modules correctly.

More generally however, it seems to me that this solution will produce an image with numbers displayed on top. What I really need however is a mask image such that pixels belonging to the different masks are assigned consecutive integers (similar to the output of bwlabel() when run on a discrete binary mask image). In this way, I hope to use matlab to loop through all of the masks and extract cells one-by-one for secondary quantification and to match results with cellprofiler analyses. I think that I would have difficulty making use of an image with numbers superimposed, which it seems is the output of DisplayDataOnImage.

Answers to these concerns would be greatly appreciated.

Thank you,



Hi Ernest,

Ah, you just want the label matrix- I misunderstood. The label matrix from the identified objects in IDPrimAuto is stored as three different images in handles.Pipeline:
UneditedSegmented: the orginial, unedited label matrix, with ALL objects including those on the edge and outside of size criterion.
SmallRemovedSegmented: the label matrix after objects outside the diameter range have been discarded.
Segmented: the final label matrix, edited for objects outside the diameter range and those touching the border of the image.

You can easily save this label matrix in SaveImages by typing the name of whichever label matrix you would like. For example, if your objects are nuclei, you would type ‘SegmentedNuclei’ under ‘What did you call the images you want to Save…?’

(This answers your previous question- any time ‘Other…’ is a choice on a CP popupmenu, you can click that and type anything in; to find out what measurements are accessible, you can look at that module’s help or for a more complete list of somewhat ‘hidden’ measurements, look at the output file in matlab)

But just beware, the objects will be numbered 1:{num of objects} so your image won’t look like an image, nor will it retain this numbering information you desire (since the labels are not in the 0-1 range images typically are). Since you’re working in matlab, I suggest saving the image as a .mat- when you open it in matlab, it will be the label matrix. You could play around with rescaling and bit depth, but you’re going to lose the info you’re looking for. If the end goal is matlab, I’d probably save the label matrix as a .mat.