 # How to calculate the percentiles and entropy of a ROI selected on an MR image?

Hi there,

I wonder if anyone knows how to use Fiji to calculate the percentiles and entropy of a ROI selected on an MR image.

Thanks.

Lucas

Get the greyscale histogram of the ROI and compute it using the frequencies therein. I suppose you are referring to Shannon’s entropy ( S = -Sum( Pi * log2(Pi)) where P is the probability of each (ith) greyscale level).

Hi, Gabriel,

Thank you for your kind suggestion. Could you please tell me which command or plugin of Fiji can calculate the percentiles and entropy? I do not understand how to use the “frequencies therein”.

Lucas

There are no commands you need to write a script of a macro.

Leave the entropy aside. Are there any command or plugin of Fiji that can calculate the 25% and 75% percentiles of a ROI selected on an MR image?

What I said. You need to write a script of a macro.

I incidentally wrote a macro yesterday that needed to know the “start” and “stop” of a histogram based on percentiles, so you may find this extracted code useful as an example to get you started. It expects an RGB stack; since you are working with a greyscale image you must simplify the code accordingly. Remember to take into account your images’ bitdepth.

``````numLevels = 256;	//for 8-bit images or RGB
startPerc = 1;	//this percentage of pixels should be below the start level
stopPerc =  1;	//this percentage of pixels should be above the stop level
setSlice(1);	getHistogram(valR, histR, numLevels);	sumR = 0;
setSlice(2);	getHistogram(valG, histG, numLevels);	sumG = 0;
setSlice(3);	getHistogram(valB, histB, numLevels);	sumB = 0;
for (i = 0; i < valR.length; i++) {
sumR += histR[i];
sumG += histG[i];
sumB += histB[i];
}
for (i = 0; i < valR.length; i++) {
numR += histR[i];
numG += histG[i];
numB += histB[i];
if (numR <= sumR*startPerc/100) startR = i;
if (numG <= sumG*startPerc/100) startG = i;
if (numB <= sumB*startPerc/100) startB = i;
if (numR <= sumR*(100-stopPerc)/100) stopR = i;
if (numG <= sumG*(100-stopPerc)/100) stopG = i;
if (numB <= sumB*(100-stopPerc)/100) stopB = i;
}
print("R: ", startR, stopR);
print("G: ", startG, stopG);
print("B: ", startB, stopB);
``````

The “manual” calculation of sumR, sumG and sumB by summing the histogram values instead of just multiplying the image width by depth was written this way to make it easy to take into account NaN values since I plan to expand this to 32-bits in the future. So also here you can simplify the code by just say sum=getWidth*getHeight;

Thank you for your kind help.

Many thanks for your kind help.