Macro that only selects nuclei which have a cytoplasm stain but crops nuclei




I’m new to imagej macros and have a small but major problem with my nuclear selection program.
I have written a macro which:

  1. Creates a mask of all nuclei in an image
  2. Creates a selection mask using a cytoplasm stain
  3. Applies the cytoplasm selection over the nuclei mask
  4. Clears outside the cytoplasm selection (i.e removing all nuclei not with stained cytoplasm)
  5. It then records the fluorescent intensity within this specific cell type nuclear mask
    However, currently some nuclei which are close to the edge/half in half outside of the cytoplasmic stain are cropped because it clears the part of the nucleus that is outside of the cytoplasmic selection.
    Is there a command to keep the whole nucleus if that nucleus is touching the cytoplasm selection and only remove those nuclei which are not touching any cytoplasm?


If you are talking binary regions, then you could try Binary Reconstruction of the nuclei “mask”, using the cytoplasms as “seeds” (those are the terms used in the plugin).
That command would return all the nuclei that partially or fully overlap with the cytoplasmic masks.
A Binary Reconstruction plugin is in my “morphology collection” zip file
(and in Fiji available through the “Morphology” update site).


Hi Gabriel,

Thanks for your help! I tried your Binary Reconstruction but the reconstruction came back exactly the same as the nuclei mask (images below)? I have highlighted a clearly negative cell which should have been removed so I’m not sure if it did anything to the partially overlapping cytoplasm/nuclei or just didn’t do anything?
Have I got the right input images having both as binary thresholds or does one need to be a selection and the other a binary threshold?
Any additional help would be greatly appreciated!
Many thanks


When I apply:
run("BinaryReconstruct ", “mask=nucl seed=cells create white”);
the nucleus that you highlighted does not get reconstructed (because there is no cell seed to reconstruct it), so the plugin works fine.
I suspect that you got wrong the “white” option. Your regions are black, not white, so untick that option.


So the first time I ran it I didn’t tick “white particles on black background” and got the images I posted.
I re ran it having ticked that box and it gave the correct result, seems the wrong way round? - but it works!

Thanks for the plugin


Not sure I follow because the images you posted are not binary (download them and run a histogram to see why).
I had to threshold them again to convert them to binary. Perhaps you probably have an inverted look up table in those images so you see black and white inverted, sorry I can’t tell from here.
I suggest to make sure of what kind of image you submit to the plugin to avoid this type of confusion. The plugin considers “white” to be the greyscale value 255 and “black” to be 0. Hope it helps.


Hi, just a small comment on the non-binary images, the images @tgfrancis7 posted were jpegs, the jpeg compression changes some pixels rendering the image no-longer binary.

@tgfrancis7 you should avoid JPEGs, try saving as PNG instead :slight_smile:


Hi again @gabriel

Thank you for putting my on to your binary reconstruct plugin it works really well! I was wondering if there was a way to use it to delete the seed regions within the mask and keep the seed regions outside of the mask?

We are trying to look at the difference in nuclear fluorescent intensity from two different cell populations in a single image.



I would probably reconstruct the seeds and then subtract those from the original mask using the image calculator. Maybe I am missing something…


Thank Gabriel, I was just overcomplicated it!