Login  Register

Correction: Macro for Converting Perkin-Elmer binary series to RGB stacks

Posted by Till Bretschneider on Nov 21, 2006; 10:49am
URL: http://imagej.273.s1.nabble.com/macro-for-OD-calibration-based-on-autoupdate-of-measured-grey-value-tp3700961p3700967.html

In the very last process of cleaning the code I introduced a bug which
should be corrected in the version below.
( I still wonder why the first version ran through without giving any
errors)

Cheers,

Till

Till Bretschneider wrote:

> Hello,
>
> I would like to contribute a macro which converts Perkin-Elmer binary
> image series or stacks to a RGB-tiff stack.
> It runs in batch mode and does so recursively for subfolders.
> If you find bugs or have comments please let me know.
>
> Till
>
corrected version:

//Convert-Perkin Macro by Till Bretschneider

// This macro recursively parses a folders with subdirectories
// and converts series of Perkin-Elmer binary images to a RGB-
// stack.
// The macros reads the 3-dimensions x,y, and z and t, the
// number of frames, from a *.cfg file in xml format.
// It handles timeseries and z-stacks of two color channels
// only. However, it is easy to extend it to any number of
// channels if required.
// Important: The RGB stack is created in the directory of
// where the original data is located.
// You can open the RGB stack in ImageJ. If it is a 3-D stack
// you may wish to use the excellent Image5D plugin which can
// be found here: http://rsb.info.nih.gov/ij/plugins/image5d.html
// Call "Image5D->Convert RGB stack to Image5D" and select "z"
// for the "3rd dimension". "3rd dimension size" should be set
// to the number of z-slices as given in the Log file from
// the Convert-Perkin macro.

// Basis for the macro are the examples of BatchProcessFolders
// and OpenSeriesUsingFilter which can be found on
// http://rsb.info.nih.gov/ij/macros/



dir = getDirectory ("Choose Source Directory ");

convertFiles (dir);

function convertFiles (dir)
{
  list = getFileList (dir);
  for (i = 0; i < list.length; i++)
    {
      if (endsWith (list[i], "/"))
    convertFiles ("" + dir + list[i]);
      else if (endsWith (list[i], ".cfg"))
    {

      cfg = File.openAsString (dir + list[i]);
      lines = split (cfg, "\n");

        X=parseCfg(lines,"X");
        Y=parseCfg(lines,"Y");
        Z=parseCfg(lines,"Z");
        T=parseCfg(lines,"T");

      print ("####");
      print (dir + list[i]);
      print ("X: " + X + " Y: " + Y + " Z: " + Z + " T: " + T);
      print ("####");

//###############################
//main routine

      index2 = indexOf (list[i], ".cfg");
      basename = substring (list[i], 0, index2);
      files=create_file_list(Z,T,basename,3);
   
      setBatchMode (true);

      openList (files, X, Y);
      green = basename + "-green";
      rename ("green");
      Ngreen = nSlices;
      print ("green imported");

      files=create_file_list(Z,T,basename,4);

      openList (files, X, Y);
      red = basename + "-red";
      rename ("red");
      Nred = nSlices;
      print ("red imported");


      if (Nred > Ngreen)
        {
          selectImage ("red");
          for (d = 0; d < Nred - Ngreen; d++)
        {
          setSlice (nSlices);
          run ("Delete Slice");
        }
        }
      else if (Nred < Ngreen)
        {
          selectImage ("green");
          for (d = 0; d < Ngreen - Nred; d++)
        {
          setSlice (nSlices);
        }
          run ("Delete Slice");
        }



      run ("RGB Merge...", "red=red green=green blue=*None*");
      rename (basename + "-RGB");
      saveAs ("Tiff", dir + basename + "-RGB.tif");
      print ("written RGB file: " + basename + "-RGB.tif");
      close ();
//###############################

      i = list.length;    //break after config file was found



    }
    }


//#######################################################################
  function openList (list, w, h)
  {
    stack = 0;


    for (i = 0; i < list.length; i++)
      {
    showProgress (i, list.length);
    if (File.exists (list[i]))
      {

        run ("Raw...", "open=[" + list[i] + "] image=[16-bit Unsigned]
width=" + w +     " height=" + h + " offset=0 number=1 gap=0
little-endian");

        run ("Copy");
        if (stack == 0)
          {
        stack = getImageID ();
          }
        else
          {
        close ();
        selectImage (stack);
        run ("Add Slice");
          }

        run ("Paste");

      }

      }                // if exists
    if (stack != 0)
      setSlice (1);

  }

}                //function openList(list,w,h) {

//#######################################################################
function itostring (i)
{

  if (i > 99)
    string = "" + i;
  else if (i > 9)
    string = "0" + i;
  else
    string = "00" + i;

  return string;

}

//#######################################################################
function parseCfg(lines,item) {

      for (l = 0; l < lines.length; l++)
        {

          index1 = indexOf (lines[l], "<"+item+">");
          if (index1 >= 0)
        {
          index2 = indexOf (lines[l], "</"+item+">");
          sub = substring (lines[l], index1 + 3, index2);
          return parseInt (sub);
        }
    }

exit ("could not parse variable: "+item);

}

//#######################################################################
function create_file_list(Z,T,basename,first) {


      if (Z == 0)
        files = newArray (T);
      else
        files = newArray (Z * T);

      count = 0;

      if (Z == 0)
        for (n = first; n < first + 2 * T - 1; n += 2)
          {
        files[(n - first) / 2] = dir + basename + "." + toHex (n);
          }
      else
        for (n = first; n < first + 2 * T - 1; n += 2)
          {
        for (z = 0; z < Z; z++)
          {
            zstring = itostring (z);
            files[count] =   dir + basename + "_" + zstring + "." +
toHex (n);
            count++;
          }

          }

return files;

}

--
Dr. Till Bretschneider
AG Celldynamics (Gerisch)
Max-Planck-Institut fuer Biochemie
Am Klopferspitz 18a
D-82152 Martinsried, Germany

Tel: +49-89-8578-2329
Fax: +49-89-8578-3885
E-mail: [hidden email]
WWW: http://www.till-bretschneider.de