Setting freehand lines without releasing mouse key

imagej

#1

Hey community,

is there a possibility to lock a freehand line just with pressing “t” without releasing the mouse key?

I have to measure the length of lots of lines with this tool and the ROI manager.
Two lines are always linked. So at the transition of this two lines I would just like to press “t” but not release the mouse key (or lift the pen - I will try it with a tablet next) at this area because I won’t find this exakt point again (and it is important to start with the measurement of the second line where the first line is terminated).

I hope you can help me! Thanks!


#2

No you cannot because if you press the mouse again on the end of the first line, then you select it, rather than start a new line.
But you can

  1. draw the line,
  2. release the mouse (do not move it),
  3. press t to store in the ROI manager,
  4. press ALT-SHIFT-A to remove the line you just draw and
  5. click again to start drawing the 2nd line.

#3

Isn’t it that you want to draw a segmented line and then treat each edge as if it were an individual line? Macro-wise it is relatively easy to pick up the coordinates of the vertices of the segmented line…


#4

I guess that is because it is trickier to get the lengths of the segments interactively.


#5

Stealing from this posting, I right-clicked the line tool making it segmented, then clicked once on the first bridge post, then the hand rail, then double-clicked the last post, then ran an adapted version of the length function, resulting in the Results window showing

58.0086, 325.1861

This is the recording:

run("Bridge (174K)");
///setTool("polyline");
makeLine(46,200,45,142,370,131);
l=selectionLength();
print(l);
function selectionLength() { 
lineLengthString="";
	getSelectionCoordinates(x, y); 
	segments = newArray(x.length-1); 
	lineLength =0; 
	for (i=1; i<x.length; i++) { 
		segments[i-1] =  sqrt( pow( (x[i-1] - x[i]),2) + pow( (y[i-1] - y[i]),2) ); 
		if(i>1){lineLengthString = lineLengthString + ", ";}
		lineLengthString = lineLengthString +segments[i-1]; 
      } 
     return lineLengthString; 
}

#6

Thank you for your efforts and answers!

I think I have to go more in detail to understand my problem…

Here is a short video, it’s not good but you can see what I am doing:

I need to get the length of the cress seedlings visible in this picture. I have 100-200 hundrets of such pictures a week, all of them show 16 seedlings.
So I need a quick tool to measure. The best solution was with imageJ, drawing the line along the seedling with the freehand tool and combine it with the ROI Manager. In future I will use it with a tablet and special pen so I can follow the seedlings curve much better, but won’t be really able to catch the same point twice (end of shoot/start of root). It’s just a test of the software now with the mouse and normal PC.

The length of shoot and root is needed separately - but if I have to click to end the line and start with a new line I can’t catch the same point and that would be important. Making it segmented etc. is too time-consuming.

I hoped that there would be a way to set in the line without releasing the mouse/ in future lift the pen (which is more difficult to catch the point again because the mouse still is at the same point after releasing it - but the pen is moved), for example just to press t…


#7

Apart from that you maybe can segment each sprout automatically (post an image and see what is possible?), I think if you first draw the freehand line, then have a macro that adds it to the roi manager as an interpolated line with interval 1 (which makes it a series of points), then for all lines in the image you select which point divides the sprout in the shoot and root, then the macro measures all lengths, you end up with what you want?


#8

Thank you! That sounds good and might be another option. Unfortunately I am not that familiar with ImageJ yet: do I have to write the macro by myself or is there one I can use?

I already tested round about 40 possible software solutions for an automatic analysis of the seedlings but none (even not IJ_Rhizo oder WINrhizo) was convincing and successful for various reasons…


#9

Good day,

please post at least one typical raw image in the original TIF- or PNG-format. No JPG-format though, because JPG introduces artifacts! You may also post images as Zip-archives.
(Converting a JPG-compressed image to TIFF- or PNG-format doesn’t make sense.)

How do you acquire the images?
(Illumination, Camera, Distance, Reproducibility, etc.)

Please keep in mind that the quality of the original images is of utmost importance. Optimum image acquisition is better than any post hoc image processing.

Regards

Herbie


#10

Hey Herbie :slight_smile:
Thank you! So far we just scanned the bags with the cress with a Canon scanner.
I only get JPG then. We try to find a solution with a cam and incident light as an alternative to get photos with a better quality and depth of focus now.
I work with this system only recently and I try to improve every step so it still is in progress. But unfortunately I can only show you an example like this at the moment.

(How can I upload something? I think the upload of my picture wasn’t successful; I used the upload button above the text box…)


#11

So far we just scanned the bags with the cress with a Canon scanner.

You put the specimen with the soil etc. onto a document scanner?

I only get JPG then.

Cannon scanners can deliver a lot of formats, e.g. PNG and TIFF. We prefer the latter.

How can I upload something?

Maybe as a new user of this forum there are certain restrictions (I’m unable to change them). You may perhaps use a dropbox-like way of posting …

HTH

Herbie


#12

I used a free host now to upload the picture: https://ibb.co/hrRDJp
As you can see we don’t use soil, just a little bit water and chromatography paper :slight_smile:

For scanning we can choos JPEG in color and b/w and as an alternative TIFF, but only in b/w. And I definitely need the color to distinguish between root and shoot.


#13

Thanks for the image!

The image is not that bad but there is a strong shine through due to the wet chromatography paper.
You really should use a homogeneous dark background.
Furthermore, please avoid any overlapping of the specimens!

Now please tell us what parts you are interested in by drawing onto this image.
Please specify exactly what you need to be automatically detected or measured. Please be aware of the fact that in the end we need a formal (mathematical) definition because this is what computers understand.

Regards

Herbie

PS:
Closer inspection of the sample image shows that the wet specimens are enclosed in a plastic bag which leads to disturbing structures in the image. Image acqusiition should be revised and better contrast between the stalks and the background should be reached.


#14

Provided you don’t mind drawing the lines manually but just need a tool that chains several freehand segments after each other (and adds each of the segments to the ROI Manager), you can write a custom tool macro to aid in your task.

Here’s an example Segmented Freehand Line Tool that adds the current freeline selection to the ROI manager whenever you press or release ⇧Shift, and starts a new freeline selection at the last mouse position. (When you release the mouse button, it adds the last selection to the manager and the ⇧Shift state is ignored.)

// Author: Jan Eglinger

macro "Segmented Freehand Line Tool - C008P00345878798aff0-Cf44D00D01D10D11D58D59D68D69DeeDefDfeDff" {
    var leftButton = 16;
    var shift = 1;
    var shiftChanged = 0;
    getCursorLoc(x, y, z, flags);
    xArr = newArray(1); xArr[0] = x;
    yArr = newArray(1); yArr[0] = y;

    // drag mode
    while (true) {
        getCursorLoc(x, y, z, flags);
        if (flags&leftButton==0) {
            // button released - add last selection
            roiManager("Add");
            return;
        }
        xArr = Array.concat(xArr,x);
        yArr = Array.concat(yArr,y);
        makeSelection("freeline", xArr, yArr);
        if (flags&shift!=shiftChanged) {
            // shift key changed - add selection
            roiManager("Add");
            xArr = newArray(1); xArr[0] = x;
            yArr = newArray(1); yArr[0] = y;
            shiftChanged = flags&shift;
        }
        wait(10);
    }
}

To install the macro tool, paste the code into a Plugins > New > Text Window… and choose Macros > Install Macros Ctrl-I.

Here’s what you can get:

image

You should then be able to measure all ROIs simply by clicking Measure on the ROI Manager window.


#15

Thank you all for you efforts and answers!
I am really sorry for my late answer now but I was very busy the last two weeks and I couldn’t concentrate on that topic because other problems had to be solved more urgent.

I tried a lot to improve the background of the pictures but there is no option and (yes, Herbie, it is right that the cress is still in bags while scanning :slight_smile: ) the seedlings need to stay in the bag.

@imagejan
Thank you for showing me this macro solution! It is the solution I am looking for!
Using your macro the line is locked only with releasing the mouse key/pen or with pressing t (but then I have to release the mouse/pen in the next step to lock the line - but I am doing so anyway to end it so it is perfect to me).
Then after “Measure” I get the length of the first part of the line (line 1) and the whole length of both parts (line 1 and line 2) as line 2. But that’s no problem because I can subtract the values to get the single length of line 2.
(If I press shift the line isn’t locked reliably… Pressing t is much better!)

But unfortunately the Surface Pro Tablet - that I can test at the moment - only realizes the line that is drawn with a pen after a short distance - means with a delay. So it doesn’t start at the point I actually began to draw the line with the pen…
Do you have an explanation/solution for that?
Without this problem the macro would be the absolutely perfect solution for me - thank you sooo much!

(I am really sorry, I am such a beginner :see_no_evil:
Thanks to all of you for your assistence!)


#16

Unfortunately I wasn’t able to solve the problem yet:
If I draw the line with this macro with the mouse everything is perfect and the line starts where it should be (even using the tablet). But if I use the pen for drawing - and that’s what I want - the line is drawn with a little delay every time. I tried various changes in the macro but I actually don’t have an idea what I need to change to solve the problem. Or if the final result is that the macro doesn’t work with the pen.
The normal freehand line works with the pen, the delay is only using the macro (which I need for adding a line to the ROI manager without removing the pen).
I would be very very very glad and thanful if someone has an idea how to solve this problem! :slight_smile:


#17

Unfortunately that’s a bit hard for me to reproduce, without a pen screen…

Can you be more specific about the delay? When exactly does it occur? When starting the line, when pressing ⇧Shift, when releasing it?


#18

I was trying to understand @imagejan ´s macro but I don´t get it completely … maybe Jan or someone else can shed some light on the code.
For now I have two questions:

what is the condition that is checked here?

what is returned and which line receives this returned value?


#19

Looping would never stop, if there weren’t that “return” to escape if the if-condition is fullfilled.

Regards

Herbie


#20

Thanks for your answers! I made a video that might help you to understand my problem: https://youtu.be/BQtY0D7W3KQ

I draw a line with the pen using the macro and the line starts a little bit after drawing (sec 8 in the video). Clicking the button on the pen that stands for the right mouse button the delay is shorter but still there (sec 31 in the video).
With the original freehand tool I don’t have this problem with the pen (first yellow line that is already there at the beginning of the video, it disappears later).
Using the mouse with the macro function and the tablet I don’t have the problem either (also shown in video, sec 44).

It is independent from pressing shift or t to add a line to the ROI manager which works perfect! :slight_smile: