Converting and then calculating parts of the image

Hello, Need someone who knows how to work in ImageJ software. I have MR images of human abdomen. I want to calculate the amount of fat tissue on the outer perimeter of the image/body and the amount of the inner fat tissue, located within the outer perimeter (3 areas - as can be seen in the colored attached image). The images should be analyzed with auto treshold plugin which converts grayscale pixels into binary images, based in a global historgram-derived method. Black pixels represent a adipose tissue and white pixels the remaining soft tissue. We should categorize the fat tissue into visceral and subcutanous fat, I can explain you more about that as I plan to do the separation mannualy. in the end we should calculate the amount of fat tissue with cm2, I guess we could convert pixels to cm and then calculate ? I have been testing the Imagej software but I am having problems separating two parts of of the image. Can anyone help me - show me how to do the whole proccess and I will do it on my remaining images ? I am willing to pay for tutoring. !If something was unclear I can explain more. Thanks, Marko

Hi, as far as I see that is super simple.
I would first apply a filter such as a median filter to smooth out the signal.
But could be that the noisy bits in at the edges of the different signal portions are .jpg artifacts actually.

Process > Filters > Median…

The image needs to be converted to 8-bit. But I guess this is also a .jpg problem and the orginal is actually gray scale.

Image > Type > 8-bit

Then you can run intensity based thresholds:

Image > Adjust > Threshold…

Depending on which portion of your fat tissue you are interesed in you can toggle on/off the Dark background switch.

This gives you a binary mask. To separate out now the different objects properly you use the Particle Analyzer:

Analyze > Analyze Particles…

Set the size filter accordingly to get to the largest object and use the Exclude on Edges option to get rid of the white background. I think for your roundish outer fat tissue the circularity filter should be good as well.

How does one decide which filter thresholds to use. Easy peasy:
Use the Analyze Particles with the Add to Manager option on. Then measure area and the shape descriptor: Circularity (Analyze > Set Measurements). Press in the ROI Manager > Measure and you get all the measurements. The row number corresponds to the ROI number. Choose now a threshold for both that captures only the object you are interesed in.

I would do this separately for the outer and the inner fat tissue.
Upon request I am willing to write a small macro that show cases this in more detail.

1 Like

Hello, thank you for such a detail and quick reply. I did as you did untill the analyze particles part where I got lost. What do I select in size, circularity, “show” ?
I am interested in a macro that showcases that in more detail.
Thanks

Hi,

the settings of the analyze particles are described here in more detailed with examples: https://imagej.nih.gov/ij/docs/guide/146-30.html#sub:Analyze-Particles…

Here is a working example of how this could look like. The result of this macro are “Mask of InnerTissue” and “Mask of OuterTissue”. The settings of the Particle Analzyter are key here. Depending on what to exclude further or include one can do different things. One can process for example the masks more. Or use more parameters for filtering the image.

The actual implementation diverges from what I described before. I just do one threshold and now invert to filter the mask for the different objects.

// first duplicate the image
image = getTitle();
selectImage(image);
run("Duplicate...", " ");

// rename to differentiate
rename("ProcessingImage");

// convert to 8-bit
run("8-bit");

// median filter with small radius
run("Median...", "radius=1");

// threshold image
setAutoThreshold("Default dark");
setOption("BlackBackground", true);
run("Convert to Mask");

selectImage("ProcessingImage");
run("Duplicate...", " ");
// rename to differentiate
rename("OuterTissue");
run("Invert");

selectImage("ProcessingImage");
run("Duplicate...", " ");
// rename to differentiate
rename("InnerTissue");

// select only the outer part of the selection
selectImage("OuterTissue");
run("Analyze Particles...", "size=10000-Infinity circularity=0.20-1.00 show=Masks add");
selectImage("Mask of OuterTissue");
run("Invert LUT");

// select only the outer part of the selection
selectImage("InnerTissue");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Masks exclude");
selectImage("Mask of InnerTissue");
run("Invert LUT");

// close some intermediates
close("ProcessingImage");
close("InnerTissue");
close("OuterTissue");
1 Like

If all that is to complicated you can also use the magic wand tool: https://imagej.nih.gov/ij/docs/guide/146-19.html#sub:Wand-Tool

It basically thresholds pixels that have the same gray value as the pixel one selects within tolerance and are connected.

1 Like

Hello, sorry for the late reply and thank you for your time.

I ran the macro and got this as a results

Works great on doing the steps you mentioned before and speeds thing up - thanks.

However I am wondering for the outer tissue, can we somehow delete the black fat tissue that was inside the circle of the outer tissue ?

As for the inner tissue, can we put a code inside the macro to select and calculate all white dots on the image (that would be our soft tissue - all but fat) ?

Same question comes as for the outer tissue - how to delete the outer tissue circle to calculate only black pixels inside the outer tissue “circle” ?

If that actions cant be automatized, can I use something like freehand selections on two sides of the for example outer tissue so I calculate it manually ?

Thanks again.

If I am doing it right, I can use the wand tool, and select the black parts of the inner tissue image and after selecting each, click measure. After all were selected I can calculate how much black parts of the image are there. For the outer fat tissue I can use the freehand selection and make a circle on the inner circle of the outher fat tissue and press “del” it will make it black, I then use the wand tool, but it selects/measures the black are inside the white outer tissue, how can I bypass that ?

Hm the result looks a bit different from what I expected it to be.
Could you add the following line to the top of the macro and run it again?

run("Colors...", "foreground=white background=black selection=yellow");
run("Options...", "iterations=1 count=1 black");

For your questions:
A delete the black fat tissue that was inside the circle of the outer tissue:
I don’t understand it based on the image you provided. Could you point it out what you mean in the images?

B. to select and calculate all white dots on the image:
Yes one can could for sure calculate objects like that. Do you just want the total area?

C. how to delete the outer tissue circle to calculate only black pixels inside the outer tissue “circle”:
Again I don’t understand based on the image you provide. Could you specify what you mean in the image?

It would help to have the original example and the output of the macro to understand your problem and adjust it.

Much did not change with the added code.
I am sending you the original images Desktop.rar (146.4 KB)

A: look at the image A.jpg I need to calculate the remaining white area.

B: I want the total of the outer fat tissue, total of the inner fat tissue and the rest of soft tissue, bones etc… (3 measurements in total)

C: I filled the outer fat tissue with black, so the remaining white area should be calculated - image C.jpg

Ok, I guess cropping the specific parts you are interested in seems to be the best decision here anyways. Since the clear separation between the different tissues seems to be tricky.

I would suggest you then to drop the macro - or most of it, it is not helpful.
Just perform the segmentation:

// first duplicate the image
image = getTitle();
selectImage(image);
run("Duplicate...", " ");

// rename to differentiate
rename("ProcessingImage");

// convert to 8-bit
run("8-bit");

// median filter with small radius
run("Median...", "radius=1");

// threshold image
setAutoThreshold("Default dark");
setOption("BlackBackground", true);
run("Convert to Mask");

Crop out the areas that separate the Inner and the outer Tissue.

You can then just do the measurement by performing the Analyze Particles operation and selecting the measurements you want. To be really sure you do it only in the area of interest I would suggest you to add it to the ROI manager and then do the measurement.

So select the mask image. Then run with add to ROI Manager turned on:
Analyze > Analyze Particles…
Analyze

The ROIs will be added to the ROI Manager:
ROImanager

Set the measurements you want to perform (https://imagej.nih.gov/ij/docs/menus/analyze.html#set):
Analyze > Set Measurements…

Select the ROI you want to measure (exclude bad segmentations):
Press Measure in the ROI Manager.

This gives you a Result table with the Measurements you wanted.

Hello, thank you again for the reply. I have been super busy in the last days so didnt have time to reply to you.

Let me show you what I did, I opened the image, applied the macro:


Then I croped out the middle to get the outer fat tissue only:


Then I deleted the white remains of the texts:

Then I went to Analyze > Analyze Particles…


Does the imagej calculate only the black pixels ?

Set measurements (hardy understood anything on the info page that you sent :frowning: )
111

And this was the results
112

Did I do it correctly ?
How could I invert colors to get this inner tissue ?

Thanks

Hi,

in principle yes, you did it correctly. Concerning if it processes black or white: The Analyze Particles should only consider pixels that are in the foreground.
If you specify white as foreground ( ***Edit > Options > Colors…***) and the white parts in your images are of value 255 then it should only consider those.

When you perform the Analyze Particles then chose:
Show: Nothing

For the area measurement then just go for Summarize. This will give you the area of the foreground pixels in the entire image under Total Area in the results table.

Measuring the mean and min, max values in the binary image is not very informative. But one could measure those in the original image by using the Redirect to function in the set measurements.

1 Like