2

Is it remotely possible to have a macro that automatically set scale multiple times from an image and then use it to calculate arae and other measurement parameters ?

for example i have over a thousand of the image below, with usually three views of the same organism. i will like to use the scale bar under each image (100 microns constant) to determine the length, width and area of each image. The scale bars are always located below each image and this is fairly constant for all the other images. Any help or guidance will be appreciated.

And please be patient with me i'm new to the imagej macro scripting...

Agglutinella sp.

Hammao
  • 801
  • 1
  • 9
  • 28

1 Answers1

3

Here is a macro to get you started:

#@ Double (label = "Scale bar length in microns", value = 100) scaleBarLength

// Processes the active image, closing it when finished.
function process() {
  // Measure the width of the scale bar.
  roiManager("reset");
  setThreshold(250, 255); // threshold the scale bar
  run("Analyze Particles...", "size=200-Infinity add");
  if (roiManager("count") != 1) {
    exit("Failed to isolate the scale bar");
  }
  roiManager("select", 0);
  Roi.getBounds(x, y, width, height);
  run("Duplicate...", "scalebar");
  pixelsPerPhysical = getWidth();
  close();

  // Crop off the no-longer-needed scale bar.
  makeRectangle(0, 0, getWidth(), y - 1);
  run("Crop");

  // Assign the physical calibration.
  micronsPerPixel = scaleBarLength / pixelsPerPhysical;
  print("Microns per pixel = " + micronsPerPixel);
  run("Properties...", "unit=micron" +
    " pixel_width=" + micronsPerPixel +
    " pixel_height=" + micronsPerPixel +
    " voxel_depth=" + micronsPerPixel);

  // Segment the main structure.
  setAutoThreshold("Default dark");
  run("Create Mask");
  run("Dilate");
  run("Erode");
  run("Fill Holes");
  run("Create Selection");
  close();
  run("Restore Selection");

  // Measure the main structure.
  run("Measure");

  // All done.
  //close();
  selectImage(orig);
}

// Chop up the image and process each piece.
orig = getImageID();
w = getWidth();
h = getHeight();

// Isolate and process left image.
print("Processing left image");
makeRectangle(0, 0, w / 3, h);
run("Duplicate...", "left");
process();

// Isolate and process middle image.
print("Processing middle image");
makeRectangle(w / 3, 0, w / 3, h);
run("Duplicate...", "middle");
process();

// Isolate and process right image.
print("Processing right image");
makeRectangle(2 * w / 3, 0, w / 3, h);
run("Duplicate...", "right");
process();

run("Select None");

This macro makes some assumptions:

  • All your images are divided into exactly three equally wide parts.
  • The scale bar is the only white/nearly-white chunk of pixels. You can tune that latter assumption by changing the [250, 255] range in the setThreshold call to control exactly how bright the scale bar will be, and changing the size=200-Infinity to control exactly how big the expected size of the scale bar will be in pixels.
  • Auto-thresholding, followed by a simple dilate/erode/fill-holes sequence, will result in a reasonable mask of the object(s) to be measured. This can be adjusted in part labeled "Segment the main structure."

For the data posted above, the macro emits the following to the Log:

Processing left image
Microns per pixel = 0.6579
Processing middle image
Microns per pixel = 0.8696
Processing right image
Microns per pixel = 0.8929

And it emits the following information to the Results window:

Results table

You can control which columns get calculated using the Set Measurements... command.

Use setBatchMode(true) to make things happen in the background (without windows being visibly created and deleted).

For more information, see this guide to macro programming.

Finally, be careful with JPEGs. It is better not to use them for quantitative image analysis unless you are certain it is OK. See this article for details.

ctrueden
  • 6,751
  • 3
  • 37
  • 69
  • thank you for the macro, sadly the macro failed to run even on the .tif format of the sample image i provided above. I'm adding a link to a folder so as to give you an idea how my images look like. Again, thank you for devoting your valuable time to help me... – Hammao Nov 27 '17 at 12:28
  • https://www.dropbox.com/sh/v1awerv7t1calqw/AAAoxV6FfwfPu_TGcQ_DEHLQa?dl=0 – Hammao Nov 27 '17 at 12:37
  • 1
    It seems you cross-posted this question [here on the ImageJ Forum](http://forum.imagej.net/t/is-it-remotely-possible-to-have-a-macro-that-automatically-set-scale-from-an-image/8025?u=ctrueden), where you already got an answer. So I'm not going to pursue this matter any further. Next time, please _disclose_ that you cross-posted, to avoid people duplicating effort. – ctrueden Nov 28 '17 at 16:34
  • @Ctruden, thanks for pointing out the cross post issue... i will take note next time. again, thanks for your help. – Hammao Dec 05 '17 at 15:55