Modify behaviour of line tool in ImageJ / FiJi

Hey guys,

for a specfic use-cas in my image analysis routine it would be nice to be able to modify the line-tool in imagej / fiji.

I need to do alot of line measurements perpendicular to a certain structure. Therefore it would be nice to have a perpendicular baseline in the line measurement tool as orientation.

ee-1.tif (768.2 KB)

I’d like to modify the line selection tool as in this sample image. Selection 1 is the normal behaviour andthe desired behaviour is the combination of 2,3 and 4 (while only measuring selection 2). 3 and 4 should be perpendicular to 2 for orientation.

Is this achieveable with a macro?

@cripcate

It’s a little of a workaround… but then you have to draw three, separate lines - just keeping track of their lengths/positions relative to one another.


makeLine(392, 145, 392, 362);
roiManager("Add");
makeLine(377, 145, 406, 145);
roiManager("Add");
makeLine(377, 362, 406, 362);
roiManager("Add");

This draws such lines on your sample image… just make sure the following numbers match:


makeLine(num1, num2, num1, num3);
roiManager("Add");
makeLine(num4, num2, num5, num2);
roiManager("Add");
makeLine(num4, num3, num5, num3);
roiManager("Add");

Make sure the length is ‘even’ between num4 and num5 relative to num1 (which is the center point). Does that make sense?

1 Like

It does make sense. Is there a way of telling imagej to execute this macro everytime i draw a line so it does it automatically?

I dont really need the the perpendicular lines measurement-wise, just as an orientation.
The reason I want to have this feature is this:

pano_blended_fused-1.tif (2.7 MB)

I want to measure lines 3 and 6 in this image, which shoul dbe roughly perpendicular to 1 and2 or 4 and 5 respectively. The background is measuring the width of a growth interval in a bivalve. the faint lines you can see in a blueish color are growth lines.

Maybe there’s even an easier way to achieve what i want.

You can write you own macro tool doing this. Have a look at the parallel lines tool suggested here as a starting point:

1 Like

@cripcate
It’s going to be a bit more complicated if you want it to be interactive… so you’ll have to brush up on scripting a bit. Here are some helpful links:

But to get you started… you’ll need code that looks like this:

// ask user to draw lines and add them to ROI Manager
waitForUser("Draw your lines using the line tool and press 't' to add them to the ROI Manager.\nIf you want to delete one, select it in the ROI Manager and delete it.\nContinue drawing and adding new lines until you are happy with them all...\nClick 'OK' when finished.");
roiManager("Show All");

// count the lines
count=roiManager("count"); 

// first, rename all main lines in ROI Manager
if (count == 0) {
	showMessage("You have not drawn lines.")
} else {
	for(i = 0; i < count; i++) { 
		roiManager("Select", i);
		roiManager("Rename", "line" + i);
	}
}

// now add the 'end/perpendicular' lines for each main line
for(i = 0; i < count; i++) { 
	roiManager("Select", i);
	name = Roi.getName;
	correctName = "line"+i;
	if (name == correctName) {
		getLine(x1, y1, x2, y2, lineWidth);
                // you need to calculate the angle here to use it in maths later to set next lines... like angle = atan((x2-x1)/(y2-y1)); or something
		makeLine(x1-20, y1, x1+20, y1);
		roiManager("Add");
		makeLine(x2-20, y2, x2+20, y2);
		roiManager("Add");
		count=roiManager("count"); 
	}	
}

I don’t have the maths right yet to set the perpendicular lines (90-degrees to the drawn line) correctly… but figured you can finish that part yourself. I don’t have more time to work on this at the moment… but this should give you a solid start!

Hey all, thanks for the input. It helped tremendously. I adapted your macro tool @imagejan and am quite happy with it.
The only thing I would wish for: Is it possible that the perdendicular lines are drawn in real time while dragging the “Main Line”?
I can do a bit of scripting but this is out of my capabilities. Here’s the macro tool script so far:

macro "Perpendicular Line Tool - C000-B11-L0440-L3399-L8cc8" {
    var leftButton = 16;
    getCursorLoc(xStart, yStart, zStart, flagStart);
    if (flagStart&leftButton==0) {
        print("Not the left button. Stopping...");
        return;
    }
    // drag mode
    while (true) {
        getCursorLoc(x, y, z, flags);
        if (flags&leftButton==0) {
            // if single point, then return immediately
            if(xStart == x && yStart == y) {
                return;
            }
            // go to parallel mode
            // starting from line xStart, yStart, x, y
            drawPerpendicularLines(xStart, yStart, x, y);
            Overlay.clear();
            return;
        }
        makeLine(xStart, yStart, x, y);
        wait(10);
    }
    makeOval(x-radius, y-radius, radius*2, radius*2);
}

function drawPerpendicularLines(x1, y1, x2, y2) {
    makeLine(x1, y1, x2, y2);
    while (true) {
        getCursorLoc(x, y, z, modifiers);
        if (modifiers&leftButton!=0) {
            return;
        }
        // get slope of line
        dx = (x2-x1);
        dy = (y2-y1);

        // calculate perp line endpoints
        xa = (x1 - dy/4); ya = (y1 + dx/4);
        xb = (x1 + dy/4); yb = (y1 - dx/4);

        xc = (x2 + dy/4); yc = (y2 - dx/4);
       	xd = (x2 - dy/4); yd = (y2 + dx/4);

	    Overlay.drawLine(xa, ya, xb, yb);
	    Overlay.drawLine(xc, yc, xd, yd);
	    
        Overlay.show;
        wait(10);
    }
}

EDIT: Another two bugs I found so far:

  1. You have to click on the image once before you can start another line, to erase the previous one, which I would like to prevent.
  2. You can’t adapt the line after drawing it, only by switching to the normal line tool. And if you move the line, it erases the perpendicular overlay lines.

I don’t know enough java or imagej macro to fix this myself :frowning: