Drawing a line automatically between 2 points



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:


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.




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…)


Use the script editor:


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…)


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.


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!


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.


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…


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?


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


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?


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



Have a look at this:


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.


Good day,

if you search this Forum you can find a lot of helpful code and links.

Here is the ImageJ User Guide and here are the documents for macro coding:
These links have been posted a hundred times before …

Here is an ImageJ-macro that does what you want:

requires( "1.52k" );
waitForUser("Set start and end point.\nThen click OK.");
getSelectionCoordinates(x, y);
run("Select None");
makeLine(x[0], y[0], x[1], y[1]);
run("Add Selection...");
run("Select None");
run("Overlay Options...", "stroke=gray width=3 apply");

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

You may change the color and width of the line by changing the entries in the code line:

run("Overlay Options...", "stroke=white width=3 apply");

The line is drawn as an overlay, i.e. it can be hidden or deleted or burned to the image (flattened).



Measuring width in 'pixel rows' (new to ImageJ)