Auto-select area and calculate mean RGB value

Hello all,

I have thousands of images like this:

What I would like to do is:

  1. automatically select the wing (no background)
  2. calculate the average RGB values for the wing.

I would also (added bonus) like to be able to select an area, say a 1/3rd of the wings area, nearest the point that would join the body and calculate the average RGB value there.

I have tried using the ROI manager and the macro below but I used this for only a few images as I have to set the ROI individually for each image. Is there a way to automate this better or perhaps a different way I have not thought of? I have thousands of pics and dont want to have to set ROIs for all of them individually.

function measure_stack(filename) {
       setOption("Stack position", true);
       for (n=1; n<=nSlices; n++) {

macro "Batch Measure RGB for ROI" { 

        run("Set Measurements...", "area mean standard min integrated limit display redirect=None decimal=3"); 
        dir1 = getDirectory("Choose an Image Directory "); 
        dir2 = getDirectory("Choose an ROI file Directory"); 
        dir3 = getDirectory("Choose an Results Directory");  

        list = getFileList(dir1); 
        list2 = getFileList(dir2); 
         for (i=0; i<list.length; i++) { 
                path = dir1+list[i]; 
                showProgress(i, list.length); 

                //loading ROIs here 
                                 path2 = dir2+list2[i]; 
roiManager("Open", path2); 
     //closes main image window 
                                if (isOpen("Exception")) { 
                                        run("Close");           //close error windows to avoid saving to results. 


path3 = dir3; 
run("Clear Results");

         if (isOpen("Results")) { 

My overall aim is to look at “lightness” or “darkness” of the wing

Thank you so much for your help in advance!


I took @mountain_man’s explanation in one of his thread. This is what I came up with for your questions 1 and 2.
I think @mountain_man will likely and nicely have a look at it.

run("Duplicate...", "title=1");
run("Duplicate...", "title=2");
//setThreshold(0, 144);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
run("Set Measurements...", "area mean add redirect=None decimal=0");
run("Analyze Particles...", "size=1000-Infinity display exclude include add");
run("RGB Stack");
run("Set Measurements...", "mean add redirect=None decimal=0");
roiManager("Multi Measure");

Hello Mathew,

Thank you for your answer, I will have a go at it now.
Do you know if I can change the column name in the results window to the name of the image?

@Mathew - don’t worry about the naming issue found the solution using: IJ.renameResults("Results");

@Mathew, after your help (thank you so much) I realised that I need the mean grey value and have progressed somewhat into the issue about selecting sections of the wing to measure. Although it isnt without problems.

I started a new issue that I am hoping you may have time to look at - although no worries if not (a cheeky request I know)! You can find it here - Divide ROI into 3 equal parts - not using vertical line but line of choice

Hi. It can help me, I try to calculate the percentage that represents the lesion in sheets with respect to the total area of each sheet, they are injuries like the following image:

I want to have an automated form like the one proposed, but I can’t get FIJI to calculate the area of the injury after I have calculated the area of each sheet.
Thank you

Hi @EdTerr5
A recommendation : Do not use jpeg.
JPG-compression introduces artifacts that can’t be removed!
You can find information here:

  1. To obtain the leaf area:

2. To obtain the Leaf Disease Coverage

1 Like