Automatically set scale
Posted by Andrew Sanchez on Aug 27, 2015; 6:11pm
URL: http://imagej.273.s1.nabble.com/Automatically-set-scale-tp5014169.html
I am working on a macro that will automatically set the scale for all images in a directory, then take one line measurement and save the results in a spreadsheet.
Right now, I am manually setting the scale by defining the value of "Distance in pixels" by drawing a line then run("Set Scale...", "known=1 unit=mm");
I am wondering if there is a way to set the value for "Distance in pixels" without manually drawing a line? Given that we are going to use a 1 mm square object that is the same color unique color (not present in any other part of the image) and in the same location in every image, is it possible to automatically input the measurements from the object of known size into "Distance in pixels," to avoid drawing the line for scale on each image?
One way I imagine to do this is to set a threshold which selects the 1 mm square, then count the number of pixels composing the sqaure. This number of pixels will equal 1 sqaure mm, so I can then take square root of this number to get the length in pixels of one mm. I can then use that number for "Distance in pixels."
But how can I fill the value of "Distance in pixels," with that number in a macro?
This is the macro I am currently working with:
dir1 = getDirectory("Choose Source Directory ");
list = getFileList(dir1);
//setBatchMode(true);
for (k = 0; k<list.length; k++) {
showProgress(k+1, list.length);
open(dir1+list[k]);
//setTool("line");
waitForUser("Set the scale for this image","Hold shift to draw a straight line of known length, then click 'OK'");
run("Set Scale...", "known=1 unit=mm");
//setTool("line");
waitForUser("Measure the length of desired object","Hold shift to draw a straight line across the object, then click 'OK'");
run("Set Measurements...", "display redirect=None decimal=4");
run("Measure");
selectWindow(list[k]);
close();
selectWindow("Results");
saveAs("text", dir1+"results.csv");
}