Hello,
I have a very basic question, but since I never wrote a macro before I feel really lost. I need a simple macro that opens every separate file and that for every file in a folder calculates its difference with a one specified picture and then inverts it. I know how to do the difference: process>image calculator> and how to invert ctrl+shift+i, however I do not know how to make it repeat the function for every separate file, i.e. I do not know how to refer to my files while writing the macro. I would really appreciate your advice! |
1) If you have 1 directory with all the files, you can use this:
head = getDirectory("Choose a directory containing raw image sets."); setBatchMode(true); children = getFileList(head); for(i = 0; i < children.length; i++) { curChild = curDir + children[i]; // DO STUFF HERE WITH curChild } 2) If you need to recursively search folders: head = getDirectory("Choose a directory containing raw image sets."); setBatchMode(true); focusStacks(head); function focusStacks(curDir) { children = getFileList(curDir); for(i = 0; i < children.length; i++) { curChild = curDir + children[i]; isDir = File.isDirectory(curChild); if(!isDir) { // DO STUFF HERE WITH curChild } else { focusStacks(curChild); } } } 3) Batchmode doesn't work well.... It should be fine for what you are trying to do, but if you make your macro any more complicated, you might want to consider making a plugin. |
In reply to this post by olao4
Oh, you will definitely need this as well! fileName = File.getName(curDir); selectWindow(fileName + ".tif"); Once you select a window, you can just run commands like you normally would. |
Thanks for a fast reply!
I still have a problem though. It says that some variable is undefined, I guess it means curDir, even after I added the two lines you posted later... |
I didn't give you the EXACT code. I only gave you some starting points. Here, I just wrote the following code for you, but I didn't test it out: // Get image to do subtraction userImg = File.openDialog("Select Specified File") open(userImg); imgName = File.getName(userImg); // Get file list head = getDirectory("Choose a directory containing raw image sets."); children = getFileList(head); // Set batch mode to true setBatchMode(true); // Process each image for(i = 0; i < children.length; i++) { // Open 2nd image curChild = head + children[i]; open(curChild); // Subtraction imageCalculator("Subtract create", imgName, children[i]); // Invert result selectWindow("Result of " + imgName); run("Invert"); // Save result selectWindow("Result of " + imgName); saveAs("Tiff", head + "Result_" + imgName); close(); // Close 2nd image selectWindow(children[i]); close(); } // Close 1st image selectWindow(imgName); close(); A few things to note: 1) I didn't know if you are trying to subtract the 1st image from the 2nd or the 2nd from the first. 2) If you want to continue using Macros, you will unfortunately need to learn a little programming. Herre are some resources: http://rsb.info.nih.gov/ij/developer/macro/macros.html http://rsbweb.nih.gov/ij/developer/macro/functions.html Start imageJ and then use Plugins->Macros->Record... |
Free forum by Nabble | Edit this page |