Converting a Variable Type from Int to String

Hi all!

I just had a quick question as to whether there was a way to convert the following variable into a string? I am using the ImageJ Macro language.


autloc5 = “method=” + autometh5 + “radius=” + autorad5 + “parameter_1=” + autop1e + “parameter_2=” + autop2e + “white”;


The variables autometh5 and autorad5 and so on are user inputed numbers, and I was wondering if there was a way to convert the entire variable into a string?

Thanks so much!

Hey Joseph,

you mean something like this?

text = "" + number;

Let us know if this helps!

Best,
Robert

Hi Robert!

I just tried this, and unfortunately, it does not work in my case :frowning:

Hopefully the code below will provide some context for what I am trying to achieve:


function style1() {

	run("Smooth");
	run("Auto Local Threshold...", autloc3);
	run("Watershed");
	run("Analyze Particles...", anpart3);
}

autloc3 = "method=" + autometh3 + " radius=" + autorad3 + " parameter_1=" + autop1c + " parameter_2=" + autop2c + " white";

anpart3 = "size=" + anpartsize3 + "-Infinity clear summarize add";

In the code above, autometh5, autorad5, and so on are user inputted values, and I was hoping to adjust the function according to the user’s input. However, the entire variable must be a string type in order for it to work. For example, the following works perfectly when I hard code the function:


run("Smooth");
run("Auto Local Threshold...", "method=Bernsen radius=15 parameter_1=40 parameter_2=0 white");
run("Watershed");
run("Analyze Particles...", "size=10-Infinity clear summarize add");

If you look at the Auto Local Threshold line, you can see that the entire section after that is a string, so I am hoping to convert everything to a string in order to run this function, and for some reason adding "" does not work :frowning:

Again, I appreciate all the help and time!

1 Like

Is there an error message or something suggesting it doesn’t work?

In a way. When I use the hard coded version where the parameters have been set already, it runs smoothly, but when I use the user inputted version, even though I use the exact same numbers, it says that it cannot run the “watershed” function since the image needs to be converted to an 8-bit image. Very strange.

Hi Joseph,
Below are 3 versions of your code that apply it to the Blobs sample image starting with your hard-coded example, a version that passes individual variables to the Auto Local Threshold function, and a version that combines all parameters for the Auto Local Threshold function into a single string. All three versions produce the same result.
The ImageJ macro language is very forgiving regarding variable types and doesn’t really require explicit variable type declaration and conversion.
Hope this helps,
Volko

run ("Blobs");
imageID=getImageID();
//Hard-coded
run("Duplicate...", " ");
rename("Hard-coded");
run("Smooth");
run("Auto Local Threshold", "method=Bernsen radius=15 parameter_1=40 parameter_2=0 white");
run("Watershed");
run("Analyze Particles...", "size=10-Infinity clear summarize add");
//Individual variables
autMethod="Bernsen";
autRad=15;
autPar1=40;
autPar2=0;
selectImage(imageID);
run("Duplicate...", " ");
rename("Ind.Parameters");
run("Smooth");
run("Auto Local Threshold", "method="+autMethod+" Bernsen radius="+autRad+" parameter_1="+autPar1+" 40 parameter_2="+autPar2+" white");
run("Watershed");
run("Analyze Particles...", "size=10-Infinity clear summarize add");
//Single string
localThresholdParameters="method="+autMethod+" Bernsen radius="+autRad+" parameter_1="+autPar1+" 40 parameter_2="+autPar2+" white";
selectImage(imageID);
run("Duplicate...", " ");
rename("Single Parameter String");
run("Smooth");
run("Auto Local Threshold", localThresholdParameters);
run("Watershed");
run("Analyze Particles...", "size=10-Infinity clear summarize add");
1 Like

Hi Volko!

I tried both of your methods, and for some reason it still does not work :frowning:

I am getting this error:

image

When I hard code in the function, I do not get this error, but when I run the same exact function but with variables in place, I receive this error.

Hm, not sure what is happening. Have you tried just running the macro code I sent on its own? Does this work or does this also produce the error? If my simple code works on its own, but you get an error when you incorporate it into your code, it might be useful post a larger code chunk that can be run on its own and reproduces your error.
The Auto Local Threshold should only be working with 8-bit images - so, you should have an 8-bit image when you call the Watershed function in your code. I really can’t understand why it throws the error it gets to the Watershed function unless there is some other code between the two lines of code.
Sorry that I can’t be of any more help,
Volko

Your code works fine when I copy and paste it in to a new macro environment, however, when I implement it in, I still get the same issue. Very Strange. Do you think it may have something to do with the user input? Here is the portion that asks for the user’s input.


Dialog.addChoice("Auto Local Threshold Method: ", newArray("Bernson","Contrast","Mean","Median","MidGrey","Niblack","Otsu","Phansalkar","Sauvola"));
Dialog.addNumber("Auto Local Threshold Radius: ", 15);
Dialog.addNumber("	Auto Local Threshold Parameter 1: ", 40);
Dialog.addNumber("	Auto Local Threshold Parameter 2: ", 0);
Dialog.addNumber("Analyze Particles Size: ", 10);
		
anpartsize3 = Dialog.getNumber();
autometh3 = Dialog.getChoice();
autorad3 = Dialog.getNumber();
autop1c = Dialog.getNumber();
autop2c = Dialog.getNumber();

autloc3 = "method=" + autometh3 + "  radius=" + autorad3 + " parameter_1=" + autop1c + " parameter_2=" + autop2c + " white";
anpart3 = "size=" + anpartsize3 + "-Infinity clear summarize add";

May I also ask why you added the “bernsen” before the radius and the “40” before parameter 2?

Wow ok I just fonud out my issue. I spelled “Bernsen” incorrectly. I spelled it as “Bernson” with an “o” instead of an “e”. I am so sorry I should have seen this earlier. Thank you so much for your help!!

1 Like

Yes, I just noticed this when looking at your code. It is always the little things that drip us up.
That’s why it is generally best to create short complete macro codes that can be run to try to trouble shoot a problem.
Good luck,
Volko

1 Like