Drawing a line automatically between 2 points

line-roi
#1

Hey ImageJ experts,

I need a tool in ImageJ which simply connects 2 points - I determined before - to a line.
Is there something I could use?

I am a still beginner so the solution might be simple but unfortunately I have no idea to handel this with ImageJ.
Thank you! :slight_smile:

0 Likes

#2

Good day!

Consider this ImageJ macro call:

makeLine(x1, y1, x2, y2)
Creates a new straight line selection. The origin (0,0) is assumed to be the upper left corner of the image. Coordinates are in pixels. You can create segmented line selections by specifying more than two coordinate pairs, for example makeLine(25,34,44,19,69,30,71,56) . After creating the selection, use Roi.setStrokeWidth to set the width and Roi.setStrokeColor to set the color.

Draws a line selection according to start and end coordinates.

If you need something different, then please be more specific.

Regards

Herbie

1 Like

#3

Thank you so much for your quick answer, Herbie!

I actually don´t know how to use the “makeLine” Tool.
Before that tool I would also like to use “Roi.getCoordinates(xpoints, ypoints)”.

How can I get ImageJ to execute these commands?
(As I already wrote: I am a real beginner and not a native speaker concerning English so all these things are not that easy for me to understand… I “asked google” for an instruction but didn´t find help - I have to confess that I am not talented in IT things…)

0 Likes

#4

Use the script editor:

2 Likes

#5

Thank you! :slight_smile:
Unfortunately I can´t find the script editor…
File - New - … there are 5 Options but nothing with “Script”. Just “Text Window”.
If I press “[” nothing happens…

(Sorry, I feel really silly…)

0 Likes

#6

Oh, sorry, it seems you’re using ImageJ version 1.x, while I was assuming you’re using Fiji (which includes ImageJ2), the distribution recommended in the life sciences.

1 Like

#7

Sorry, I should have told you the version I use.
I can download Fiji if that is recommendable/ necessary! :slight_smile:
Maybe this evening or tomorrow… Thank you so far!

0 Likes

#8

Fiji is nice because it has a lot of additional features, but you can still use/write macros with ImageJ 1.x. The only difference is that you are basically working with a plain text file rather than a script writer. This means you won’t have things such as syntax highlighting and function suggestions. Ultimately, your script will be the same though.

1 Like

#9

You can, of course, use the makeLine(x1, y1, x2, y2) command in ImageJ by just going to Plugins>New>Macro.
Also useful is the Macro Recorder command: Plugins>Macros>Record…

0 Likes

#10

Thank you :slight_smile:

I already tried this way before asking here:
Plugins - New - Macro
Then I inserted “Roi.getCoordinates(xpoints, ypoints)” and “install/run Macro” but nothing happend. It was the same with the “MakeLine” Tool.
(Points are defined before and add to the ROI Manager.)

Do I have to use special kind of language to use these commands?

0 Likes

#11

Yes: https://imagej.nih.gov/ij/developer/macro/functions.html

0 Likes

#12

Thanks for the link but I already knew it, I need some kind of instruction how to use “makeLine(x1, y1, x2, y2)”.
If I copy that in the text window and try to run or install it as a new macro nothing happens. Can you please show me an example how/with which additional text I can use it?

0 Likes

#13

you have to get the x and y coordinates of the two points.

how?

0 Likes

#14

Have a look at this:
https://imagej.nih.gov/ij/macros/tools/LineMeasureTool.txt

1 Like

#15

Thank you :slight_smile: I am so sorry for all the silly questions because I am not familiar to ImageJ…

I choose two points with the Multi-Point-Tool and I need a line between the two points. These line shall be a scale.
So I hoped that it would be possible to use the MakeLine Tool.
If necessary I can add these points to the ROI Manager und get the coordinates with “Roi.getCoordinates(xpoints, ypoints)” - I don’t know if this is redundant.

Is there a page/tutorial with which I can learn how to write a macro?
Recording is much more easier for me…
Thanks for the example but I don’t know what is important in what sequence and so on. I have really no idea what “getCursorLoc(x, y, z, flags)” means and the brackets and so on… or why this is important.

0 Likes

#17

Hi @vioma91,

As a starting point, there are tutorials here:
https://imagej.nih.gov/ij/developer/index.html

And a list of the available macro functions is here:
https://imagej.nih.gov/ij/developer/macro/functions.html

On the macro function page, you’ll find a lot of example macros that show you how the functions can be used in practice.

I would read the tutorials so you can start to understand how functions and variables work in the macro language.

For a start, when you have this line in your macro:

Roi.getCoordinates(xpoints, ypoints)

you are saying: find out the position of the active ROI in the image, and store its x coordinates in an array called xpoints and its y coordinates in an array called ypoints.

The elements of the array are specified with brackets. xpoints[0] is the first element, and therefore is the x coordinate of the first point in your multipoint selection.

Now to make the line.

The command:

makeLine(x1, y1, x2, y2)

makes a line between point (x1, y1) and point (x2, y2).

So to make these commands work together, you substitute the coordinates you collected in the array:

makeLine(xpoints[0],ypoints[0],xpoints[1],ypoints[1]) 

Here is a working macro (tested in Fiji) that shows the commands in action:

// ImageJ macro demonstrating how to retrieve points from a multi-point selection 
// and make a line between them


// For demonstration -- delete the code between the **** later

// *************

// open a sample image
run("AuPbSn 40 (56K)");

// make an example selection
xpoints = newArray(40, 440);
ypoints = newArray(50, 270);
makeSelection("point", xpoints, ypoints);
  
// *************

// Keep the code below to get and use the coordinates from YOUR multipoint selection.
// First make your selection, then run the macro.

Roi.getCoordinates(xpoints, ypoints); // store point coordinates
makeLine(xpoints[0],ypoints[0],xpoints[1],ypoints[1]); // make line
run("Measure"); // measure the line

I highly recommend working through the tutorials referenced above, if this is still confusing!

Good luck!

1 Like

#18

If you need a line, why don’t you just draw a line and measure its length (e.g. pressing “M”) instead of getting 2 points and then create a line?

2 Likes

#19

Thank you so much for your help!
Unfortunately I have other projects that are more urgent at the moment and because of that I cannot move forward with this topic/problem here.
Maybe I find time for that in 1-2 weeks.
So if I don’t response prompt don’t be surprised. It is just because of a lack of time. I will still refer to every answer in detail if there is more time. Thanks to all of you!

0 Likes