Finding a focus slice followed by stitching tiles

Hi,
I am a novice in image analysis on Fiji with no experience in scripting (macros).
I use deltavision systems to image the whole slide. My samples are frozen tissue sections (10um thick) and I stain with 2-3 fluorescent markers and DAPI (for nuclei). My image settings are below;
Channels : 3-4
Lens: 20x oil
Image size: 1280x1280
Number of z-slices: 5
Tile imaging to cover the whole tissue section

I get one file at the end of imaging that contains all the images(channels+z-slices+tiles). I try to open the image in Fiji and want to use plugin “Find focus slices”, so that I have one focus slice for each channel and tile and use them to stitch the tiles.
“Find Focus slices” plugin requires splitted channels to find the focus slice. Imaging if I have 200 tiles, than it will be impossible for me to open each tile > split channels > run the plugin.
I am uploading one sample image with 16 tiles, imaged in 4 channels with 5 z-slices each.
Any help will be much appreciated.
Thanks.
Sample file (https://drive.google.com/open?id=1IY8IDjTDXJhtSsX8K4ZBMxgFkxVT1qQF)

Here is a quick and dirty macro you can use. I assume you are using Bio-Formats to open the data and that you are using this plugin when finding the focused slices. When opening the data sets, you will need to use Bio-Format’s “split channels” option so that each channel is opened as a different stack.

config = "select=100";
if (getBoolean("Use edge filter?")) {
	config = config + " edge";
}
waitForUser("Please open stacks for focus slice finding. Be sure\nto close all other image windows. Then click \"OK\".");
num = nImages();
for (i = 1; i <= num; i++) {
    selectImage(1);
    run("Find focused slices", config);
    selectImage(1);
    close();
}

Hi @Andrew_Shum,
Thanks for looking into the problem and helping me out.
Yes, I use Bio-Format’s to open my image. But I encounter a few problems.
I open image like this and select “split channels”.

So, it opens each tile separately with split channels.

Now, I tried to run the macro you provided.
File > New > Script
I do not know which language to choose, so I picked “.groovy” and tried to run the script and got following errors.

Started New_.groovy at Fri Feb 28 10:48:07 CST 2020
groovy.lang.MissingMethodException: No signature of method: org.scijava.plugins.scripting.groovy.GroovyScriptEngine.getBoolean() is applicable for argument types: (java.lang.String) values: [Use edge filter?]
Possible solutions: asBoolean()
	at org.scijava.plugins.scripting.groovy.GroovyScriptEngine.callGlobal(GroovyScriptEngine.java:366)
	at org.scijava.plugins.scripting.groovy.GroovyScriptEngine.access$000(GroovyScriptEngine.java:79)
	at org.scijava.plugins.scripting.groovy.GroovyScriptEngine$2.invokeMethod(GroovyScriptEngine.java:286)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
	at Script2.run(Script2.groovy:2)
	at org.scijava.plugins.scripting.groovy.GroovyScriptEngine.eval(GroovyScriptEngine.java:303)
	at org.scijava.plugins.scripting.groovy.GroovyScriptEngine.eval(GroovyScriptEngine.java:122)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
	at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
	at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
	at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:228)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

Let me know, what I am doing wrong?
Thanks.

It is an IJ1 Macro; not Groovy.

@Andrew_Shum.
Thanks for your quick reply.
I am able to run the macro. Now I have one focussed z-slice for each channel and tile. Now I want to stitch all the resulting files.
In order to stitch, should I save them first? or is there any other way of directly using the open files for stitching?
Stitching plugin only gives the option to select the images from the folder to stitch.
Thanks.

Unfortunately, I have not had to stitch images myself so I am not familiar with how the image stitching plugin works. However, if you know how to use it with saved images, then it would probably be best to just save them all. If you tell me how the images need to be saved (ex. a separate folder for each channel with each tile as its own file), I can try to add that to the macro for you.

@Andrew_Shum…
Thanks…That would be so nice of you.
I am trying stitching on some images and it looks like that all the tiles and corresponding channels should be in the same folder. So I think saving each resulting z-slice as tiff in one folder will work for me. I will try to import as image sequence and use it for stitching.
Thanks.

In that case, try this revised macro. You can modify the saveAs command to save in whatever format you want.

dir = getDirectory("Select or Create Directory for Focused Slices");
config = "select=100";
if (getBoolean("Use edge filter?")) {
	config = config + " edge";
}
waitForUser("Please open stacks for focus slice finding. Be sure\nto close all other image windows. Then click \"OK\".");
num = nImages();
for (i = 1; i <= num; i++) {
    selectImage(1);
    run("Find focused slices", config);
    saveAs("PNG", dir+getTitle());
    close();
    selectImage(1);
    close();
}

Hi @Andrew_Shum,
Thanks for your help. For stacking, I need to save as Image Sequence and the tiff file names should be ending like name001.tiff, name002.tiff and so on.
I tried to edit the macro for this specific purpose but not successful because of lack of experience.
I want the name of the resulting file to be “file name”+ImageSequence like 001, 002 and so on.
Below are the two edited macros that I tried by searching the forum but didn’t get the result.

dir = getDirectory("Select or Create Directory for Focused Slices");
config = "select=100";
if (getBoolean("Use edge filter?")) {
	config = config + " edge";
}
waitForUser("Please open stacks for focus slice finding. Be sure\nto close all other image windows. Then click \"OK\".");
num = nImages();
for (i = 1; i <= num; i++) {
    selectImage(1);
    run("Find focused slices", config);
    run("Image Sequence... ", "format=TIFF save=dir+getTitle()");
    close();
    selectImage(1);
    close();
}

OR

dir = getDirectory("Select or Create Directory for Focused Slices");
config = "select=100";
if (getBoolean("Use edge filter?")) {
	config = config + " edge";
}
waitForUser("Please open stacks for focus slice finding. Be sure\nto close all other image windows. Then click \"OK\".");
num = nImages();
for (i = 1; i <= num; i++) {
    selectImage(1);
    run("Find focused slices", config);
    run("Image Sequence... ", "format=TIFF digits=3 save=[/Users/marif/Desktop/20xOil/tiff/Sample1001.tif]");
    close();
    selectImage(1);
    close();

If I recall correctly, the Image Sequence... command is for saving a stack as an image sequence. Since the individual slices are separate and not in a stack, I don’t think using that command will work as expected. An image sequence is just a folder with multiple single-slice image files in it. From that perspective, saving them one at a time to the same folder should get the job done with regards to saving as an image sequence.

That being said, the naming thing is something that would need to be added. Could you describe a bit more about how you want them named? For instance, is there a particular order of assignment you want? For example, you could number channels 1-4 of tile 1 1-4 and then number channels 1-4 of tile 2 5-8. Alternatively, let’s assume you have 16 tiles, you could number channel 1 of tiles 1-16 as 1-16 and then number channel 2 of tiles 1-16 as 17-32. For the base name, did you want that to be the name of the name of the original file we get all these stacks from or something else?

Yes, Image Sequence is for stacks.
I am a little bit confused about how to save these tiffs so that they can be used in stitching. I am using the grid stitching from stitching plugin. I want to save the image/tiles in the sequence as they were imaged.
In this case;
Sample1_Tile1_001.tiff (first channel)
Sample1_Tile1_002.tiff (second channel)
Sample1_Tile1_003.tiff (third channel)
Sample1_Tile1_004.tiff (fourth channel)
Sample1_Tile2_001.tiff (first channel) …I don’t know whether the number of tiff should continue or start again from 0 for Tile2.

Earlier, I was using Software software (DeltaVision) to deconvolve, stitch and max project the images. But during analysis/quantification, max projections are not a good choice in certain cases. That is why i am trying the Find Focus Slice followed by stitching and use it for analysis to check if it improves the analysis.
Thanks.

Hi,
@Andrew_Shum,
I was able to edit the macro and save the tiffs.
Below is the edited macro;

dir = getDirectory("Select or Create Directory for Focused Slices");
config = "select=100";
if (getBoolean("Use edge filter?")) {
	config = config + " edge";
}
waitForUser("Please open stacks for focus slice finding. Be sure\nto close all other image windows. Then click \"OK\".");
num = nImages();
counter=0;
for (i = 1; i <= num; i++) {
    selectImage(1);
    run("Find focused slices", config);
    mytitle=split(getTitle(),' - ');
    saveAs("Tiff", dir+mytitle[3]+"_"+1000+counter);
    close();
    selectImage(1);
    close();
    counter += 1 ;
}

Now, I am trying to stitch the files and will let you know if it works.
Thanks for your help.

1 Like