Login  Register

Re: Simple Macro

Posted by Aryeh Weiss on Jan 26, 2016; 4:37pm
URL: http://imagej.273.s1.nabble.com/Simple-Macro-tp5015462p5015466.html

On 26/01/2016 3:40 PM, Jessica Davis wrote:

> Hello everyone,
>
> I am looking for help to write a simple macro to open different single
> images in the same folder, combine them into a stack, and save the stack
> under a different name in a different folder.
>
> The images I want to combine have similar titles. For example:
> "w000_t000_tt000" , "w000_t000_tt001" , "w000_t000_tt002"... and
> "w001_t000_tt000" , "w001_t000_tt0001" , "w001_t000_tt002" ...
>
> Can this be done?
>
>
This macro may help you (you may need to repair line breaks). It was
deigned to take a directory with x###_y###_z### and create
x###_y### stacks which contain all of the z### images. I have a more
developed python version, which does all sorts of other things.
If you want that, or need clarification for this, feel free to ask:

// beginning of macro------------------------------------------------------

inputPath = File.openDialog("choose an image file");

if (inputPath == "") {
     exit("User canceled the dialog");
}

inputName = File.getName(inputPath);
inputDir = File.getParent(inputPath);
dirName = substring(inputDir, lastIndexOf(inputDir,"/")+1);
print("input path: ", inputPath);
print("input directory path: ", inputDir);
print("input durectory name: ", dirName);
// print(inputName);

inputPrefix = substring(inputName, 0, lastIndexOf(inputName, "."));
// nprint(inputPrefix);
fileList = getFileList(inputDir);



// print(indexOf(fileList[fileList.length-1],"_x")+2);

/* determine the number of fields in the  x and y directions
  *  The naming convention is: name_x###_y###_z###
  *  total number of fields (numFields) = total number of files/number
of z planes.
  *  number of x fields is found by parsing x### in the last file of the
input directory
  *  number of y fields is (total fields)/(x fields)
  *
  */

// determine number of z-planes each xy field
zPlanesStr = substring(fileList[fileList.length-1],
indexOf(fileList[fileList.length-1],"_z")+2,
indexOf(fileList[fileList.length-1],"_z")+5);
zPlanes = parseInt(zPlanesStr);
print("number of z planes: ",zPlanes);
numFields = fileList.length/zPlanes;
print("total number of fields: ",numFields);

// Find total number of x### values
xFieldsStr = substring(fileList[fileList.length-1],
indexOf(fileList[fileList.length-1],"_x")+2,
indexOf(fileList[fileList.length-1],"_x")+5);
xFields = parseInt(xFieldsStr);

// number in y direction must equal total/x-dimension. This can be
verified to make it more robust.
yFields = numFields/xFields;
print("number of x fields: ",xFields, " number of y fields: ",yFields);


sliceIndex = 1;
// loop through all of the XY locations
for (yLoc = 1; yLoc <= yFields; yLoc++) {
     for (xLoc = 1; xLoc <= xFields; xLoc++) {
// open an image sequence fro all images which have the current XY
location. This will be our z-stack
         run("Image Sequence...", "open="+inputDir+"
file=x"+IJ.pad(d2s(xLoc,0),3)+"_y"+IJ.pad(d2s(yLoc,0),3)+" sort");
         selectWindow(dirName);
         rename(dirName+"_"+IJ.pad(xLoc,3)+"_"+IJ.pad(yLoc,3));

     }
}

// end of macro------------------------------------------------------


hope this helps
--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html