Calculate RGB values for all the particules

imagej

#1

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…)?
Thank you very much in advance for your help!
Cheers!


#2

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("Add");
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


#3

Hi and thank you very much for your answer!!!
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


#4

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("Add");
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


#5

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 );
run("Analyze Particles...", "add");
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]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=bmax;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  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


#6

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!!!


#7

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 );
run("Analyze Particles...", "add");
roiManager("Combine");
selectImage("result");
run("Restore Selection");
roiManager("Delete");
selectImage("temp");
close();
selectImage("result");
roiManager("Add");
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]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=bmax;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  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


#8

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!


#9

Dear,

please note the following inequality:

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


Strange numbers in average values
#10

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


#11

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


#12

Thank you for your reply

Yes, I already tried your very helpful macros :slight_smile:
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 :slight_smile:

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 :frowning:

Best regards


#13

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


#14

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


#15

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