Re: Error while script iteration over next folder in headless mode

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
GP
Reply | Threaded
Open this post in threaded view
|

Re: Error while script iteration over next folder in headless mode

GP
Hie!

I am getting an error while iterating over second folder in root directory in headless mode on windows.
The imageJ script is written to iterate over all the folders in the root folder.
It works well when executed via Fiji.
On running it in headless mode it works fine on first folder but as it moves to the next folder I get the following error:


The Script performs the following steps:
-- Rotates Image
-- Makes substack
-- Z-projects
-- Crops based on canvas size
-- Applies Convex Hull
-- Analyze particles

Macro is as follows:
function main(dir)
{
  files = getFileList(dir);
  for (i = 0; i < files.length; i++)
  {
    inDir = dir+files[i];
    if( File.isDirectory(inDir) )
    {
      print("Running entire process on: "+inDir);

      // STEP 1: CROP
      cropDir = inDir + "Crop/";
      File.makeDirectory(cropDir);
      step1_crop(inDir,cropDir);

      // STEP 2: AREAS
      areasDir = cropDir + "Areas";
      File.makeDirectory(areasDir);
      step2_areas(cropDir,areasDir);
    }
  }
}
function step1_crop( inDir, outDir )
{
  files = getFileList(inDir);
  for( i=0; i < files.length; i++ )
  {
    file = files[i];
    if(
         endsWith(file,".tif")
      || endsWith(file,".tiff")
    )
    {
      print(" Cropping: "+file);
      open(inDir+file);
      crop();
      saveAs("tiff", outDir+file);
    }
  }
}

function step2_areas( inDir, outDir )
{
  files = getFileList(inDir);
  for( i=0; i < files.length; i++ )
  {
    file = files[i];
    if(
         endsWith(file,".tif")
      || endsWith(file,".tiff")
    )
    {
      open(inDir+file);
      Title = File.nameWithoutExtension();
      areas();
      saveAs("tiff", outDir+file);
      saveAs("Measurements", outDir + Title + ".csv");
    }
  }
}

setBatchMode(true);
function crop() {
  sliceCount = nSlices();

  run("Rotate 90 Degrees Left");

  sourceWindow = getTitle();
  run("Find focused slices", "select=100 variance=0.000");
  label = getMetadata("Label"); // get the subtitle

  l1 = lengthOf("Z_");
  l2 = indexOf(label,"Z_");

  str1 = substring(label, l1+l2);
  str2 = substring(label, 0, l2);
  focus = parseInt(str1+str2); // convert string into integer, fs: focussed slide

  print(" Focussed Slice: " + focus);

  slice_first= focus - 4;
  slice_last = focus + 4;
  if( slice_first < 1 ) slice_first= 1;
  if( slice_last > sliceCount) slice_last = sliceCount;

  run("Make Substack...", "  slices=" + slice_first + "-" + slice_last);
  run("Z Project...", "projection=[Max Intensity]");
  run("Canvas Size...", "width=512 height=512 position=Center");
  run("Auto Threshold", "method=Li white");
}

function areas()
{
  run("Clear Results");
  run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding fit shape feret's integrated median skewness kurtosis area_fraction redirect=None decimal=3");
  run("Convex Hull Plus", "mode=[Convex Hull selection] white log");
  run("Analyze Particles...", "  show=Outlines display include");
}
setBatchMode(true);
main(dir);

*I close all the files as well I skipped that code block to follow MCVE.

Help is much appreciated.
Thank you so much.

Best Regards,