Loop the Open Image Sequence as Hyperstack function?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop the Open Image Sequence as Hyperstack function?

Christine Labno
Hello group,

I would like to expand on a question posted by Christophe
Leterrier a couple months ago.  I am opening also a group of
files like this:

>> image001_ch01
>> image001_ch02
>> image001_ch03
>> image002_ch01
>> image002_ch02. . .


as a hyperstack with LOCI Bio-Formats, using the "group files
with similar names" method suggested by Curtis, and it works
beautifully.  

However, the end user has to choose a file from the stack by
hand each time, so only one group/stack can be processed at a
time. Can I set this up to loop and process every group in a
folder, but without opening every .tif and therefore
processing each group multiple times?  In other words, how do
I set the script to choose every _nth_ .TIFF file?  

I usually use something like this to loop and process
every tiff in a folder:

for (f=0; f<list.length; f++) { //main files loop
        path = dir+list[f];
       // showProgress(f, list.length);
 if (!endsWith(path,"/") && endsWith(path,"f")) open(path);  
//do only .tif files
 print(path);
  if (nImages>=1) {

I'm sure this has been done before, but everything I have
tried seems incompatible with the LOCI importer. Any
suggestions?  


Christine


Christine Labno, Ph.D.
Asst. Technical Director
Light Microscopy Core
University of Chicago
Office of Shared Research Facilities
Abbott 129
(773) 834-9040 (phone)
Reply | Threaded
Open this post in threaded view
|

Re: Loop the Open Image Sequence as Hyperstack function?

ctrueden
Hi Christine,

One easy way to solve your problem is to place each group of files in
its own subfolder. Then in your file loop, for each directory found,
you could call another subroutine that passes the first TIFF file
found within that directory to the Importer plugin.

If you need to keep multiple file groups in the same folder, the code
becomes a bit trickier. The most straightforward thing to do -- if you
know how to program in Java -- would be to write a custom plugin that
repeatedly calls the Bio-Formats Importer plugin. I have included an
example plugin below to get you started.

It may be possible to do what you want using only the ImageJ macro
language, but you would need to use the Bio-Formats Macro Extensions
with the getUsedFile and getUsedFileCount functions and do a lot of
bookkeeping with lists of files, so it is probably easier to just use
the plugin code below.

The code below calls the Bio-Formats Importer in windowless mode to
avoid popping up an options dialog for every file group in the
directory, but you can change that by removing the "windowless=true"
section of the params string.

Please let me know if you have any further questions or issues with Bio-Formats.

Cheers,
Curtis

---------------
//
// Mass_Importer.java
//

import ij.IJ;
import ij.gui.YesNoCancelDialog;
import ij.io.DirectoryChooser;
import ij.plugin.PlugIn;
import java.io.File;
import java.util.HashSet;
import java.util.Vector;
import loci.formats.FilePattern;
import loci.formats.ImageReader;
import loci.plugins.LociImporter;

/**
 * Processes all image files in the chosen directory,
 * grouping files with similar names.
 *
 * @author Curtis Rueden ctrueden at wisc.edu
 */
public class Mass_Importer implements PlugIn {

  public void run(String arg) {
    // prompt user for directory to process
    DirectoryChooser dc = new DirectoryChooser("Bio-Formats Mass Importer");
    String dirPath = dc.getDirectory();

    // create a list of files we have already processed
    HashSet done = new HashSet();

    // image reader object, for testing whether a file is in a supported format
    ImageReader tester = new ImageReader();

    // list of files to actually open with Bio-Formats Importer
    Vector filesToOpen = new Vector();

    // process all files in the chosen directory
    File dir = new File(dirPath);
    File[] files = dir.listFiles();
    IJ.showStatus("Scanning directory");
    for (int i=0; i<files.length; i++) {
      String id = files[i].getAbsolutePath();
      System.out.println("Testing " + id + "...");//TEMP
      IJ.showProgress((double) i / files.length);

      // skip files that have already been processed
      if (done.contains(id)) continue;

      // skip unsupported files
      if (!tester.isThisType(id, false)) continue;

      // use FilePattern to group files with similar names
      String name = files[i].getName();
      FilePattern fp = new FilePattern(name, dirPath);

      // get a list of all files part of this group, and mark them as done
      String[] used = fp.getFiles();
      for (int j=0; j<used.length; j++) done.add(used[j]);

      filesToOpen.add(id);
    }
    IJ.showProgress(1.0);
    IJ.showStatus("");

    // confirm that user wants to proceed in opening the file groups
    int numToOpen = filesToOpen.size();
    if (numToOpen == 0) {
      IJ.showMessage("No file groups found.");
      return;
    }
    String groups = numToOpen == 1 ?
      "1 file group" : (numToOpen + " file groups");
    YesNoCancelDialog confirm = new YesNoCancelDialog(IJ.getInstance(),
      "Bio-Formats Mass Importer", "Found " + groups + " in directory '" +
      dirPath + "'; proceed?");
    if (!confirm.yesPressed()) return;

    // launch the Bio-Formats Importer plugin to open each group of files
    for (int i=0; i<numToOpen; i++) {
      String id = (String) filesToOpen.get(i);
      String params =
        "location=[Local machine] " +
        "windowless=true " +
        "group_files=true " +
        "open=[" + id + "] ";
      new LociImporter().run(params);
    }
    IJ.showStatus("");
  }

}

On Thu, Jul 10, 2008 at 1:28 PM, Christine Labno
<[hidden email]> wrote:

> Hello group,
>
> I would like to expand on a question posted by Christophe
> Leterrier a couple months ago.  I am opening also a group of
> files like this:
>
>>> image001_ch01
>>> image001_ch02
>>> image001_ch03
>>> image002_ch01
>>> image002_ch02. . .
>
>
> as a hyperstack with LOCI Bio-Formats, using the "group files
> with similar names" method suggested by Curtis, and it works
> beautifully.
>
> However, the end user has to choose a file from the stack by
> hand each time, so only one group/stack can be processed at a
> time. Can I set this up to loop and process every group in a
> folder, but without opening every .tif and therefore
> processing each group multiple times?  In other words, how do
> I set the script to choose every _nth_ .TIFF file?
>
> I usually use something like this to loop and process
> every tiff in a folder:
>
> for (f=0; f<list.length; f++) { //main files loop
>        path = dir+list[f];
>       // showProgress(f, list.length);
>  if (!endsWith(path,"/") && endsWith(path,"f")) open(path);
> //do only .tif files
>  print(path);
>  if (nImages>=1) {
>
> I'm sure this has been done before, but everything I have
> tried seems incompatible with the LOCI importer. Any
> suggestions?
>
>
> Christine
>
>
> Christine Labno, Ph.D.
> Asst. Technical Director
> Light Microscopy Core
> University of Chicago
> Office of Shared Research Facilities
> Abbott 129
> (773) 834-9040 (phone)
>