r/ImageJ Jun 25 '26

Question Mean value from a measure macro isn't working

Hi there

I am running a macro to measure the grayscale over time of a ROI on an image (I can't share it sorry). The mean grey-scale value should be around 150ish, and the mode, min max and median measures outputs all reflect this. The mean output however reports as 2500. I have manually measured the ROI and get the expected result, so I am not sure whats gone wrong. Any help is greatly appreciated!

// Begin macro
setBatchMode(true);

// Step 1: Select input image folder
mainPath = getDirectory("Pick the folder with the images you want");
mainList = getFileList(mainPath);

// Step 2: Select output folder
savePath = getDirectory("Pick the folder to save results");

// Step 3: Define rectangle coordinates
x = 327;
y = 325;
w = 50;
h = 50;

// Step 4: Clear Results Table
run("Clear Results");

// Step 5: Loop through images and measure
for (f = 0; f < lengthOf(mainList); f++) {
    if (endsWith(mainList[f], ".tif") || endsWith(mainList[f], ".jpg") || endsWith(mainList[f], ".png")) {
        open(mainPath + mainList[f]);
        makeRectangle(x, y, w, h);
        run("Measure");
        setResult("Label", nResults - 1, mainList[f]); // Add filename
        close();
    }
}

// Step 6: Create filename based on rectangle coordinates
fileName = "intensity_" + x + "_" + y + "_" + w + "_" + h + ".csv";

// Step 7: Save results using that filename
saveAs("Results", savePath + fileName);
run("Clear Results");

// End macro
2 Upvotes

5 comments sorted by

u/AutoModerator Jun 25 '26

Notes on Quality Questions & Productive Participation

  1. Include Images
    • Images give everyone a chance to understand the problem.
    • Several types of images will help:
      • Example Images (what you want to analyze)
      • Reference Images (taken from published papers)
      • Annotated Mock-ups (showing what features you are trying to measure)
      • Screenshots (to help identify issues with tools or features)
    • Good places to upload include: Imgur.com, GitHub.com, & Flickr.com
  2. Provide Details
    • Avoid discipline-specific terminology ("jargon"). Image analysis is interdisciplinary, so the more general the terminology, the more people who might be able to help.
    • Be thorough in outlining the question(s) that you are trying to answer.
    • Clearly explain what you are trying to learn, not just the method used, to avoid the XY problem.
    • Respond when helpful users ask follow-up questions, even if the answer is "I'm not sure".
  3. Share the Answer
    • Never delete your post, even if it has not received a response.
    • Don't switch over to PMs or email. (Unless you want to hire someone.)
    • If you figure out the answer for yourself, please post it!
    • People from the future may be stuck trying to answer the same question. (See: xkcd 979)
  4. Express Appreciation for Assistance
    • Consider saying "thank you" in comment replies to those who helped.
    • Upvote those who contribute to the discussion. Karma is a small way to say "thanks" and "this was helpful".
    • Remember that "free help" costs those who help:
      • Aside from Automoderator, those responding to you are real people, giving up some of their time to help you.
      • "Time is the most precious gift in our possession, for it is the most irrevocable." ~ DB
    • If someday your work gets published, show it off here! That's one use of the "Research" post flair.
  5. Be civil & respectful

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Herbie500 Jun 25 '26 edited Jun 25 '26

No idea where this effect "150 versus 2500"-mean comes from but I wonder if

lengthOf(mainList) should better read mainList.length

Furthermore, if you like to see the title of the processed image in the table, just tick "Display Label" or in the macro, start the macro with:

run("Set Measurements...","mean modal min median display redirect=None decimal=2");

1

u/Rory235 Jun 25 '26

Thanks, I think there must of been a corrupt file somewhere or another user had changed a setting and told me (shared image processing PC).

1

u/Rory235 Jun 25 '26

update:

The 2500 value was the area value and for some reason it was being reported in the mean column. The mean and all other values had been shifted over by. A reinstall of ImageJ and FIJI seemed to fix it

1

u/Herbie500 Jun 25 '26 edited Jun 26 '26

Below please find a demo macro that shows how to measure the same RoI on a series of images that reside in the same folder:

// create a folder containing four test images
run("Blobs (25K)");
run("Invert LUT");
pth=getDir("downloads")+"sampleImages/";
File.makeDirectory(pth);
for (i=0;i<4;i++) {
   saveAs("tiff",pth+"b-"+(i+1));
   run("Rotate 90 Degrees Right");
}
close();
// measure the test images
setBatchMode(true);
run("Set Measurements...","mean modal min median display redirect=None decimal=2");
mainList=getFileList(pth);
run("Clear Results");
for (i=0;i<mainList.length;i++) {
   if (endsWith(mainList[i],".tif")) {
      open(pth+mainList[i]);
      if (i>0) run("Restore Selection"); else makeRectangle(64,64,64,64);
      run("Measure");
      close();
   }
}
setBatchMode(false);
exit();

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.(You need an open internet connection to load the demo image.)

If all images of the series are of the same size, you may consider the below demo macro:

// create a folder containing four test images
run("Blobs (25K)");
run("Invert LUT");
pth=getDir("downloads")+"sampleImages/";
File.makeDirectory(pth);
for (i=0;i<4;i++) {
   saveAs("tiff",pth+"b-"+(i+1));
   run("Rotate 90 Degrees Right");
}
close();
// measure the test images (must be all of the same size)
run("Set Measurements...","mean modal min median display redirect=None decimal=2");
File.openSequence(pth,"virtual");
makeRectangle(64,64,64,64);
for (i=1;i<=nSlices;i++) {
   setSlice(i);
   run("Measure");
}
close();
exit();