Login  Register

Re: Using Image Calculator with Virtual Stacks

Posted by Rasband, Wayne (NIH/NIMH) [E] on Jul 02, 2011; 7:03pm
URL: http://imagej.273.s1.nabble.com/Using-Image-Calculator-with-Virtual-Stacks-tp3684024p3684025.html

On Jul 1, 2011, at 7:02 PM, jmarks wrote:

> Dear All
> I have 2 image stacks that are far too large to fit in memory. I would like
> to generate and save ratioed images of each pair of slices in the two
> stacks.
> I thought that Virtual Stacks and Process>Batch>Virtual Stack would do the
> job, but I am getting unexpected results.
> Each virtual stack has its own directory.
> Using the Batch Process Dialogue, I can identify the directory to contain
> the 32-bit ratioed images.

This is not working because Process>Batch>Virtual Stack only supports a single input stack, but the Image Calculator requires two. What you can do instead is use a macro like the one below. What the macro does is calculate the ratio of two virtual stacks, which are assumed to be open, saving the output in a directory, which is opened on completion as a third virtual stack. If you are running the 1.45k daily build, the last line of the macro can be replaced with
  run("Image Sequence...", "open=&dir sort use");

 requires("1.45d");
 if (nImages!=2)
    exit("Exactly 2 stacks required");
 setBatchMode(true);
 dir = getDirectory("Select output directory");
 for (i=1; i<=nSlices; i++) {
    showProgress(i, nSlices);
    selectImage(1);
    setSlice(i);
    selectImage(2);
    setSlice(i);
    imageCalculator("Divide create 32-bit", 1, 2);
    save(dir+IJ.pad(i, 5)+".tif");
    close;
 }
 setBatchMode(false);
 run("Image Sequence...", "open=["+dir+"] sort use");


> Running Image Calculator in the code box provided in the dialogue produces
> the correct 32 bit result image. I can rename this result image "Result" and
> append the current Slice number of the virtual stacks to generate a unique
> filename, e.g. Result.001, Result.002...
>
> However, what ends up being saved by the Batch Process as "Result.*" is not
> the 32-bit result image, but the current slice of one of the virtual stacks
> used as input to the Image calculator. Furthermore, specifying the Output
> folder seems to serve no function, as an explicit SaveAs command requires a
> fully qualified path.
>
> The simple macro code I have written in the code box is
>
>      run("Image Calculator...", "image1=[B 340 backsub] operation=Divide
> image2=[B 380 backsub] 32-bit");   // do the division with 32 bit result
>      rename ("Result") ;                  
>      selectWindow("B 340 backsub") ;   // select one of the virtual stacks
>      n = getSliceNumber() ;                 // get the current slice index
>      nString = toString(n, 0);                // convert the index to a
> string
>      MyImage = "ratio." + nString ;        // create a string for the
> output filename with the index as the extension
>      selectWindow("Result") ;               // select the result image
>       SaveAsString = "Y:\\human brain\\june 28 2011 cold\\B ratio\\" +
> MyImage ; // create the string with the fully qualitifed path
>        saveAs("Tiff", SaveAsString);  // save the result image
>       close() ;                              // close the result image
>
>
> Have I misunderstood how this batch functionality with Virtual Stacks is
> intended to be used?
> Any help would be very much appreciated.
> Jeremy Marks
> University of Chicago
>
> --
> View this message in context: http://imagej.588099.n2.nabble.com/Using-Image-Calculator-with-Virtual-Stacks-tp6539611p6539611.html
> Sent from the ImageJ mailing list archive at Nabble.com.