Rodion Bulyžkin
Rodion Bulyžkin

Reputation: 1

ImageJ - Set measurements - Keeps returning NaN results

My custom macro is supposed to calculate multiple values (like Feret, MinFeret etc).

However some columns in the result table are receiving NaN values all the time (Feret, MinFeret, FeretX, FeretY, XM, XY...).

I've also checked settings of ImageJ (Analyze > Set measurements) and settings of project.

Strangely enough, once I checked out Feret diameter calculation, the checkbox is getting reset to unchecked each time I'm running macros. I'm primarily interested in Feret values, but these are not the only columns receiving NaN.

I would expect every calculation set in Set Measurements to be executed.

I'd attach my macro and resulting csv document. Any suggestions would be welcome.

Results:

My image samples:

Feret's diameter checkbox being reset:

Macro:

// Input folder containing images
input = "image_dir";
// Output folder to save results
output = "image_dir/output";
outputFileCSV = output + "/combined_results.csv";
outputFileXLS = output + "/combined_results.xls";

// Scale settings (replace with your values)
known_distance = 100; // Known distance (e.g., 100 micrometers)
pixels_per_unit = 108; // Number of pixels for 100 micrometers
unit = "um"; // Unit of measurement

// Create or clear the combined CSV file
File.delete(outputFileCSV);
File.append("File Name,Area,X,Y,XM,YM,Perimeter,BX,BY,Width,Height,Major,Minor,Angle,Circ.,Feret,IntDen,Median,Skew,Kurt,RawIntDen,FeretX,FeretY,FeretAngle,MinFeret,AR,Roundness,Solidity\n", outputFileCSV);

// Get list of files in the folder
list = getFileList(input);
setBatchMode(true); // Enable batch mode for faster processing

// Loop through each image file
   for (i = 0; i < list.length; i++) {  
afilename = list[i].toLowerCase(); // Convert to lowercase for case-insensitive check
   if (endsWith(filename, ".tiff") || endsWith(filename, ".png")) {
      dotIndex = lastIndexOf(list[i], ".");
      nameWithoutExtension = substring(list[i], 0, dotIndex); // Get the filename without extension
      open(input + "/" + list[i]);

         // Set scale
      run("Set Scale...", "distance=" + pixels_per_unit + " known=" + known_distance + " unit=" + unit + " global");

      // Set Measurements (make sure Perimeter, Centroid and other metrics are included)
      run("Set Measurements...", "area centroid bounding fit shape feret feretx ferety feretangle perimeter integrated median skewness kurtosis stack min & display");

      // Convert to 8-bit, apply threshold, and fill holes
      run("8-bit");
      setAutoThreshold("Default");
      run("Make Binary");
      run("Fill Holes");

      // Analyze particles with size filter to exclude too small particles
      run("Analyze Particles...", "size=50-Infinity clear");

      rows = nResults(); // Get the number of results
   for (r = 0; r < rows; r++) {
         // Extract metrics (including Perimeter)
      area = getResult("Area", r);
      x = getResult("X", r);
      y = getResult("Y", r);
   xm = getResult("XM", r);
      ym = getResult("YM", r);
      perimeter = getResult("Perim.", r); // Get Perimeter
      bx = getResult("BX", r);
      by = getResult("BY", r);
      width = getResult("Width", r);
      height = getResult("Height", r);
      major = getResult("Major", r);
      minor = getResult("Minor", r);
      angle = getResult("Angle", r);
      circ = getResult("Circ.", r);
      feret = getResult("Feret", r);
      feretX = getResult("FeretX", r);
      feretY = getResult("FeretY", r);
      feretAngle = getResult("FeretAngle", r);
      intDen = getResult("IntDen", r);
      median = getResult("Median", r);
      skew = getResult("Skew", r);
      kurt = getResult("Kurt", r);
      rawIntDen = getResult("RawIntDen", r);
      minFeret = getResult("MinFeret", r);
      ar = getResult("AR", r);
      roundness = getResult("Round", r);
      solidity = getResult("Solidity", r);
      }
      // Append data to the CSV file for the current image
      dataRow = nameWithoutExtension + "," + area + "," + x + "," + y + "," + xm + "," + ym + "," + perimeter + "," + bx + "," + by + "," + width + "," + height + "," + major + "," + minor + "," + angle + "," + circ + "," + feret + "," + intDen + "," + median + "," + skew + "," + kurt + "," + rawIntDen + "," + feretX + "," + feretY + "," + feretAngle + "," + minFeret + "," + ar + "," + roundness + "," + solidity + "\n";
      File.append(dataRow.trim(), outputFileCSV); // Save data to CSV

      close(); // Close the image
   }
}
setBatchMode(false); // Disable batch mode

// Convert the CSV file to XLS format
if (File.exists(outputFileCSV)) {
   // Open the CSV file as a Results Table
   run("Open...", "path=" + 'outputFileCSV');

   // Save it as an XLS file
   saveAs("Results", 'outputFileXLS');

   // Close the Results Table
   run("Close");
}

Upvotes: 0

Views: 71

Answers (0)

Related Questions