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"); } |
How about using the "distance" as one of the options in Set Scale... For example:
run("Set Scale...", "distance=1.5 known=1 pixel=1 unit=mm"); ... will set 1.5 pixels to 1 mm. Ved ___________________________________ Ved Sharma, PhD Department of Anatomy and Structural Biology Albert Einstein College of Medicine Bronx, New York -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Ved,
The problem is that the distance in pixels which equals 1 mm will be different in every image, so I can't set it to a constant number like you suggested. I need to set it to a variable which I can automatically fill with the correct distance in pixels for each unique image, based on an object of known size which we are using to set the scale. I hope that makes sense. Andrew |
Hi Andrew,
The code snippet below allows the user to insert the distance in pixels without measuring it. Dialog.create("Pixel distance"); Dialog.addNumber("Give distance in pixels for 1 mm" , 0); Dialog.show(); pixel = Dialog.getNumber(); run("Set Scale...", "distance="+pixel+" known=1 pixel=1 unit=mm"); Best wishes Kees Dr Ir K.R. Straatman Senior Experimental Officer Advanced Imaging Facility Centre for Core Biotechnology Services University of Leicester http://www2.le.ac.uk/colleges/medbiopsych/facilities-and-services/cbs/lite/aif -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Andrew Sanchez Sent: 28 August 2015 05:19 To: [hidden email] Subject: Re: Automatically set scale Hi Ved, The problem is that the distance in pixels which equals 1 mm will be different in every image, so I can't set it to a constant number like you suggested. I need to set it to a variable which I can automatically fill with the correct distance in pixels for each unique image, based on an object of known size which we are using to set the scale. I hope that makes sense. Andrew -- View this message in context: http://imagej.1557.x6.nabble.com/Automatically-set-scale-tp5014169p5014178.html Sent from the ImageJ mailing list archive at Nabble.com. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Kees, Thanks for the snippet. I actually do need to measure the distance in pixels for each picture. The object used to set the scale will be the same color and size (1 square mm) and in the same location. The size of this object in pixels will vary from picture I picture but will represent 1 square mm, so I need to get the measurement each time . I'm looking for a way to automate this. I plan on running the images through a color threshold and then run analyze particles to get the scale for 1 square mm and then take the square root of that number to get the distance in pixels of 1 mm. Unless somebody has any suggestions for a more elegant solution Andrew
|
Free forum by Nabble | Edit this page |