MACRO ROI background subtraction for a batch

imagej

#1

Dear ImageJ 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


#2

This is exactly how you learn to write “proper macros” :slight_smile:
… with the help of the documentation, which you seem to have already looked at.

One problem I can see is that the example macro that you found actually contains 2 methods of achieving a similar goal. Method 1 takes a single selection as input, and method 2 loops through the ROIs in the ROI Manager. The 2 methods are enclosed in macro {} blocks.

I’d suggest picking just one of the methods as the starting point for your macro. Probably the 1st method, where there is just 1 selection, is best. You can omit the macro { and } lines.

Then, wrap that code in the batch processing loop, as you did.

Remember to close() each image after processing.

If these tweaks don’t solve the problem, post what you came up with and I’m sure someone will have an idea.

Hope this helps.


#4

Thank you very much for the comment and guide, I am going to try again with your suggestion and post the result if worked.


#5

Hello Teresa
I tried again and it’s only processing the first image does not open the rest of the image in the folder. Macro errorHere is what I did:

macro "ROI background" {

path    = getDirectory("Choose a Directory");
list    = getFileList(path);
length  = list.length;

File.makeDirectory(path + "ImagingBG/");

//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");  
 macro "Subtract Measured Background 1" {
      if (selectionType==-1)
         exit("This macro requires an area selection");
      for (i=1; i<=nSlices; i++) 
          setSlice(i);
          getStatistics(area, mean);
          run("Select None");
          run("Subtract...", "value="+mean);
          run("Restore Selection");                
    saveAs ("Tiff", path + "ImagingBG/BG" + i);  
      run("Close All");  
   }   

and if delete the } in this line macro “Subtract Measured Background 1” {

I get an error window (please, see attached)


#6

Hi @Pablo_M,

(I added 3 backticks ``` before and after your code to make it easier to read onscreen.)

As the error message suggests, there is a bracket issue. But the error message isn’t always intuitive. In fact, it looks like you have 3 opening brackets and only one closing bracket.

To solve this,

  1. Delete all of the lines that start with macro {and their corresponding closing brackets. These lines are for packaging multiple macros in a single file. But I’m pretty sure you only need 1 macro to do this job.

  2. Make sure each for loop has an opening and a closing bracket.

Also, you have 2 nested for loops both using i as the counting variable. Try changing one of them to j.

That gives you the following, which works for me:

path    = getDirectory("Choose a Directory");
list    = getFileList(path);
length  = list.length;

File.makeDirectory(path + "ImagingBG/");

//Images substract

for (i=0; i<length; i++) { // loop through images in directory
             
	n=i+1;
	open(path+list[i]);
	img = File.nameWithoutExtension();
	selectWindow(img+".lsm");
	run("ROI Manager...");
	//setTool("rectangle");
	waitForUser("Draw ROI, then hit OK");  
	
	if (selectionType==-1)
		exit("This macro requires an area selection");
	for (j=1; j<=nSlices; j++) { // loop through slices in image
		setSlice(j);
		getStatistics(area, mean);
		run("Select None");
		run("Subtract...", "value="+mean);
		run("Restore Selection");                
	}
	saveAs ("Tiff", path + "ImagingBG/BG" + i);  
	run("Close All");  
}

If you’re using Fiji, the Templates menu in the script editor has many useful bits of code for future projects. In particular, the “process folder” templates are good to study, to help with batch processing tasks like this.

Hope this helps.


#8

Dear Teresa,
Thank you very much for the guide, tips, and help with this macro. It’s working perfectly.
Thanks again for your patience.
I will follow your suggestion in the coming macros that I am trying to adapt.
Kind regards
Pablo