How to combine different lines to get an area measurement using ImageJ/Fiji?


I’m trying to draw and combine three different lines that intersect to create a triangle and then measure the area within that triangle. I’ve tried to combine the ROI’s of these three lines into one but it still won’t give me an area measurement within the triangle.

The image below shows exactly what I want to do, I want to be able to draw one freehand line with two straight lines that all intersect to create a triangle and then measure the area within that triangle.


Does anyone have any solutions?

Just to clarify for anyone else that might want to help out, do you want the result to include a freehand line or be a triangle? Those are not the same thing, and the triangle would be easier since you could use the three intersection points.

Yeah sorry when I say triangle I mean the two straight lines and the one freehand line will form an approximate triangle so yes I need to include the freehand line.

Hi Daniel,
The macro below is a bit rough and ready, but it should do what you want. It draws the line on a new canvas, uses analyze particles to fill the centre, opens the binary image to remove the line ends and creates a new selection that is added to the ROI with the area enclosed by the lines.
As it stands it will use the first 3 ROIs in the ROI manager to define the area.
There is probably a more elegant solution to the problem, but it seems to work.
Hope it helps,

getDimensions(width, height, channels, slices, frames);
newImage("Untitled", "8-bit black", 512, 512, 1);
run("Analyze Particles...", "  show=Masks display include");
run("Invert LUT");
run("Options...", "iterations=1 count=1 black do=Open");
run("Analyze Particles...", "  show=Nothing include add");
close("Mask of Untitled");


Thank you for the reply,

So I’m having some trouble with the macro code. What I’ve tried doing so far is opening the image I want to analyze and drawing the lines I need (one freehand and two straight) and then I’m using the macro code but it consistently gives me an undefined variable message for line 3. So I try deleting line 3 (“newImage(“Untitled”, “8-bit black”, 512, 512, 1);”) but then the new line 3 (“roiManager(“Deselect”);”) also gets an undefined variable error message. Do you think you could give me a step by step on how to utilize this code?


  1. Open the image to be analyzed, draw the three lines and create ROIs for each line
  2. Go to Plugins>Macros>Record, copy and paste the macro code and run it

The image below shows my exact goal to help you. I’m trying to define an area on the splaying edges of this tendon using an approximate triangle, count the # of nuclei within this area to measure the cell density. This is why I need to know the area this approximate triangle creates.


I edited the macro text from @Volko to make it code, which should help using it. Not actually sure how to use it, but you will have issues trying to copy and paste forum text if it is not formatted correctly.

@Volko and @Research_Associate,

The macro code works great now, exactly what I need.

Thank you so much for the help, appreciate it!

1 Like

Hi Daniel,
Not quite sure why you had that problem. Did you copy the macro code to the script editor and set the language to ‘IJ1 Macro’ before trying to run it?
Below I have annotated the code to make it clear what happens in each line. I also noted that in my rush yesterday, I overlooked to set the dimensions of the temporary image correctly (it was fixed to 512x512), which would cause some problems for images of different dimensions. This is now fixed in the updated code below.
You could put the whole code into an action tool to give you a button on the menu bar to run the code.
Glad it is working for you.

image=getImageID();    								//get image ID
getDimensions(width, height, channels, slices, frames); //get size of current image
newImage("Lines", "8-bit black", width, height, 1);  //create a blank image with same dimensions
roiManager("Deselect");                                 //make sure that nothing is selected in ROI manager
roiManager("Draw");                                    //draw white lines for ROIs on new blank image 
run("Analyze Particles...", "  show=Masks display include");   //use Analyze Particles to create mask of the lines with the area enclosed by lines filled
run("Invert LUT");										       //invert LUT to show lines as white on black background
run("Options...", "iterations=1 count=1 black do=Open");      //use binary open to remove overhanging ends of lines
run("Analyze Particles...", "  show=Nothing include add");    //use Analyze Particles to create new ROI of area enclosed by lines
close("Lines");											//close temporary 'Lines' image
close("Mask of Lines");								//close temporary 'Mask of Lines' image
								//return focus to original image