Generic Macro + 2 outputs

Hello everyone

I am trying to use Batch processing using standard imageJ but I am struggling with Macros

here is my macro

function action(input, output, filename) {
open(input + filename);
run("8-bit");
//setTool("zoom");
//setTool("line");
makeLine(2343, 1845, 2525, 1845);
run("Set Scale...", "known=1 unit=mm");
makeRectangle(0, 0, 2560, 1828);
run("Crop");
run("Bandpass Filter...", "filter_large=40 filter_small=1 suppress=None tolerance=50 autoscale saturate");
setAutoThreshold("Default");
//run("Threshold...");
setThreshold(0, 80);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=0.60-Infinity show=[Overlay Masks] display clear add");
saveAs("Results", "C:/Users/Talal/Desktop/resultsall/out/filename");
//setTool("rectangle");
makeRectangle(0, 3, 2560, 1825);
run("Duplicate...", " ");
saveAs("PNG", "C:/Users/Talal/Desktop/resultsall/out/filename");
}
input = "C:\Users\Talal\Desktop\resultsall\enter\";
output = C:\Users\Talal\Desktop\resultsall\out\";

list = getFileList(input);
for (i = 0; i < list.length; i++)
action(input, output, list[i]);
setBatchMode(false);

however, it does not work. when I delete the first 2 lines it works…however, the output has the same name
I have multiple images…my outputs is a txt sheet with the results of particle analysis and an image of the processed image

1 Like

Backslashes are special characters. On Windows, you’ll have to escape them in your paths by using double-backslashes:

input = "C:\\Users\\Talal\\Desktop\\resultsall\\enter\\";

or by using forward slashes (/) instead.

1 Like

Hi @Talal_M,

welcome to the forum. You may just have some issues with / and . I recommend using slashes / in order to denote paths correctly. Furthermore, the quotations marks were copy pasted from Word or Powerpoint? By correcting them, it may work.

Please give it a try:

function action(input, output, filename) {
	open(input + "/" + filename);
	run("8-bit");
	//setTool("zoom");
	//setTool("line");
	makeLine(2343, 1845, 2525, 1845);
	run("Set Scale...", "known=1 unit=mm");
	makeRectangle(0, 0, 2560, 1828);
	run("Crop");
	run("Bandpass Filter…", "filter_large=40 filter_small=1 suppress=None tolerance=50 autoscale saturate");
	setAutoThreshold("Default");
	//run("Threshold...");
	setThreshold(0, 80);
	setOption("BlackBackground", true);
	run("Convert to Mask");
	run("Analyze Particles...", "size=0.60-Infinity show=[Overlay Masks] display clear add");
	saveAs("Results", "C:/Users/Talal/Desktop/resultsall/out/filename");
	//setTool("rectangle");
	makeRectangle(0, 3, 2560, 1825);
	run("Duplicate...", " ");
	saveAs("PNG", "C:/Users/Talal/Desktop/resultsall/out/filename");
}

input = "C:/Users/Talal/Desktop/resultsall/enter";
output = "C:/Users/Talal/Desktop/resultsall/out";

list = getFileList(input);
for (i = 0; i < list.length; i++) {
    action(input, output, list[i]);
}
setBatchMode(false);

Cheers,
Robert

I don’t see any issues with quotation marks in the original script code.

1 Like

It gives the error unrecognised bandpass filter

I did, before someone reformatted the post of Talal :wink:

Could you please try replacing the … with three dots … ? It appears like one character instead of 3

It is processing the images However, I am not getting the resultant area spread sheet in separate files. I wish to have the outputs by the original files name.

Was there a problem with the original code ? What did you do to modify it haeslein ?

For the record: the only edit I made was adding “code fences” (```) to the script code to format it (you can see the changes by clicking on the little pencil icon at the top right of the post).

For normal (i.e. not code-formatted) text, the Discourse forum automatically replaces normal quotes (") by typographical quotes (“”). Moderators and admins can edit the post to see what was the original text, and can add missing formatting to help new users getting it right. Formatting (and in particular syntax highlighting) allows to spot errors more easily, as you can see by the coloring of the last two lines in the original code.

1 Like

Should the spreadsheet output be done manually or can it be done through Batch ?

Try replacing the line

saveAs("Results", "C:/Users/Talal/Desktop/resultsall/out/filename");

with

saveAs("Results", output + "/" + filename + ".xls");

I hope that helps.

Cheers,
Robert

1 Like

Thank you. It is working. I don’t understand the “/”. Also, my images are not being processed. They should be masked

Hey @Talal_M,

take your time. If you don’t understand the “/”, try without it and observe carefully what happens in the output folder and the folder above. For this little experiment I recommend emptying the output folder before exeucting the macro.
Regarding the masking: Go through your code and try to understand every line: What is it doing? Why? We’re happy to help, but we cannot fully see through screenshots what’s happening. You need to find your way through yourself. Last but not least, I would recommend using Fiji instead of ImageJ. It can also execute ImageJ macros. In Fiji, use the menu File > New > Script to edit your code in the Script Editor. It comes with quite some features easing the coding such as an integrated help telling you what the commands do when you start typing their names:

I hope that helps!

Cheers,
Robert

2 Likes

I look through the code. I did use Fuji and was able to process the images through multiple image processing function. However, I was trying to get it to work through macros on imageJ. I want to be able to pull it off. Thank you so much for your help. Please don’t close this inquiry yet.

1 Like

It is working now. I just changed the last line to (“PNG”, output + “/” + filename + “.png”);
On the ImageJ website they did recommend making the general macro as the code above. What I don’t understand is when I am recording the macro, the line of code was made as such:
saveAs(“Results”, “C:/Users/Talal/Desktop/resultsall/out/filename”);

Well it records where you saved the file. If you want to save it in different locations in your script, it’s recommend to use variables for the folder specification - as you did in your code :wink: