 # Calculate RGB values for all the particules

Hi all!

I am new in this forum and I am not an expert at all in Image Analysis, so my apologize in advance if my question is trivial :-/.
I am currently trying to conduct a project in ecology. I took several egg pictures (the pictures were taken in a box with a standardized light and the parameters of the camera were also fixed) and I would like to measure:

1. The spottiness of each eggs
2. The colors (RBG or HSB values) of these spots

Here an example of a raw egg picture:

1. For the spottiness I already used the “Analyse Particles” tool after modify the Color Threshold. This gave me this kind of picture:

I did the same for the whole egg in order to calcule the egg area covered by spots (spot areas/egg area)

1. Now I would like to get R, B, G values for the same particles I selected as spots. This I cannot reach… Previously I manually randomly selected 5 spot areas using the “Wand Tool”, but it is not the best, because I have a quite big variability in the spot colours within eggs. So, ideally I would like to work with the same “spot selection” as the Color Threshold makes.

Anyone know how to do that (if possible…)?
Cheers!

Good day,

yes it is possible!

If you like to see the RGB values for every selected spot, the following macro may help:

``````run("Set Measurements...", "mean redirect=None decimal=2");
roiManager("Split");
run("Make Composite");
n = roiManager("count");
a = Array.getSequence(n);
a = Array.slice(a,1,n);
roiManager("Select", a);
roiManager("multi-measure measure_all one append");
``````
1. Paste the above macro code to an empty macro window (Plugins >> New >> Macro).
2. In the “Threshold Color”-dialog, click “Select”.
3. Run the macro.

A Results window will then list many number-triples that give you the RGB values for every selection area.

Regards

Herbie

It seems to work well. Here what I obtained:

What is still confused for me is the Result table:
I get three lines, is the first for the R value, the sedcond for the G and the third for the B?
I guess (but not sure) that each column correspond to one selected particles? If yes, this is quite strange that the number of columns in the Result table is not the same as the number of particles I get after using the Analyse Particle function. For example, with the picture above I get 339 particles, while I have 1147 columns in the Result table after running the macro you kindly gave to me.

Sorry again for this probably naive question

Best regards

Well,

you are mostly right with your assumptions.

I get three lines, is the first for the R value, the sedcond for the G and the third for the B?

Yes.

I guess (but not sure) that each column correspond to one selected particles?

Yes.

With your sample image and a HSB-threshold H=fulPlass, S=fullPass, Brightness=0…105 and no Dark background, I get 645 selections (ROIs) and 645 RGB-entries to the Results table. No idea why I get 476 particles when using “Analyze Particles…”

The high number you see is due to the fact that the ROI manager wasn’t closed and you did your test several times. Here is the macro that closes it:

``````run("Set Measurements...", "mean redirect=None decimal=2");
roiManager("Split");
run("Make Composite");
n = roiManager("count");
a = Array.getSequence(n);
a = Array.slice(a,1,n);
roiManager("Select", a);
roiManager("multi-measure measure_all one append");
selectWindow("ROI Manager");
run("Close");
``````

Regards

Herbie

The following is a macro that performs all steps, however with the fix aforementioned setting of the “Color Threshold”.

``````setBatchMode( true );
run("Set Measurements...", "mean redirect=None decimal=2");
orig = getImageID();
run( "Duplicate...", "title=result" );
run( "Duplicate...", "title=temp" );
hsbThres( 105 );
selectImage("result");
run("Restore Selection");
selectImage("temp");
close();
selectImage("result");
run("Make Composite");
roiManager("multi-measure measure_all one append");
selectImage(orig);
run("Restore Selection");
run("From ROI Manager");
selectImage("result");
close();
//selectWindow("ROI Manager");
//run("Close");
setBatchMode( false );
exit();
// Color Thresholder 1.51s
function hsbThres( bmax ) {
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min=0;
max=255;
filter="pass";
min=0;
max=255;
filter="pass";
min=0;
max=bmax;
filter="pass";
for (i=0;i<3;i++){
selectWindow(""+i);
setThreshold(min[i], max[i]);
if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
selectWindow(""+i);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
}
``````

The regions in the resulting image are overlays that are saved with the image if saved in TIF-format and that can be hidden or shown. You may turn them into ROIs by “Image >> Overlay >> To ROI Manager”.

Regards

Herbie

Waouh amazing! Thank you!
So now if I want only one value for R, G and B, taking account all the spots selected, I just have to calculated the three mean of all the columns obtained
Thank you very much!!!

Good day,

originally you wrote that you wish to get

The colors (RBG or HSB values) of these spots

and now you write you like to see the mean color of all spots.

Here is a macro that does this and that uses “Analyze Particles…” that finds less regions than my previous approach but the mean colors differ only by less than 1%.

``````setBatchMode( true );
run("Set Measurements...", "mean redirect=None decimal=2");
orig = getImageID();
run( "Duplicate...", "title=result" );
run( "Duplicate...", "title=temp" );
hsbThres( 105 );
roiManager("Combine");
selectImage("result");
run("Restore Selection");
roiManager("Delete");
selectImage("temp");
close();
selectImage("result");
run("Make Composite");
roiManager("multi-measure measure_all one append");
selectImage(orig);
run("Restore Selection");
run("From ROI Manager");
selectImage("result");
close();
//selectWindow("ROI Manager");
//run("Close");
setBatchMode( false );
exit();
// Color Thresholder 1.51s
function hsbThres( bmax ) {
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min=0;
max=255;
filter="pass";
min=0;
max=255;
filter="pass";
min=0;
max=bmax;
filter="pass";
for (i=0;i<3;i++){
selectWindow(""+i);
setThreshold(min[i], max[i]);
if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
selectWindow(""+i);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
}
``````

If you should need the individual colors of the spots than please tell me and I shall change the macro accordingly.

HTH

Herbie

Well I probably wrote it not clearly, sorry for that…

I need to know the RBG values for the spots, true, but my aim was to get a mean for the R,B and G values for all the spot selected. But it would be biologically interesting to also get the R,B,G variation between the spots of the same eggs. So, using the previous macro you gave to me (and if I understand well), which calculate individually the RBG values for all spots, I can calculate the means and the variances.

Many thanks again!

Dear,

Mean( individual means ) ≠ Sum( values in all selections ) / Sum( areas of all selections );

where

individual mean = Sum( values in individual selection ) / ( area of individual selection );

and

Mean( individual means ) = Sum( individual means ) / ( number of selections );

Example:

area1 = 2pixel
value at each pixel = 1000
area2 = 4pixel
value at each pixel = 10

Sum( individual means ) / ( number of selections ) = ( 2000 / 2 + 40 / 4 ) / 2 = 1010 / 2 = 505;
Sum( values in all selections ) / Sum( areas of all selections ) = ( 2000 + 40 ) / ( 2 + 4 ) = 340;

Regards

Herbie

Ok, but how to know how ImageJ calculates the mean values it gives?

For example, if I manually select the spots in this picture:

After, I use the Plugin to obtain the RGB values. I get one “mean” value for R, G and B. So, is this value corresponds to the good mean, i.e. the mean of all the values for the pixels selected?

Thank you very much for your kind help, you are very patient with beginners (me)…
Kind regards

Dear,

as you may have realized I’ve posted two macros for you.

1. The first returns 476 number triplets (RGB values) for your sample image with HSB-threshold Brightness 0…105.
http://forum.image.sc/t/calculate-rgb-values-for-all-the-particules/7905/5?u=herbie

2. The second returns a single number triplet (RGB value).
http://forum.image.sc/t/calculate-rgb-values-for-all-the-particules/7905/7?u=herbie

(Please use the macro versions that are presently posted because, as noted earlier, they have been changed to now use both “Analyze Particles…”,)

If you compute the mean of the 476 triplets (re 1), you will get different RGB values than those obtained from 2.

Both results (RGB values) mean different things and you need to understand their difference in the context of your investigations (that I’m not aware of). This may help:

1. If you want to treat the various selected areas as individual measurements and do some statistics, such as Mean and Std, then you need to go with approach 1.
2. If you want to know the mean color in the sense that there is a virtual single (large) area, i.e. an area that represents the fused selections, then you should go with approach 2.

Please try to understand what makes the difference between 1. and 2., otherwise your work will not be of any scientific value.

Finally, you should be aware of the fact that color may change (at least slightly) from the center to the borders of the egg image, due to illumination and reflective properties of the egg shell pigments.

Regards

Herbie

1 Like

Yes, I already tried your very helpful macros My last example was built because I tryed to understand how the values (different between the two macros as you said) are calculated and what is the most appropriate method to answer to my biological question. And this is exactly what you point out in your last message In few words my project:
I took several pictures for different eggs (n = 621), layed by different bird females

I would like to calculate:

1. The spottiness (percentage of egg shell coverade by spots) of each egg (this I found how to do)
2. The colour (RBG values) of these spots
3. The variation in egg spot colour within eggs
4. The colour (RBG values) of the egg shell

About the egg spot colour: I would like to consider all the pixels for the egg spots as one entity and calculate what are the RBG values for this entity. So, should I have to go for the approch 2?

I also would like to consider the variation in pixel RBG values inside the entity (all the pixels from the egg spots). Biologically, the variation around the RBG values obtained by the approach 2 will inform if the spots are in the same colour (for example only dark spots) or if there is variation on the egg (for example, light spots and dark spots in the same egg). This variation in egg spot colour could vary between eggs from the different females. But to obtain that, it seems to correspond more to the approach 2 which allows me to calculate a standard deviation…

So, I am still lost I think Best regards

Sorry,

but all that can be stated about the differences of both methods has been stated by me. It appears that you didn’t yet grasp the mathematics and the associated meaning with respect to your task.

It is of course possible to use both methods, depending on whether you are considering the within egg statistics or the between eggs statistics. But in any case you have to understand what you are doing and why. This is where science begins!

Finally, don’t forget the presently subjective character of the threshold value “105”! An objective method should be found.

Regards

Herbie

I know what I want and what I need to calculate to answer to my biological question, I just don’t know how to access to that with ImageJ because this is the first time I use this software and the first time I have to work with image analyses in my studies.
I warmly thanks for you answer and I will do my best to understand the underlying behind the both methods
Regards

Dear,

your problem with the two approaches has nothing to do with ImageJ. It has to do with mathematics. Please make sure you understand what the results of both methods really mean.

1. Every number triple (RGB values) represents the mean color of a single selected patch or spot.
2. This single number triple (RGB values) represents the mean color of all selected patches or spots fused.

In my earlier post I’ve shown that the mean of the values in 1. don’t agree with the values in 2. Both represent different aspects of your data.

Do and understand the maths!

Best

Herbie

Hello Herbie

I realise this is a very old topic but the macro is very helpful for what I want to do as well - so thank you.

Is there a way to add the photo name as the Label in the results file? I have hundreds of images to do and I want to make sure that I am not getting any results confused.

Thanks,
Eve

Hi @EveTC,

Go to `Analyze > Set Measurements...` and enable the `Display label` option: This will add a column with the image name to the results table.

Nico

Amazing, thank you so much @NicoDF!

1 Like