Issues saving a Summary file within Marcro

Hello,

I have made a macro to batch process a set of images. The final step yields a results table titled “Summary” that I need to save. Unfortunately when I write selectWindow(“Summary”); which is the name of the file I get notified that there is no file with that title. Does anyone know how to fix this issue? Any help would be greatly appreciated!

Kind regards,

Vanessa

Without knowing your macro here is a link to the macro API to save a results table:

http://rsb.info.nih.gov/ij/developer/macro/functions.html#S

Here an older post which might be helpful:

http://forum.image.sc/t/printing-text-to-analyse-particles-summary-table-and-save-the-table/355/4

Hello @Bio7, I appreciate your response! Below is the macro the macro I am using. It is a modified version of the NeurphologyJ interactive version plugin.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Neurphology macro
////
//// This macro quantifies neurite length, neurite area, neurite endpoints, neurite attachment points, and soma size
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



  
  requires("1.42k");
  run("Set Measurements...", "area mean standard modal shape limit redirect=None decimal=3");
  setForegroundColor(0, 0, 0);
  run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
  rename("original");
  run("Duplicate...", "title=flatten");
  run("Subtract Background...", "rolling=50");
{
  selectWindow("original");
  run("Duplicate...", "title=blurred");
  selectWindow("blurred");
  run("Gaussian Blur...", "sigma=1");
  imageCalculator("Subtract create", "original","blurred");
  rename("sub_blurred");
  selectWindow("blurred");
  close();
  selectWindow("sub_blurred");
run("Threshold...");
  getThreshold(lower1,upper1);
  close();
selectWindow("flatten")
  run("Threshold...");
  getThreshold(lower2,upper2);
  print("\\Clear");
  print("contrast level: " + lower1);
  print("soma intensity: " + lower2);
  
  lowc=19

  lowi=290

  nwidth=6

  cleanup=300
if(bitDepth()!=16)
  {
     run("16-bit");
  }
  else

////Background cleanup process

  ////detecting low contrast pixels
  selectWindow("original");
  run("Duplicate...", "title=blurred");
  selectWindow("blurred");
  run("Gaussian Blur...", "sigma=1");
  imageCalculator("Subtract create", "original","blurred");
  rename("sub_blurred");
  selectWindow("blurred");
  close();
  
  ////create a mask for low contrast pixels
  selectWindow("sub_blurred");
  setThreshold(0, lowc);
  run("Create Mask");
  rename("low_contrast");

  ////create a mask for low intensity pixels
  selectWindow("flatten");
  setThreshold(0, lowi);
  run("Create Mask");
  rename("low_intensity");
  run("Clear Results");
  selectWindow("flatten");
  close();

  ////create a mask to remove low intensity and low contrast pixels
  imageCalculator("AND create", "low_contrast","low_intensity");
  rename("zero_intensity");
  run("Invert");
  run("16-bit");
  run("Subtract...", "value=254");
  
  selectWindow("low_intensity");
  close();
  selectWindow("low_contrast");
  close();
  selectWindow("original");
  resetThreshold();
  imageCalculator("Multiply create", "original","zero_intensity");
  rename("new_original");
  
  ////soma detection
  selectWindow("new_original");
  run("Duplicate...", "title=open");
  run("Minimum...", "radius="+nwidth+"");
  run("Maximum...", "radius="+nwidth+"");
  setThreshold(685, 2212);
  run("Create Mask");
  rename("soma");
  run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");
  
  ////image clean up (remove small particles and debris) and soma removal
  selectWindow("new_original");
  run("Duplicate...", "title=neuritesoma");
  setThreshold(96, 4000);
  run("Create Mask");
  rename("neuritesoma_mask");
  setBackgroundColor(0, 0, 0);
  setAutoThreshold();
  run("Particle Remover", "size=0-"+cleanup+" circularity=0.00-1.00 show=Nothing");
  rename("neuritesoma_clean");
  run("Duplicate...", "title=neurite");
  selectWindow("soma");
  run("Create Selection");
  selectWindow("neurite");
  run("Restore Selection");
  run("Clear");
  run("Select None");
  
  ////neurite detection
  selectWindow("neurite");
  setAutoThreshold();
  run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");
  
  ////neurite length detection
  selectWindow("neuritesoma_clean");
  run("Duplicate...", "title=neuritesoma_skeleton");
  run("Skeletonize");
  imageCalculator("Subtract create", "neuritesoma_skeleton","soma");
  rename("neurite_length");
  setAutoThreshold();
  run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");
  selectWindow("neuritesoma_clean");
  close();

  ////attachment point detection
  selectWindow("soma");
  run("Select None");
  run("Duplicate...", "title=soma_dilate");
  run("Options...", "iterations=1 black count=1");
  run("Dilate");
  imageCalculator("AND create", "soma_dilate","neuritesoma_skeleton");
  rename("stem");
  run("Duplicate...", "title=stem_erode");
  run("Options...", "iterations=1 black count=7");
  run("Erode");
  imageCalculator("Subtract create", "stem","stem_erode");
  rename("stem_points");
  imageCalculator("Subtract create", "stem_points","soma");
  rename("attachment_points");
  setAutoThreshold();
  run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");
  run("Options...", "iterations=1 black count=1");
  run("Dilate");
  selectWindow("stem");
  close();
  selectWindow("stem_erode");
  close();
  selectWindow("neuritesoma_skeleton");
  close();
  selectWindow("stem_points");
  close();

  ////endpoint detection
  selectWindow("neurite_length");
  run("Duplicate...", "title=neurite_erode");
  run("Options...", "iterations=1 black count=7");
  run("Erode");
  imageCalculator("Subtract create", "neurite_length","neurite_erode");
  rename("tip");
  imageCalculator("Subtract create", "tip","soma_dilate");
  rename("endpoints");
  setAutoThreshold();
  run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");
  run("Options...", "iterations=1 black count=1");
  run("Dilate");
  selectWindow("neurite_erode");
  close();
  selectWindow("tip");
  close();
  selectWindow("soma_dilate");
  close();

  ////create color-combined image for user evaluation
  selectWindow("soma");
  run("Select None");
  selectWindow("original");
  run("8-bit");
  run("Merge Channels...", "red=neurite_length green=endpoints blue=soma gray=original create keep");
  rename("red=neurite green=endpoints blue=soma gray=original");
  run("Channels Tool...");
  run("Merge Channels...", "red=neurite_length green=attachment_points blue=soma gray=original create keep");
  rename("red=neurite green=attachment-points blue=soma gray=original");
  run("Channels Tool...");

  ////close windows that are not needed anymore
  selectWindow("sub_blurred");
  close();
  close();
  selectWindow("zero_intensity");
  close();
  selectWindow("open");
  close();
  selectWindow("new_original");
  close();
  selectWindow("neurite");
  close();
  selectWindow("neurite_length");
  close();
  selectWindow("soma");
  close();
  selectWindow("neuritesoma");
  close();
  selectWindow("neuritesoma_mask");
  close();
  selectWindow("attachment_points");
  close();
  selectWindow("endpoints");
  close();

And where did you add

selectWindow("Summary");

and can you post an example image?

Evtl. line 98: run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing summarize");, etc.?

I place Select summary on the last line! I am trying to save it with the title being the same, or similar to the original image title. I have attached a sample image in the dropbox link below! Thank you again for your help, @Bio7

I debugged the script a while in ImageJ and found out that I had to install the following
plugins (two classes) before the macro ran without a problem:

http://rsb.info.nih.gov/ij/plugins/particle-remover.html

Then I added the necessary lines at the end to save the summary and everything worked fine:

selectWindow("Summary"); 
saveAs("Results",  "C:/summary.xls");

So the solution for me was to install the missing plugins (maybee I should have read the docs before).

I hope this works for you, too.

After adding that last line to my macro I receive this message. Do you have any idea why?
Thank you!

You have to replace the file path with your custom MacOSX file path (my example uses a Windows path).

Something like:

saveAs("Results", "/Users/yourusername/Desktop/summary.xls");

Simply add your desired file path.

Ah that make sense ( should have been obvious!). Thank you! When I batch process my images now, only one file saves of the batch and the rest remain open. I’m thinking this may be because only one file can have the name “summary”. Do you know how to make it so that the results save as the same title as the initial image it opens?

You can create a variable of your input image title at the beginning, e.g.,:

imageTitle=getTitle();

And then use this variable (imageTitle) to save the summary table for this image.

saveAs("Results", "/Users/yourusername/Desktop/"+imageTitle+"_summary.xls");

You can also use other information (if necessary) to enhance your file name:

http://imagej.net/developer/macro/functions.html#getInfo

or create a unique id for the filename.

This worked perfectly! Thank you so much, @Bio7 !