Grid/Collection stitching implementation in macros

Dear users of the community,
I’ve been trying to implement the Grid/Collection stitching plugin in a macro.
By learning from previous posts I’ve already corrected or found solution to different issues, but now I’m facing an error I’ve not found a solution for (yet!).

I’ve written a macro to first create a TileConfig file (which is being correctly created in the right folder, with the right settings) and then call the stitching plugin.
The problem is that if I manually call the plugin, no problems. If instead I call the plugin and pass the options via the macro, I receive ultimately this error:
"ERROR: Found: 0 tiles, but at least 2 are required for stitching. Aborting."
Despite both the images and the config file are there. And indeed, manually calling the plugin is flawless.
Here’s the code:

 var directory = getInfo("image.directory"); 
 var dirstitch = "["+replace(directory, "\\", "/")+"]";
 var org_name = getTitle; 
 var f_name = substring( org_name, indexOf(org_name, "E"), lengthOf( org_name )-7 ); 
 var file = File.open(directory+f_name+".txt");
 var tilefile = "["+f_name+".txt]";
 print(file, "dim=2"+"\n "+"\n ");
 print(file, f_name+"_01.png; ; (0, 0)"+"\n ");
 print(file, f_name+"_02.png; ; (1536, 0)"+"\n ");
 print(file, f_name+"_05.png; ; (0, 1152)"+"\n ");
 print(file, f_name+"_06.png; ; (1536, 1152)"+"\n ");
 print(file, f_name+"_04.png; ; (0, 2304)"+"\n ");
 print(file, f_name+"_03.png; ; (1536, 2304)"+"\n ");
 print(dirstitch);
 print(tilefile);
 String.copy(f_name);
 run("Grid/Collection stitching", "type=[Positions from file] order=[Defined by TileConfiguration] directory=" + dirstitch + " layout_file=" + tilefile + " fusion_method=[Linear Blending] regression_threshold=0.90 max/avg_displacement_threshold=4 absolute_displacement_threshold=5 compute_overlap computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");

Has anyone encountered a similar problem, before?
Thanks for any advice that might come!

Federico

Hi @Federico_La_Manna,

Did you ever find a solution for your problem? I am having a similar issue with the Grid/Collection Stitching plugin.

I’m writing a macro to batch stitch files, but whenever I plug in variables instead of specific file names, I get an error where it tries to find the file that is called “variable” instead of the variable’s actual value, which is the actual file name. I found another fix for a similar issue which is to take all of your parameters and move them into a different defined variable outside of the run(“Grid/Collection stitching”, …), and then call that string when running the plugin. But now I get a different error where it can’t find ‘C:/TileConfiguration.txt’ – and I don’t even know where to start with that. Am I missing a built in plugin? Here is the code I’m working with right now:

folder1 = getDirectory("Choose Input Folder");
outputfolder = getDirectory("Choose Output Folder");
list = getFileList(folder1)
for (i = 0; i < list.length; i++) {
    showProgress(i+1, list.length);
    open(folder1 + list[i]);
    imageName= getTitle();
    imagepath = getInfo("image.directory");
    imagepath2 = getInfo("image.filename");
    print(imagepath);
    variable = (imagepath + "File.separator" + imagepath2);
    paramsStr = ("type=[Positions from file] order=[Defined by image metadata] browse=&folder1 multi_series_file=&variable fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 add_tiles_as_rois compute_overlap increase_overlap=0 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
    print(paramsStr);
    run("Grid/Collection stitching", paramsStr);
    selectWindow("Fused");
    saveAs("Results", outputfolder + imageName + "fused.tif");
    run("Close");
    close();
}

Thanks for any help you might have!

Hi, I figured out my issue, and I wanted to put it in this thread for posterity. The solution I figured out is this:

You need to flip the slash directions.
The string returned from get.Directory("") produces a path that contains back slashes, like this:

C:\Users\name\Documents\folder\file.lsm

When you call up a file using the Grid/Collection stitching plugin, however, it uses forward slashes, like this:

C:/Users/name/Documents/folder/file.lsm

And unfortunately, if you try to plug in a path from getDirectory into run(“Grid/Collection stitching”, …) the plugin won’t recognize the path and it’ll error out.

The simple solution is to replace all of the back slashes in your path with forward slashes, and the macro works! Here is a paired down version of what I found works to accomplish the replacing:

open("");
	
	imagepath = File.directory();
	imgpathdash = replace(imagepath, "\\", "\/");
	imagename = getTitle();
	imgnamedash = replace(imagename, "\\", "\/");
	img = imgpathdash + imgnamedash; 

	print(img);
	run("Grid/Collection stitching", "type=[Positions from file] order=[Defined by image metadata] browse=&img multi_series_file=&img fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 add_tiles_as_rois compute_overlap increase_overlap=0 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
	run("Close");

Here is the full code I eventually got to work to batch stitch a folder of lsm files:

#@ File (label="Input Folder", Style="Directory") folder
#@ File (label="Output Folder", Style="Directory") output
#@ String (label="Type", choices={"Grid:row-by-row","Grid:column-by-column","Grid:snake by rows","Grid: snake by columns","Filename defined position","Unknown position","Positions from file","Sequential Images"}, value="Positions from file") ChosenType
#@ String (label="Order", choices={"Defined  by image metadata","Defined by TileConfiguration","Left & Down","Right & Down","Right & Up","Left & Up"}, value="Defined by image metadata") ChosenOrder
#@ String (label="Output file type", description="What format the results will be saved in", value=".tif") TypeOutput
#@ String (label="What file modifier needs to be removed?", description="Like the original file type", value=".lsm") rmvit
#@ String (label="What should append the file name?", description="How you want to edit the file name", value="fused") NameEnd

setBatchMode(true);

stitchem(folder);

function stitchem(variable) {
list = getFileList(folder);
for (k=0; k<list.length; k++) {
	showProgress(k+1, list.length);
	open(folder + "\\" + list[k]);
	
	imagepath = File.directory();
	imgpathdash = replace(imagepath, "\\", "\/");
	imagename = getTitle();
	imgnamedash = replace(imagename, "\\", "\/");
	img = imgpathdash + imgnamedash; 

	print(img);
	run("Grid/Collection stitching", "type=[Positions from file] order=[Defined by image metadata] browse=&img multi_series_file=&img fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 add_tiles_as_rois compute_overlap increase_overlap=0 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
	run("Close");
	
	imageNamermvit = replace(imagename, rmvit, "");
	outName = imageNamermvit + NameEnd + TypeOutput; 
	
	saveAs("Tiff", output + "\\" + outName);
	close();	
}
}

Dialog.create("Done!")
Dialog.addMessage("All the .lsm images have been successfully stitched!")
Dialog.show()

Hopefully this helps someone!

Sidenote, I also was able to finally get script parameters to work for selecting the input and output folders. Previously, I had to use getDirectory because if I used script parameters, my macro would give an error that it couldn’t find the folder. Turns out that when using script parameters, you have to insert a slash after that variable whenever you use it to call a path – which is something you don’t have to do with getDirectory.

For example, with getDirectory my later function would look like this:

open(folder + list[k]);

But with script parameters, the later function looks like this:

open(folder + "\\" + list[k]);

-Shannon

2 Likes