HELP- How to set scale and get Length of line

java

#1

Hello,

I am a new developer of ImageJ and i would like to open an image and to be able to set an scale and get length of objects from the image.

I am able to do all this in ImageJ, but i cannot from Java. I have this code:

ImageJ imageJApplication = new ImageJ(2);
ij.io.Opener opener = new ij.io.Opener();
this.imp = opener.openImage(imageFilePath);
IJ.setTool(Toolbar.LINE);

With this code i open an image an i can paint lines.

Now, i have this code to set the scale, but nothing happens in the form:
IJ.run(“Set Scale…”, “distance=29.0689 known=5 pixel=1 unit=cm global”);

I also have this code to paint the scale in the image, but nothing happens:
IJ.run(“Scale Bar…”, “width=17 height=4 font=14 color=White background=None location=[At Selection] bold”);

How could i set the scale and get the lengh of the new Lines?

Another question, Can i see the StausBar or the main menu from code? I only see the image…

Please, can you help me?

Thanks in advance…


Setting the scaleBar With JAVA
#2

Good day,

if you start with coding in Java for ImageJ, I recommend to use the macro recorder with option “Java” first.

This is a code example related to your question:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {
	public void run(String arg) {
		ImagePlus imp = IJ.createImage("Untitled", "8-bit ramp", 512, 512, 1);
		IJ.run(imp, "Set Scale...", "known=5 pixel=1 unit=cm");
		IJ.run(imp, "Calibration Bar...", "location=[At Selection] fill=White label=Black number=5 decimal=0 font=14 zoom=1 overlay");
		imp.show();
	}
}

If you wish to code ImageJ-plugins the traditional way, then please study:
https://imagingbook.files.wordpress.com/2013/06/tutorial171.pdf

Always consider coding ImageJ-macros which is much easier!

Regards

Herbie


#3

Thank you for your answer!

I have tested your code and it works:

IJ.run(imp, “Set Scale…”, “distance=29.0689 known=5 pixel=1 unit=cm global”);
imp.show();

But if i write in different order it doesn´t work:

IJ.run(imp, “Set Scale…”, “distance=29.0689 known=5 pixel=1 unit=cm global”);
imp.show();

I need to show the image an after a user action set the scale, do you know the problem?

Thanks in advance…


#4

I wold like to explain my example a litle bit more…

I have recorded a macro from ImageJ in order to do:

Open an existing image
Set the Line Tool
Draw a Line
Set the scale knowing 5 cm
Draw another line
Get the lenght of the new Line

All is correct, but when i test the code (copy and paste) in my own application, i open the image, but nothing else happens:
This is the code recorded:

IJ.open(imageFilePath);
IJ.setTool(Toolbar.LINE);
IJ.makeLine(126, 376, 293, 369);
IJ.run(“Set Scale…”, “known=5 unit=cm”);
IJ.makeLine(153, 214, 453, 546);
IJ.run(“Measure”);

Why the commandBar (the menu) is not visible when i open the image from JAVA?

Can you help me, please?

I read the tutorial, but it didn´t help me…


#5

Sorry but your post lacks details.

I understand that you need to allow user action in your code, so you need to put something like

waitForUser(title, message)
This is a two argument version of waitForUser , where title is the dialog box title and message is the text displayed in the dialog.

(this is a macro function taken from here: https://imagej.nih.gov/ij/developer/macro/functions.html)

to interrupt the execution of your code.

Please try to code an ImageJ-macro first.

Regards

Herbie


#6

Sorry, I think I’ve mixed up two topics. The main question is why this code doesn’t work(only open the image and set the tool):

IJ.open(imageFilePath);
IJ.setTool(Toolbar.LINE);
IJ.makeLine(126, 376, 293, 369);
IJ.run(“Set Scale…”, “known=5 unit=cm”);
IJ.makeLine(153, 214, 453, 546);
IJ.run(“Measure”);

Thanks in advance.


#7

Well,

if you allow me to stay with a macro for the time being, this ImageJ macro works as expected:

run("Set Measurements...", "  redirect=None decimal=3");
newImage("Untitled", "8-bit ramp", 768, 768, 1);
makeLine(126, 376, 293, 369);
run("Set Scale...", "known=5 unit=cm");
makeLine(153, 214, 453, 546);
run("Measure");

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

This is the result (length in cm) I get:
results

HTH

Herbie


#8

It works, I think that i have any error in java code. Can you see any error?

IJ.open(imageFilePath);
IJ.setTool(Toolbar.LINE);
IJ.makeLine(126, 376, 293, 369);
IJ.run(“Set Scale…”, “known=5 unit=cm”);
IJ.makeLine(153, 214, 453, 546);
IJ.run(“Measure”);

Other error: the forms from java doesn´t have buttons and the backColor is White…

Thanks, again…

David.


#9

Good day David,

it appears that you are not that fluent in ImageJ-realted Java coding. Therefore, I highly recommend that you stay with ImageJ macro coding instead which is much easier to accomplish.

The code you’ve presented is not valid ImageJ Java code for several reasons …

Just for the record, here is the Java code the recorder delivers for an ImageJ-plugin:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {
	public void run(String arg) {
		ImagePlus imp = IJ.createImage("Untitled", "8-bit ramp", 512, 512, 1);
		imp.setRoi(new Line(126, 376, 293, 369));
		IJ.run(imp, "Set Scale...", "known=5 unit=cm");
		imp.setRoi(new Line(153, 214, 453, 546));
		IJ.run(imp, "Measure", "");
		imp.show();
	}
}

Of course you must first compile it and if you run it, it gives the same result as the macro I’ve posted before.

Regards

Herbie


#10

Yes, it works!! i had a problem because i didn´t use the imp.show() method…

Last question (sorry…) It´s possible to set a method after imp.show()?

For example, if i write.

  • IJ.run(imp, “Scale Bar…”, “width=56 height=4 font=14 color=White background=None location=[Lower Right] bold”);
  • imp.show();

It works, but if i write_

  • imp.show();
  • IJ.run(imp, “Scale Bar…”, “width=56 height=4 font=14 color=White background=None location=[Lower Right] bold”);

The scaleBar not showing(). (I need to do it on this way because the user has to open the image and later he has some buttons on my own form to set changes on the image…) Do i have to refresh or update imp? I have used updateAndRepaintWindow with no results…

Thanks again…

David


#11

David,

as I’ve written before:

How do you stop the execution of the plugin for the user intervention?

Clueless about your coding

Herbie

PS:

i had a problem because i didn´t use the imp.show() method…

Aha.
I think there were some more problems — no?


#12

Ok… i mean… I don´t know if i can do all that i want with ImageJ (Java Code):

The user clicks on different buttons of my form in order to open and to size a image… Example:

1-Click() ==> I open the image:

        imp = IJ.openImage(imageFilePath);
        IJ.setTool(Toolbar.LINE);          
        imp.show();

2-Click() ==> the user draws a line (on ImageJ app) and i set the scale

   new ij.gui.WaitForUserDialog("Click OK when finish").show();
   IJ.run(imp, "Set Scale...", "distance=29.0689 known=5 pixel=1 unit=cm global");

3-Click() ==>the user draws another line in order to size it:

 (draw the line)
 new ij.gui.WaitForUserDialog("Click OK when finish").show();

4-Click() ==> i set the scaleBar in order to see the lengh in the image

       new ij.gui.WaitForUserDialog("Click OK").show();
       IJ.run(imp, "Scale Bar...", "width=56 height=4 font=14 color=White background=None location=[Lower            Right] bold");

The problem is in the last click, the scaleBar is not showing… do i need to call a refresh() event or similar…?

Yes, you are right… I had several errors!!!

Thanks…


#13

So we are slowly approaching a concept of what you like to obtain.

I don´t know if i can do all that i want with ImageJ (Java Code)

You should be aware that ImageJ and general Java-code may not be the same.

If you like to code an ImageJ-plugin the easy way, then there exist some restrictions. What is easy to obtain is described in the tutorial that I’ve previously posted. With this approach you can conveniently set-up a dialog for parameter entry similar to that provided for ImageJ-macros. However, if you need more refined custom dialogs, you need to handle the events yourself and things get more involved, i.e. you need to be more fluent in Java and the problems you may possibly encounter don’t fit to this Forum anymore.

The most general apporach is that you construct a Java-appplication and only use the ImageJ API as a library for certain parts. The result will then be a stand-alone application that gives you all the freedom in look and function, but it may require a lot of work. Of course, the result is no longer an ImageJ-plugin (that is bound to ImageJ), but an application that is fully independent from ImageJ, i.e. stand-alone.

So you must decide which apporach you want.
If you’ll code an ImageJ-plugin the easy way, I see problems with:

The user clicks on different buttons of my form

You may have a look at the Generic Dialog API of ImageJ that serves parameter entry. It doesn’t require that you care about events yourself.

What I don’t understand in your sketch is:

3-Click() ==> the user draws another line in order to size it:
4-Click() ==> i set the scaleBar in order to see the length in the image

What do you mean with “size a line”?
What do you mean by “see the lengh in the image”?

The problem is in the last click, the scaleBar is not showing

What about using

imp.show();

again?
(Works for me.)

Regards

Herbie


#14

Thank you Herbie!

Its possible to run a macro or set the scale after show event? I have tried it, but nothing happens…

I need to show the image, and later (with the image visible) to set the scale (by the user) and show the scalebar. I dont understand why it doesn’t work…

Any idea?

Thanks!


#15

You don’t really refer to my posts.

What about using

imp.show();

again?
(Works for me.)

Believe me it works!
If it doesn’t, you may have other problems with your code.
Does your Java code compile without problems?

Its possible to run a macro or set the scale after show event?

What macro?
What event?

You terms appear highly confusing to me.

Herbie


#16

Hello again,

It Works showing the scaleBar:
ImagePlus imp = IJ.createImage(“Untitled”, “8-bit ramp”, 512, 512, 1);
imp.setRoi(new Line(126, 376, 293, 369));
IJ.run(imp, “Set Scale…”, “known=5 unit=cm”);
imp.setRoi(new Line(153, 214, 453, 546));
IJ.run(imp, “Scale Bar…”, “width=9 height=4 font=14 color=White background=None location=[At Selection] bold”);
imp.show();

But it doesn´t show the scaleBar:
ImagePlus imp = IJ.createImage(“Untitled”, “8-bit ramp”, 512, 512, 1);
imp.setRoi(new Line(126, 376, 293, 369));
IJ.run(imp, “Set Scale…”, “known=5 unit=cm”);
imp.setRoi(new Line(153, 214, 453, 546));
imp.show();
IJ.run(imp, “Scale Bar…”, “width=9 height=4 font=14 color=White background=None location=[At Selection] bold”);
imp.show();

Ahhhh!! I don´t understand!!!

Please, can you help me?


#17

This ImageJ-plugin code works for me:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {
	public void run(String arg) {
		ImagePlus imp = IJ.createImage("Untitled", "8-bit ramp", 512, 512, 1);
		imp.setRoi(new Line(126, 376, 293, 369));
		IJ.run(imp, "Set Scale...", "known=5 unit=cm");
		imp.show();
		new ij.gui.WaitForUserDialog("Draw a new line!").show();
		IJ.run(imp, "Scale Bar...", "width=5 height=4 font=14 color=White background=None location=[Lower Right] bold");
		imp.show();
	}
}

When I run the plugin “My_Plugin.class”, I get:

Then I draw a new line by hand and click OK and I get:

I fear you expect something completely different, but this is what the code does …

Regards

Herbie


#18

When i test your code, the scalebar is not showing… only the image. And the default tool is not the line

Thank you again…


#19

Here is the code again (the second “show” isn’t actually required) and the compiled plugin as a ZIP-archive:
Archiv.zip (1.6 KB)

Un-zip the archive and install the ImageJ-plugin.

If it still doesn’t work for you, then please explain in detail what you are doing!

Make sure you run the currect version of ImageJ which is 1.52h.

Regards

Herbie

PS:
I didn’t set the tool in my code. You may change it to your liking however.
Why do you post images taken with a smart-phone camera instead of posting screen shots?


#20

I am using .NET framework, so i cannot install the plugin. I have recompiled IJ.jar to .NET

I am running directly the code from a windows form (in c#) using all the lybreries because y want to develop a new application with some buttons to open a image, draw a line, set the scale…

All is working, except this line:

IJ.run(imp, “Scale Bar…”, “width=5 height=4 font=14 color=White background=None location=[Lower Right] bold”);

Nothing happens…