Skip to content

Instantly share code, notes, and snippets.

@PMLLancao
Created April 13, 2018 23:07
Show Gist options
  • Save PMLLancao/cae9339705dc49e73227f7629cf45dca to your computer and use it in GitHub Desktop.
Save PMLLancao/cae9339705dc49e73227f7629cf45dca to your computer and use it in GitHub Desktop.
MACRO ROI background subtraction for a batch IMAGEJ
Dear GitHub community
I was wondering if someone can help me to improve a Macro that I found here https://imagej.nih.gov/ij/macros/SubractMeasuredBackground.txt
I do not know how to write proper macros so I am trying to adapt it.
I would like to do a background subtraction from an ROI to every single image of a folder, but I would like to have the" waitforuser" command in this macro because every image has different element on it, and I will have to choose every time a different spot in the image to draw a ROI.
Furthermore, I would like to save these processed images in another folder as Tiff or as the original file from Zeiss .LSM.
So far the macro runs and opens and processed only the first image.
/ This macro subtracts the mean gray level calculated
// from user-defined selections that have been added to the
// ROI Manager. It works with both single images and stacks.
path = getDirectory("Choose a Directory");
list = getFileList(path);
length = list.length;
File.makeDirectory(path + "Imaging/");
//Images substract
for (i=0; i<length; i++) {
n=i+1;
open(path+list[i]);
img = File.nameWithoutExtension();
selectWindow(img+".lsm");
run("ROI Manager...");
//setTool("rectangle");
waitForUser("Draw ROI, then hit OK");
roiManager("Add");
macro "Subtract Measured Background 2" {
run("ROI Manager...");
waitForUser("Draw ROI, then hit OK");
n = roiManager("count");
if (n==0)
exit("This macro requires at least one ROI Manager entry");
sum = 0;
for (i=0; i<n; i++) {
roiManager("select", i);
getStatistics(area, mean);
sum += mean;
}
average = sum/n;
run("Select None");
run("Subtract...", "stack value="+average);
saveAs ("Tiff", path + "Image" + i);
}
I hope someone can help me to solve this and I think will be useful for others as well.
Thanks in advance for any help.
Regards
Pablo Munoz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment