Login  Register

Re: programmatically opening a stack from a list of filenames

Posted by Ben.BigHair on Apr 05, 2007; 8:08pm
URL: http://imagej.273.s1.nabble.com/programmatically-opening-a-stack-from-a-list-of-filenames-tp3699826p3699832.html

Albert Cardona wrote:

> Ben,
>
> Perhaps best is:
>
> public class StackPlus extends ImagePlus {
>     public StackPlus(String concat_filenames) {
>         super();
>         String[] files = concat_filenames.split("\n");
>         // open all files and place their processors in an ImageStack
>         // ...
>     }
> }
>
> Then just call, from your plugin:
>
> public class My_Plugin implements PlugIn {
>     public void run(String arg) {
>         // obtain filenames from wherever
>         // ...
>         ImagePlus imp = new StackPlus(filenames);
>         imp.show();
>     }
> }
>
>
> The above assumed absolute paths where concatenated in a single String and
> separated by newline characters.
>
> Albert
>


Hi and Wow!

That is easy-easy-easy!  To think I have been bumbling around all this
time.   The class (below) accepts either a string of concatenated paths
or a string array of the same.  Thanks so much for the guidance and
encouragement.

Cheers,
Ben

***BEGIN
/**

     I had asked on the ImageJ mailing list how to programmatically
create a stack from a
     list of one or more file names.  This is slightly different than
the making a stack by
     specifying a directory which contains the images.  My need comes
from working with an
     instrument that writes "data" images and related-but-different
"calibration" images
     to the same directory.

     Albert Cardona answered via the ImageJ mailing list suggesting a
couple of approaches.
     This was his last suggestion.

         Perhaps best is:

         public class StackPlus extends ImagePlus {
             public StackPlus(String concat_filenames) {
                 super();
                 String[] files = concat_filenames.split("\n");
                 // open all files and place their processors in an
ImageStack
                 // ...
             }
         }
         Then just call, from your plugin:

         public class My_Plugin implements PlugIn {
             public void run(String arg) {
                 // obtain filenames from wherever
                 // ...
                 ImagePlus imp = new StackPlus(filenames);
                 imp.show();
             }
         }

         The above assumed absolute paths where concatenated in a single
String and
         separated by newline characters.

         Albert

     2007-04-05 [hidden email] - Thanks Albert!
*/

import ij.*;
import ij.io.*;
import java.io.*;

/**
This class extends ImagePlus by adding two constructors. The first
accepts a String
of absolute file paths concatenated with the new line character ("\n").
  The second
accepts a String array of absolute file paths.

@see ij.ImagePlus
@see ij.ImageStack
*/
public class StackPlus extends ImagePlus {

     /** Constructs a stack from a String of absolute file paths
concentanated with the
      "\n" newline character */
     public StackPlus(String concat_filenames) {
         super();
         String[] files = concat_filenames.split("\n");
         // open all files and place their processors in an ImageStack
         // ...
         Opener opener = new Opener();
         File file = new File(files[0]);
         String name = file.getName();
         if  (file.exists() == false) {
             IJ.log("File not found: " + files[0]);
             return;
         }

         IJ.showProgress(0, files.length);
         ImagePlus imp = opener.openImage(files[0]);
         int w = imp.getWidth();
         int h = imp.getHeight();
         ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
         stack.addSlice(name, imp.getProcessor());
         //step through the remaining images - check for existence and size
         if (files.length > 1) {
             for (int i = 1; i< files.length; i++){
                 IJ.showProgress(i, files.length);
                     //check each file
                 file = new File(files[i]);
                 if  (file.exists() == false) {
                     IJ.log("File not found: " + files[i]);
                     return ;
                 }
                 imp = opener.openImage(files[i]);
                 //check the dimensions of each image
                 if ((imp.getWidth() != w) || (imp.getHeight() != h)){
                     IJ.log("Images must all the same size");
                     return ;
                 }
                 stack.addSlice(file.getName(), imp.getProcessor());
             }
         }
        setStack(name,stack);
     }//end of run method

     /** Constructs a stack from a String array of absolute file paths */
     public StackPlus(String[] files) {
         super();
         // open all files and place their processors in an ImageStack
         // ...
         Opener opener = new Opener();
         File file = new File(files[0]);
         String name = file.getName();
         if  (file.exists() == false) {
             IJ.log("File not found: " + files[0]);
             return;
         }

         IJ.showProgress(0, files.length);
         ImagePlus imp = opener.openImage(files[0]);
         int w = imp.getWidth();
         int h = imp.getHeight();
         ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
         stack.addSlice(name, imp.getProcessor());

         if (files.length > 1) {
             for (int i = 1; i< files.length; i++){
                 IJ.showProgress(i, files.length);
                     //check each file
                 file = new File(files[i]);
                 if  (file.exists() == false) {
                     IJ.log("File not found: " + files[i]);
                     return ;
                 }

                 imp = opener.openImage(files[i]);

                 //check the dimensions of each image
                 if ((imp.getWidth() != w) || (imp.getHeight() != h)){
                     IJ.log("Images must all the same size");
                     return ;
                 }

                 stack.addSlice(file.getName(), imp.getProcessor());
             }
         }
        setStack(name,stack);
     }//end of run method

}//end of class definition


***END