What does MaskImage do?

I thought MaskImage.m applies the mask derived from the “object” to a “image”. The result MaskedImage is the original image, with pixels outside masks set to zero.
But I am looking at the MaskImage.m file. Second to the last line:
handles.Pipeline.(MaskedImageName) = OrigImage

There is no processing on the OrigImage in this function. Did I miss anything, or is it a bug?

What I want to do is use the mask generated from one channel on another channel.

Sarah Hu

Hi Sarah,

You are correct in your interpretation of what MaskImage does. I agree that the line with “handles.Pipeline.(MaskedImageName) = OrigImage” is not very useful, and should probably be deleted (though I won’t say it’s a bug, per se!). The next, and last, line with “CropMask” is the one we utilize, in any case.

In modules, such as IdentifyPrimAutomtic, if a CropMask(ImageName) exists, then we use, transparent to the user, the masked image, i.e. CropMask(ImageName). There are some modules which do not yet look for CropMask, so in those cases you can usually choose “Other…” when choosing an Image, and type out “CropMask”. E.g. if your image name is “OrigBlue”, and you mask it with MaskImage, then want to do some other manipulation like applying it to another channel, you can choose “Other…” -> “CropMaskOrigBlue” in the module after MaskImage.

Does that help?


Thank you David. I am sorry that I am still confused. Here is the core code in MaskImage.m:

%textVAR01 = From which object would you like to make a mask?
ObjectName = char(handles.Settings.VariableValues{CurrentModuleNum,1});

%textVAR02 = Which image do you want to mask?
ImageName = char(handles.Settings.VariableValues{CurrentModuleNum,2});

%textVAR03 = What do you want to call the masked image?
MaskedImageName = char(handles.Settings.VariableValues{CurrentModuleNum,3});

OrigImage = CPretrieveimage(handles,ImageName,ModuleName,‘MustBeGray’,‘CheckScale’);
ObjectLabelMatrix = handles.Pipeline.(‘Segmented’,ObjectName]);
CropMask = ObjectLabelMatrix>0;


My understanding is that “CropMask” is the binary mask, derived from labeled “Segmented” ObjectName].
What confuses me is that I thought, this “CropMask” is going be to applied on the OrigImage, the image chosen by the user. I am looking for a line like
And then save the masked OrigImage as the MaskedImageName in the second to last line.

I don’t know if I explained myself clearly… Please comment.

thank you

Indeed Sarah, you explained yourself clearly, and you do seem to have the right idea. I believe this wasn’t noticed before since the CropMask would be used for most subsequent calculations (e.g. thresholds) and so it wouldn’t have mattered unless you wanted to see the grayscale results of the masking. In any case, I have made these changes to the code which should make it into the next release:

MaskedImage = OrigImage;
MaskedImage(~CropMask) = 0;


Thanks for the heads up!

Thank you David.

Hi Sarah,

The new bugfix version of CellProfiler has just been released which addresses this omission, plus a few other problems. You can download it from here.