Creating segments based on area?

Hi everyone. I’m currently trying to work out how I could segment out regions based on area - These regions happen to be the same colour as the background, so I wasn’t sure if thresholding would work.

The image is shown below - as you can see, within the large grey regions, there are small black “dots” (I am not interested in the larger black regions). Does anyone know how I could segment these out and assign them a different gray pixel value? (ultimate I am trying to create a multiphase image).
Any input is greatly appreciated - thank you!


You can try using a combination of the Minimum and Maximum Gray Level Morphological Filters to create a Gray Level “Close” Operation.

Sorry I could not find a wiki page describing the filters but here is a result using a Maximum Filter of 7.0 followed by a Minimum filter of 7.0

1 Like

You can threshold the image and then make a particle analysis which excludes particles of a certain size. They can be added to the ROI Manager. Here a macro with your image example:

run("Analyze Particles...", "size=0-50 show=Masks display clear add");
selectWindow("Mask of Clipboard");

Which results in:


You can then postprocess the ROI’s programmatically (assign a new value) or use them in a new image.

1 Like

Hi, Looks like osteocyte lacunae in trabecular bone to me. Don’t know exactly why you want the "little hole to come up in a different grey value, but this little macro seems to do the job:

run(“Duplicate…”, " ");

setThreshold(20, 255);

run(“Convert to Mask”);

run(“Duplicate…”, " ");

run(“Options…”, “iterations=3 count=1 do=Nothing”);

imageCalculator(“Subtract create”, “test-image-2.jpg”,“test-image-1.jpg”);

selectWindow(“Result of test-image-2.jpg”);
run(“Divide…”, “value=2”);


imageCalculator(“Add create”, “test-image.jpg”,“holes”);

This gives me this result:

Is this roughly what you were after?

1 Like

Yes correct they are osteocyte lacunae in trabeculae - essentially I needed them segmented so that when I exported the image stack, of which this image is a part of, to a 3D rendering program I could manipulate them - does this seem logical to you?
And yes the image above is what I’m after - thank you very much.

OK, so maybe you want them to stand out more. In that xcase, change to line to divide the image by 2 to a smaller factor, like 1.2 to improve contrast

1 Like

By the way, another way of doing this is not doing a “close” operation but a holefill, and then removing holes that are too big (using analyse particles). May give a cleaner result.

1 Like

One more thing, If you want to view this all in a 3D rendering program, it may be better to built up your image by thresholding the bone, than change the value of the pixels in that image say by dividing by four, and then add the holes binary divided by two to the bone image. After that there would be 3 phases: background with value “0”, Bone with value “64” and lacunae with value “128”.

1 Like

I understand how your image above was created - but do you know how I could create this 3-phase image, and repeat the macro across the entire stack? (apologies I am new to ImageJ and not too familiar with macros)

Actually ignore that - I have segmented the bone using changeValue(129,255,64) (as the lacunae were assigned a value of 128). Do you know how I can repeat your macro across an entire stack, however?

EDIT: On second thought, don’t worry about it - I think it’s just easier to apply your method manually to the entire stack at once.