Posted by
nebenb on
Apr 26, 2018; 3:38pm
URL: http://imagej.273.s1.nabble.com/Stitching-together-multiple-images-within-a-series-of-folders-tp5020586.html
Hello,
I am trying to stitch together images of entire c.elegans worms to recreate
a 3d image of the entire worm.
Stitching them manually is easy but tedious, so I am trying to write an
ImageJ macro that will stitch them for me. All the plugins necessary are
available (pairwise stitching, run 8bit, etc) and I can get a macro to
stitch the files in a single folder if I provide the exact file names,
paths, etc within the macro. However, with my severely limited coding
knowledge, I cannot find a way to make this process generic.
Additionally, I want this macro to be propagative (not sure if this is the
right description) to the point where the user selects a 'mother' folder
with subfolders that each contain the images for a single worm (see below).
Then ImageJ would sample each 'daughter' folder, stitch those images
together, and save the result, before moving on to the next 'daughter'
folder.
MOTHER
>DAUGHTER 1
>worm 1_1
>worm 1_2
>worm 1_3
>worm 1_4
>worm 1_5
>worm 1_6
>worm 1_7
>DAUGHTER 2
>worm 2_1
>worm 2_2
>worm 2_3
>worm 2_4
>worm 2_5
>DAUGHTER 3
>worm 3_1
>worm 3_2
>worm 3_3
>worm 3_4
>worm 3_5
>worm 3_6
etc etc
From what I have found on the web and my own trail and error I have pieced
together the following:
// This macro will takemutlipleple folders with individual
// images sequential images, sorted by sequence into folders,
// and stitch them together using the "PairwiseStitching" macro.
// The stitched images will be saved to the 'mother'
// directory with the name of the sequence-folder they were
// constructed from.
print("");
print("-- 3D Stitching --");
print("");
mother = getDirectory("select mother directory");
print("MOTHER directory set as:");
print(mother);
// The MOTHER directory contains any number of daughter
// directories. The following sequence effectively primes
// ImageJ to be familiar with all the files within the
// 'Daughter' directories.
setBatchMode(true);
count = 0;
countFiles(mother);
n = 0;
processFiles(mother);
print(count+" files processed");
function countFiles(mother) {
list = getFileList(mother);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
countFiles(""+mother+list[i]);
else
count++;
print(list.length);
}
}
function processFiles(mother) {
list = getFileList(mother);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
processFiles(""+mother+list[i]);
else {
showProgress(n++, count);
daughter = mother+list[i];
print(daughter);
}
}
}
// The first two images are stitched together to serve as a
// template for subsequent images to be stitched on to.
// This template is saved in the MOTHER directory with the
// file name of the DAUGHTER directory.
for (i=0; i<2; i++)
initial(daughter, mother, file1, file2);
function initial(daughter, mother, file1, file2) {
open(daughter + list[i]);
run("8-bit");
open(daughter + list[i+1]);
run("8-bit");
print("stitching 1+2");
run("Pairwise stitching", "first_image=file1 second_image=file2
fusion_method=[Max. Intensity] check_peaks=5 compute_overlap
subpixel_accuracy x=0.0000 y=0.0000 z=0.0000
registration_channel_image_1=[Average all channels]
registration_channel_image_2=[Average all channels]");
saveAs("Tiff", mother + daughter);
if (i == 2)
exit();
}
// With a template available, imageJ can now propagate through
// The remaining files (i<list.length) to stitch them to the template
stitch(daughter);
for (i = 2; i < list.length; i++)
stitch(daughter, mother, file1, file2);
function stitch(daughter, mother, file1, file2); {
open(daughter + file1);
run("8-bit");
open(mother + "daughter.tif");
print("propagative stitching");
run("Pairwise stitching", "first_image=file1 second_image=daughter.tif
fusion_method=[Max. Intensity] check_peaks=5 compute_overlap
subpixel_accuracy x=0.0000 y=0.0000 z=0.0000
registration_channel_image_1=[Average all channels]
registration_channel_image_2=[Average all channels]");
saveAs("Tiff", mother + daughter)
}
//** I'm not sure if this style of naming will work, i'm not sure how to
recall
Thank you all so much for your help, in advance. I look forward to all
suggestions and criticisms.
- Ben
--
Sent from:
http://imagej.1557.x6.nabble.com/--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html