Adaptive thresholding an image based only on some area


I am trying to estimate the proportion of white pulp and red pulp in the spleen on photographed slides. I am experimenting with a workflow comprising:

  • ImageJ correcting for the uneven illumination of the slides (by adding an inverted background image from subtract background), deconvoluting the images into haematoxylin and eosin
  • Manually constructing a mask for each slide to remove non-spleen tissue (i.e. liver) and artefact areas
  • CellProfiler to threshold the masked haematoxylin image and identify one large object (total spleen)
  • Threshold the eosin image (only in the spleen area), then invert it
  • Measure the average intensity of the spleen area in the thresholded image to calculate the proportion of white pulp.

I can see how to threshold only the spleen area by using the PerObject setting, however on some slides the staining intensity clearly varies over the surface, and it would be useful to try adaptive thresholding over the spleen surface, but without the remainder of the image (background, liver, etc) biasing the thresholding operation.

From my reading, I believe that if use the newest release of CellProfiler and mask the eosin image to remove the non-spleen areas and leave them black, I can then subtract a small number (0.001) to leave the background negative, and this will then be excluded from the adaptive thresholding operations.

Is this correct? And if so, is it the best way to achieve what I want?



Hi Andrew,

It might be best to post a couple of example images so we can see if there’s a better approach.


Hi Mark,

Thanks for your reply and sorry for the delay. I’ve attached two images to demonstrate - one showing a mask for a spleen (originally an identified object within cell profiler) and the other the haematoxylin staining of the spleen slide (extracted using the deconvolution plug-in from ImageJ).

I would like to experiment with thresholding the haematoxylin image, but clearly don’t want the mass of black pixels that are outside the spleen to contribute. I see that I can use the PerObject selection to restrict my thresholding to the object area. However, I would like to try adaptive thresholding, again ignoring all black background pixels.

I believe that the thresholding algorithms ignore pixels with negative intensity values, hence my idea to subtract a very small number (e.g. 0.001) from each pixel in the image (as the haematoxylin staining is never completely dark in the image) and leave the non-spleen pixels negative, thus excluding them from consideration in the thresholding algorithm.

I was hoping to check that this (a) is indeed the case, and (b) is the best way to achieve the desired result!



Ah, I see what you are doing now. What you can do is use MaskImage with the haematoxylin image as the input and the spleen image as the mask. The spleen mask needs to be binary; if it’s not, you can use ApplyThreshold to make it so.

The thresholding algorithms in IdentifyPrimaryObjects all ignore masked pixels in their calculation if the input image was masked previously. So you shouldn’t need to perform any sort of image arithmetic to get what you want.


That’s brilliant, thanks for the advice.