Login  Register

Re: Straight line with preset length macro?

Posted by Michael Schmid on Oct 02, 2015; 4:56pm
URL: http://imagej.273.s1.nabble.com/Straight-line-with-preset-length-macro-tp5014526p5014531.html

Hi Alvin,

if you want a straight line, try something like this:

macro "Fixed Length Line Tool - C00bL1de0L1ee1" {
  desiredLength = 500; //in scaled units
  leftClick=16;
  getPixelSize(unit, pixelWidth, pixelHeight);
  getCursorLoc(x0, y0, z, flags);
  while (true) {
    getCursorLoc(x, y, z, flags);
    if (flags&leftClick==0) exit();
    if (x==x0 && y==y0) {
      wait(10);
    } else {
      dx = (x - x0) * pixelWidth;
      dy = (y - y0) * pixelHeight;
      length = sqrt(dx*dx + dy*dy);  //in scaled units
      enlageFactor = desiredLength/length;
      x = x0 + (x - x0)*enlageFactor;
      y = y0 + (y - y0)*enlageFactor;
      makeLine(x0, y0, x, y);
    }
  }
}


If you want to have also lines shorter than 500, don't recalculate x, y if 'enlargeFactor' is more than 1.

Michael
________________________________________________________________
On Oct 2, 2015, at 18:21, AlvinH wrote:

> 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
> <http://4.bp.blogspot.com/-Zo65_84L9Uc/UyCmC_uw0xI/AAAAAAAACCs/oxKFQtFl0hY/s1600/red-eye-frog-hd-photography-wallpapers78.jpg>  
> 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
> <http://imagej.1557.x6.nabble.com/how-to-stop-the-freehand-lines-tool-at-a-set-distance-20-um-td3690197.html>
> )
>
>   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!
>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Straight-line-with-preset-length-macro-tp5014526p5014530.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