I have a bunch of image files in one folder (TIF). I already wrote a macro to split the files into RGB channels and then save them to the same directory. The output is thus something like this:
-File 1 -File 1 (red) -File 1 (green) -File 1 (blue) -File 2 -File 2 (red) ... etc. Next I am using the JACoP plugin which takes two image files and computes various kinds of correlation coefficients/analysis between the inputs. For each directory, I want to do the following: Loop through the following for each file in my directory: 1. Set input file one to File X (red) and file two to File X (blue) 2. Click Analyze 3. Set input file one to File X (red) and file two to File X (green) 4. Click Analyze 5. Set input file one to File x (blue) and file two to File x (green) 6. Click Analyze Then finally: 7. Save the analysis output log The problem is that I don't know how to tell ImageJ how to call each of the color files in that order. How do I specify that it pulls out the files in chunks of three (RGB for image 1), analyses them, and then moves on to the next chunk of three (RGB for image 2)? My approach originally was something like this: dir=getDirectory("Choose a Directory"); print(dir); list = getFileList(dir); for (i=0; i<list.length; i++) { open(list[i]); selectImage(i+1); blue = getTitle(); selectImage(i+2); green = getTitle(); selectImage(i+3); red = getTitle(); //Analyze the red, green, and blue images with JACoP plugin //Move to next set of three images and repeat in next loop through } But this will obviously not work on the second iteration of the loop because it will need to be i+4, i+5, i+6 etc. There is probably an ImageJ function or basic approach to doing this kind of batch analysis but I'm not sure what to look for. Any suggestions/pseudocode to point me in the right direction are appreciated! |
My channel names had a certain pattern, so I needed functions that can
search for a specific pattern in the filename. The functions I used are: *split(string, delimiters)* Breaks a string into an array of substrings. *Delimiters* is a string containing one or more delimiter characters. The default delimiter set " \t\n\r" (space, tab, newline, return) is used if *delimiters* is an empty string or split is called with only one argument. Multiple delimiters in the *string* are merged (taken as one) and delimiters at the start or end of the *string* are ignored unless the delimiter is a single comma, a single semicolon or a regular expression. With ImageJ 1.49f or later, *delimiters* can be also a regular expression enclosed in parentheses, e.g. *delimiters*="(\n\n)" splits only at empty lines (two newline characters following each other). *matches(string, regex)* Returns *true* if *string* matches the specified regular expression <http://en.wikipedia.org/wiki/Regular_expression>. See also: startsWith <http://rsb.info.nih.gov/ij/developer/macro/functions.html#startsWith>, endsWith <http://rsb.info.nih.gov/ij/developer/macro/functions.html#endsWith>, indexOf <http://rsb.info.nih.gov/ij/developer/macro/functions.html#indexOf>, replace <http://rsb.info.nih.gov/ij/developer/macro/functions.html#replace>. Regular expressions are especially helpful when you're looking for a file name with a specific pattern. The following site allows you to test your regular expression and see which strings it would match: http://regexpal.com/ Good luck! Avital On Tue, Jul 21, 2015 at 7:37 PM, syntonicC <[hidden email]> wrote: > I have a bunch of image files in one folder (TIF). I already wrote a macro > to > split the files into RGB channels and then save them to the same directory. > The output is thus something like this: > > -File 1 > -File 1 (red) > -File 1 (green) > -File 1 (blue) > -File 2 > -File 2 (red) ... > etc. > > Next I am using the JACoP plugin which takes two image files and computes > various kinds of correlation coefficients/analysis between the inputs. For > each directory, I want to do the following: > > Loop through the following for each file in my directory: > 1. Set input file one to File X (red) and file two to File X (blue) > 2. Click Analyze > 3. Set input file one to File X (red) and file two to File X (green) > 4. Click Analyze > 5. Set input file one to File x (blue) and file two to File x (green) > 6. Click Analyze > > Then finally: > > 7. Save the analysis output log > > The problem is that I don't know how to tell ImageJ how to call each of the > color files in that order. How do I specify that it pulls out the files in > chunks of three (RGB for image 1), analyses them, and then moves on to the > next chunk of three (RGB for image 2)? > > My approach originally was something like this: > > dir=getDirectory("Choose a Directory"); > print(dir); > list = getFileList(dir); > > for (i=0; i<list.length; i++) { > open(list[i]); > selectImage(i+1); > blue = getTitle(); > selectImage(i+2); > green = getTitle(); > selectImage(i+3); > red = getTitle(); > > //Analyze the red, green, and blue images with JACoP plugin > > //Move to next set of three images and repeat in next loop through > } > > But this will obviously not work on the second iteration of the loop > because > it will need to be i+4, i+5, i+6 etc. There is probably an ImageJ function > or basic approach to doing this kind of batch analysis but I'm not sure > what > to look for. Any suggestions/pseudocode to point me in the right direction > are appreciated! > > > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Calling-an-ImageJ-macro-on-files-in-sets-of-3-tp5013674.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by syntonicC
I think there are multiple solutions
to increment the i with a larger number just put for (i=0; i<list.length; i+=3) { } this works fine for increasing the numbers like you want. However what I found out while coding myself is that the filelist is hardly logically ordered when it is obtained from the directory therefore i think you need to make sure that you are calling the right files. First sort the array so it should work Array.sort(list); then while opening i would not go through the list like you are suggesting by just opening the next image file and assuming everything is correct I'd use original= getTitle(); blue=substring(original,0,lengthOf(original)-4)+" (blue).jpg" // substring removes the .*** ending of the title. the + " (blue).***" adds the desired ending of the image to be opened. this way you cannot open the wrong files hope this helps Joost -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks for your help!
Here is what I have so far. dir=getDirectory("Choose a Directory"); print(dir); list = getFileList(dir); Array.sort(list); for (i=0; i<list.length; i+=3) { open(list[i]); original=getTitle; blue=substring(original,0,lengthOf(original)-9)+"-blue.tif"; open(list[i+1]); original=getTitle; green=substring(original,0,lengthOf(original)-10)+"-green.tif"; open(list[i+2]); original=getTitle; red=substring(original,0,lengthOf(original)-8)+"-red.tif"; // print(blue); // print(red); // print(green); run("JACoP ", "imga=&red imgb=&blue thra=648 thrb=517 pearson mm"); run("JACoP ", "imga=&red imgb=&green thra=648 thrb=517 pearson mm"); run("JACoP ", "imga=&blue imgb=&green thra=648 thrb=517 pearson mm"); } The format of my images is a little different than how I had them in the original post. In any case, the print functions I included for testing correctly returns the full image names of the first three blue, green, and red images in that order. So far so good! However, when I run the JACoP plugin it complains that the images were not found. Am I using string interpolation correctly? I thought I had to use square brackets to pass the variable into the string but apparently this is not the case anymore for the run function. According to <a href="http://rsb.info.nih.gov/ij/developer/macro/macros.html">this page: I am using ImageJ version 1.48v. |
Free forum by Nabble | Edit this page |