Hi, I wrote a Macro that batch Z projects a directory tree. There is one thing that gives me a headache: When I make a Z-projection, it results in a newly created image, which is not *always* the active (selected) one. Subsequent calls to save() sometimes save the MIP, sometimes the stack. I would select the Image by string, but how do I know the user has not just opened another image with the same title? Is there 1) a way to be sure to select the image that was the result of the last operation, or 2) a way to tell Z-project the name it should give to the resulting window, so I can generate my own string Id? If there is a well-known way, please add it to the macros tutorial. Thanks in advance, and keep up the good work, Mario Emmenlauer // "BatchProcessFolders" // // This macro batch processes all the files in a folder and any // subfolders in that folder. In this example, it runs the Subtract // Background command of TIFF files. For other kinds of processing, // edit the processFile() function at the end of this macro. requires("1.33s"); inputDir = getDirectory("Choose a Source Directory"); outputFormats = newArray("TIFF", "8-bit TIFF", "JPEG", "GIF", "PNG", "PGM", "BMP", "FITS", "Text Image", "ZIP", "Raw"); projectionTypes = newArray("Max Intensity", "Min Intensity", "Average Intensity", "Sum Slices", "Standard Deviation", "Median"); Dialog.create("Select MIP options"); Dialog.addMessage("Please select Batch Projection options"); Dialog.addChoice("Save projection as: ", outputFormats, "TIFF"); Dialog.addChoice("Use Projection Type: ", projectionTypes, "Max Intensity"); Dialog.addString("Default filename appendix", "_MIP"); Dialog.addCheckbox("Convert to 8 bit", true); Dialog.addCheckbox("Recreate existing MIPs", true); Dialog.addCheckbox("Select different output directory", false); Dialog.show(); outputFormat = Dialog.getChoice(); projectionType = Dialog.getChoice(); outputAppendix = Dialog.getString(); outputEightBit = Dialog.getCheckbox(); shouldRecreate = Dialog.getCheckbox(); if( Dialog.getCheckbox()) { outputDir = getDirectory("Choose an Output Directory"); } else { outputDir = inputDir; } setBatchMode(true); run("Bio-Formats Macro Extensions"); count = 0; countFiles(inputDir); n = 0; processFiles(inputDir); //print(count+" files processed"); function countFiles(inputDir) { list = getFileList(inputDir); for (i=0; i<list.length; i++) { if (endsWith(list[i], "/")) countFiles(""+inputDir+list[i]); else count++; } } function processFiles(inputDir) { list = getFileList(inputDir); for (i=0; i<list.length; i++) { if (endsWith(list[i], "/")) processFiles(""+inputDir+list[i]); else { showProgress(n++, count); path = inputDir+list[i]; processFile(path); } } } function processFile(path) { outfileName = File.getName(path); outfileName = substring( outfileName, 0, lastIndexOf(outfileName, ".")); // is the file itself a projection (we have created)? if( !endsWith(outfileName, outputAppendix)) { //outfileName = replace(path, ".", "_"); //if( shouldRecreate || !File.exists(outputDir+outfileName)) { run("Bio-Formats Importer", "open=["+path+"] view=[Standard ImageJ] stack_order=Default split_channels split_timepoints open_all_series use_virtual_stack"); if( nSlices > 1) { run("Z Project...", "start=1 stop="+nSlices+" projection=["+projectionType+"]"); if( outputEightBit && bitDepth() % 16 == 0) { //getHistogram(values, counts, nBins[, histMin, histMax]) //setMinAndMax(min, max); //setAutoThreshold(); run("8-bit"); } saveAs(outputFormat, outputDir+outfileName+outputAppendix); close(); } close(); //} } } |
Hi Mario,
concerning "z projection", I have never experienced the problem of the result not being the foreground window. Maybe you are using an old version of ImageJ? There was a race condition problem in ImageJ versions prior to 1.39f (it seems that this bugfix is not mentioned in the release notes). This might have caused the problem that you describe. Michael ________________________________________________________________ On 22 Jul 2008, at 08:42, Mario Emmenlauer wrote: > Hi, > > I wrote a Macro that batch Z projects a directory tree. There is one > thing that gives me a headache: > When I make a Z-projection, it results in a newly created image, > which is not *always* the active (selected) one. Subsequent calls > to save() sometimes save the MIP, sometimes the stack. > > I would select the Image by string, but how do I know the user > has not just opened another image with the same title? Is there > 1) a way to be sure to select the image that was the result of the > last operation, or > 2) a way to tell Z-project the name it should give to the resulting > window, so I can generate my own string Id? > > If there is a well-known way, please add it to the macros tutorial. > > Thanks in advance, and keep up the good work, > > Mario Emmenlauer > > > // "BatchProcessFolders" |
Hi Michael,
it seems this might have been the problem, after upgrading to latest ImageJ I don't see this error any more. But the basic problem remains, I don't know which stacks my macro opened. Is there a way for a plugin or macro to know which windows it opened? I use the ome loci plugin to load datasets with multiple timepoints, and have the 'split' option activated, so I get T (invisible) datasets. It seems like bad programming to just call process() close() T times, and assume it will be the correct windows. What would happen if the user activates another window in the meantime? Would I not process and close the wrong one? I could of course make assumptions on the opened datasets names, but that seems like even more error-prone? Cheers, Mario Michael Schmid wrote: > Hi Mario, > > concerning "z projection", I have never experienced the > problem of the result not being the foreground window. > > Maybe you are using an old version of ImageJ? There was a race > condition problem in ImageJ versions prior to 1.39f (it seems > that this bugfix is not mentioned in the release notes). > This might have caused the problem that you describe. > > Michael > ________________________________________________________________ > > On 22 Jul 2008, at 08:42, Mario Emmenlauer wrote: > >> Hi, >> >> I wrote a Macro that batch Z projects a directory tree. There is one >> thing that gives me a headache: >> When I make a Z-projection, it results in a newly created image, >> which is not *always* the active (selected) one. Subsequent calls >> to save() sometimes save the MIP, sometimes the stack. >> >> I would select the Image by string, but how do I know the user >> has not just opened another image with the same title? Is there >> 1) a way to be sure to select the image that was the result of the >> last operation, or >> 2) a way to tell Z-project the name it should give to the resulting >> window, so I can generate my own string Id? >> >> If there is a well-known way, please add it to the macros tutorial. >> >> Thanks in advance, and keep up the good work, >> >> Mario Emmenlauer >> >> >> // "BatchProcessFolders" > (...) > |
In reply to this post by Mario Emmenlauer-3
Hi,
I am also trying to Z project a batch of TIF images from a directory, however, this macro results in reading errors for me. I would like to Max project a series of stacks from a folder and save the projections as TIFFs in the original folder with their original name with addition _Max. I am new to writing macros, any help would be much appreciated. Thanks. |
In reply to this post by Mario Emmenlauer-3
Hi,
I would like to use a macro like this where I open a directory of TIFF stacks and max project each one and save it as a TIFF. I have tried this macro and another one, and I keep having 2 problems - 1. It tells me that "Non RGB Stack required" and will not run the Macro, although these are not RGB stacks. 2. It doesn't recognize all of my folders when I browse for folder with the stacks, some are just missing. Although I can move the images to folders which are present, it is still perplexing... Thanks for the help, it is much appreciated and will save me loads of time. - Adina. |
In reply to this post by Mario Emmenlauer-3
Hi Adina,
I wrote a macro sometime back to do something similar. I made a few changes in it, so now it does "max projection" and saves the files in a new folder, inside your TIFF stack folder. Macro is attached. If you get error message(s), then make sure that you have the latest ImageJ version installed. If that does not help, then let me know. Ved path = getDirectory("Choose a Directory"); filename = getFileList(path); newDir = path + "Max Projections" + File.separator; if (File.exists(newDir)) exit("Destination directory already exists; remove it and then run this macro again"); File.makeDirectory(newDir); for (i=0; i<filename.length; i++) { if(endsWith(filename[i], ".tif")) { open(path+filename[i]); run("Z Project...", "projection=[Max Intensity]"); saveAs("tiff", newDir + getTitle); close(); close(); } } |
Free forum by Nabble | Edit this page |