Login  Register

programmatically opening a stack from a list of filenames

Posted by Ben.BigHair on Apr 02, 2007; 4:19pm
URL: http://imagej.273.s1.nabble.com/programmatically-opening-a-stack-from-a-list-of-filenames-tp3699826.html

Hello,

I keep fooling myself into thinking that there exists a simple interface
in the ImagePlus or ImageStack classes to open a series of images in a
stack by providing the filenames. I can't decide if I suffer from (a) a
combination of wishful thinking and laziness or (b) a muddled concept of
the relationships among ImageStack, ImagePlus and ImageProcessor.*  In
any event, when I roll my own I always feel like I am doing things
awkwardly, and seek to get some pointers on how to  program this more
elegantly.

The code snippet (shown at very bottom of message) works, but it seems
like a lot of steps.  Here are the pseudo-code steps shown in the code.

- open the first image in the list as an ImagePlus
- define an ImageStack based upon the first images dimensions
- add the first image's ImageProcessor to the ImageStack
- add each of the remaining files to the ImageStack
- create a new ImagePlus from the ImageStack


Have I made this unnecessarily complex?


Thanks,
Ben

* or I suffer from both (a) and (b)!



///BEGIN

private ImagePlus openImagesAsStack(String name, String[] fnames){

     //use Opener to read images into ImagePlus objects
     //read the first image in the list and use it's dimensions to define
     // the ImageStack object dimensions
     Opener opener = new Opener();
     ImagePlus imp = opener.openImage(fnames[0]);
     ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());

     //add the first slice
     File file = new File(fnames[0]);
     stack.addSlice(file.name(), imp.getProcessor();

     //step through the remaining files and
     //add addtional slices by adding ImageProcessor objects
     if (fnames.length > 1) {
         for (int i = 1; i< fnames.length; i++){
             stack.addSlice(fnames[i], (opener.openImage(
fnames[i])).getProcessor());
         }
     }
     //create a new ImagePlus to contain the stack
     ImagePlus imp2 = new ImagePlus(name, stack);
     return imp2;
}

///END