Posted by
AlvinH on
URL: http://imagej.273.s1.nabble.com/Straight-line-with-preset-length-macro-tp5014526p5014530.html
Thank you for the responses, Herbie and Emanuele.
I am new to ImageJ so I am not sure what "spatially discrete" images or "lines of arbitrary orientation" are. However, I'll try to describe what I have to do in more detail and maybe that'll explain what you want to know.
The image that I have to work with is
1025x684 pixels.
Example image with same resolution.
Once opened, I proceed to change the units from
pixels to µm (Image > Properties) as well as the
Pixel Width to 2.93 (Pixel Height and Voxel Depth also changes to 2.93 automatically). The 2.93 is just to make the
desired resolution roughly 3000x2000 µm (3003.25x2004.12 µm to be exact).
I found a macro provided by Wayne Rasband (
Thread)
macro "Set Length Line Tool - C00bL1de0L1ee1" {
maxLength = 20;
leftClick=16
i = 0;
xpoints = newArray(2000);
ypoints = newArray(2000);
getCursorLoc(x2, y2, z, flags);
xpoints[i]=x2; ypoints[i++]=y2;
while (true) {
getCursorLoc(x, y, z, flags);
if (flags&leftClick==0) exit();
if (x!=x2 || y!=y2) {
xpoints[i] = x;
ypoints[i] = y;
i++;
x2=x; y2=y;
xpoints2 = Array.trim(xpoints, i);
ypoints2 = Array.trim(ypoints, i);
makeSelection("freeline", xpoints2, ypoints2);
List.setMeasurements;
length = List.getValue("Length");
showStatus("length="+d2s(length,2));
if (length>=maxLength) exit;
}
}
}
This macro allowed me to draw a
freeline that'll stop at
20 µm. However, I want to be able draw with the
straightline tool that'll stop at
500 µm. Hence, I played around and changed the code to
macro "Set Length Line Tool - C00bL1de0L1ee1" {
maxLength = 500;
leftClick=16
i = 0;
xpoints = newArray(1025);
ypoints = newArray(684);
getCursorLoc(x2, y2, z, flags);
xpoints[i]=x2; ypoints[i++]=y2;
while (true) {
getCursorLoc(x, y, z, flags);
if (flags&leftClick==0) exit();
if (x!=x2 || y!=y2) {
xpoints[i] = x;
ypoints[i] = y;
i++;
x2=x; y2=y;
xpoints2 = Array.trim(xpoints, i);
ypoints2 = Array.trim(ypoints, i);
makeLine(288, 335, x2, y2);
List.setMeasurements;
length = List.getValue("Length");
showStatus("length="+d2s(length,2));
if (length>=maxLength) exit;
}
}
}
This new macro allowed me to draw a
straightline that'll stop roughly at
500 µm starting at coordinates 288,335 (just a random coordinate I chose). However, the straightline never really stops at exactly 500 µm (length measurement shows 501.30 µm).
Only after changing the
Known Distance to 500 in
Analyze > Set Scale (with other info being Distance in Pixels: 171. Pixel aspect ratio: 1.0, Unit of length: µm, Scale: 0.342 pixels/µm) and making the y-coordinate constant (
makeLine(288, 335, x2, 335)) will I be able to draw a straight line that stops at exactly 500 µm in length. This also changes the image resolution to 2997.08x2000.00 µm (which is fine).
This macro is doable however, it becomes difficult/time consuming to do it this way when I have to repeat this process hundreds of times.
I would like to know if the macro can be tweaked to allow me to draw a
straight line starting at any point on the image that can go in any direction and will stop at exactly 500 µm in length.
Sorry for the long reply. Thanks for reading/helping!