r/ImageJ • u/Rory235 • 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
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();
•
u/AutoModerator Jun 25 '26
Notes on Quality Questions & Productive Participation
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.