I'm trying to 4 color image ND2 files into separate color, adjust each color with different parameters, and re-merge them into a single file in a batch mode. The script I wrote is below. At the end of macro, it stops with a dialog box saying " Merge Channels There must be at least one source image or stack [OK]". Could anyone help me to fix it?
dir1 = getDirectory('Choose Directory '); list = getFileList(dir1); dir2 = dir1+"JPG"+File.separator; File.makeDirectory(dir2); setBatchMode(true); for (i=0; i<list.length; i++) { print(dir1 + list[i]); showProgress(i, list.length); name = dir1 + list[i]; run("Bio-Formats Importer", "open=name view=Hyperstack stack_order=XYCZT Color mode=Default split_channels"); name1 = list[i]; index = lastIndexOf(name1, "."); if (index!=-1) name2 = substring(name1, 0, index); path = dir2 + name2; print(path); selectWindow(name1 + ' - C=0'); run("Channels Tool..."); run("Green"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 50); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=1'); run("Channels Tool..."); run("Blue"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 150); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=2'); run("Channels Tool..."); run("Yellow"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 35); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=3'); run("Channels Tool..."); run("Magenta"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 100); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow("Channels"); run("Close"); selectWindow(name1 + ' - C=0'); green = getTitle; selectWindow(name1 + ' - C=1'); blue = getTitle; selectWindow(name1 + ' - C=2'); yellow = getTitle; selectWindow(name1 + ' - C=3'); magenta = getTitle; print(green); print(blue); print(yellow); print(magenta); run("Merge Channels...", "C2=green C3=blue C6=yellow C7=magenta keep"); saveAs("jpeg", path + ".jpg"); run("Close"); } |
Try changing the last line of your macro to:
run("Merge Channels...", "C2="+green+" C3="+blue+" C6="+yellow+" C7="+magenta+" keep"); saveAs("jpeg", path + ".jpg"); run("Close"); } green, blue, yellow and magenta are variable names that hold the titles of your images. In your version of the code, the code looks for images called 'green', 'blue', etc. Hope this helps, Volko On 21/05/2015 20:42, woojkim wrote: > I'm trying to 4 color image ND2 files into separate color, adjust each color > with different parameters, and re-merge them into a single file in a batch > mode. The script I wrote is below. At the end of macro, it stops with a > dialog box saying " Merge Channels There must be at least one source image > or stack [OK]". Could anyone help me to fix it? > > > dir1 = getDirectory('Choose Directory '); > list = getFileList(dir1); > dir2 = dir1+"JPG"+File.separator; > File.makeDirectory(dir2); > > setBatchMode(true); > for (i=0; i<list.length; i++) { > print(dir1 + list[i]); > showProgress(i, list.length); > name = dir1 + list[i]; > > run("Bio-Formats Importer", "open=name view=Hyperstack stack_order=XYCZT > Color mode=Default split_channels"); > name1 = list[i]; > index = lastIndexOf(name1, "."); > if (index!=-1) name2 = substring(name1, 0, index); > path = dir2 + name2; > print(path); > > selectWindow(name1 + ' - C=0'); > run("Channels Tool..."); > run("Green"); > run("RGB Color"); > run("Brightness/Contrast..."); > setMinAndMax(8, 50); > call("ij.ImagePlus.setDefault16bitRange", 8); > selectWindow("B&C"); > run("Close"); > > selectWindow(name1 + ' - C=1'); > run("Channels Tool..."); > run("Blue"); > run("RGB Color"); > run("Brightness/Contrast..."); > setMinAndMax(8, 150); > call("ij.ImagePlus.setDefault16bitRange", 8); > selectWindow("B&C"); > run("Close"); > > selectWindow(name1 + ' - C=2'); > run("Channels Tool..."); > run("Yellow"); > run("RGB Color"); > run("Brightness/Contrast..."); > setMinAndMax(8, 35); > call("ij.ImagePlus.setDefault16bitRange", 8); > selectWindow("B&C"); > run("Close"); > > selectWindow(name1 + ' - C=3'); > run("Channels Tool..."); > run("Magenta"); > run("RGB Color"); > run("Brightness/Contrast..."); > setMinAndMax(8, 100); > call("ij.ImagePlus.setDefault16bitRange", 8); > selectWindow("B&C"); > run("Close"); > selectWindow("Channels"); > run("Close"); > > selectWindow(name1 + ' - C=0'); > green = getTitle; > selectWindow(name1 + ' - C=1'); > blue = getTitle; > selectWindow(name1 + ' - C=2'); > yellow = getTitle; > selectWindow(name1 + ' - C=3'); > magenta = getTitle; > > print(green); > print(blue); > print(yellow); > print(magenta); > > run("Merge Channels...", > "C2=green C3=blue C6=yellow C7=magenta keep"); > saveAs("jpeg", path + ".jpg"); > run("Close"); > } > > > > > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Merge-4-color-channels-tp5012905.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 |
Dear Volko Straub,
I tried as you suggested, but it shows the same dialog box... Any other suggestion? Thank you very much! Woo Jae |
Okay, I had a closer look at your code. There were another few problems.
First, when you use the Bio-Formats Importer the first time, you also need to combine the name variable with the rest of the string as I mentioned in my last email. I would also suggest that you put square brackets before and after the name variable to make sure that spaces in your file names are included. Second, all the commands are case sensitive. So, when you use color_mode to pass an arguement to the Bio-Formats Importer, you need to use color_mode, not Color_mode. The same applies to c2, c3 (not C2, C3) in the merge command. I modified the code below and it appears to be working. If you copy it, just be aware of any line breaks that might have been introduced by the email programme. Finally, the code seems rather complicated for what you want to achieve. If you simply want to assign specific colours to individual channels and adjust their brightness, why don't you just open the image with the Bio-Formats Importer without using the 'Split channel' option, but select 'Composite' as Color Mode? You could then use the 'Channels Tool - More' to assign specific colors to each channel. You should also be able to individually adjust the brightness/contrast for each channel. Hope this solves your issues, Volko Modified macro code: dir1 = getDirectory('Choose Directory '); list = getFileList(dir1); dir2 = dir1+"JPG"+File.separator; File.makeDirectory(dir2); setBatchMode(true); for (i=0; i<list.length; i++) { print(dir1 + list[i]); showProgress(i, list.length); name = dir1 + list[i]; run("Bio-Formats Importer", "open=["+name+"] view=Hyperstack stack_order=XYCZT color_mode=Default split_channels"); //name is variable; put [ ] around string variable to include potential spaces in filename; color_mode, not Colour mode name1 = list[i]; index = lastIndexOf(name1, "."); if (index!=-1) name2 = substring(name1, 0, index); path = dir2 + name2; print(path); selectWindow(name1 + ' - C=0'); run("Channels Tool..."); run("Green"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 50); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=1'); run("Channels Tool..."); run("Blue"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 150); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=2'); run("Channels Tool..."); run("Yellow"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 35); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=3'); run("Channels Tool..."); run("Magenta"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 100); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow("Channels"); run("Close"); selectWindow(name1 + ' - C=0'); green = getTitle; selectWindow(name1 + ' - C=1'); blue = getTitle; selectWindow(name1 + ' - C=2'); yellow = getTitle; selectWindow(name1 + ' - C=3'); magenta = getTitle; print(green); print(blue); print(yellow); print(magenta); run("Merge Channels...", "c2=["+green+"] c3=["+blue+"] c6=["+yellow+"] c7=["+magenta+"] keep"); // c2, not C2 (small c, commands are case sensitive); use [] around string variables to include spaces in filenames saveAs("jpeg", path + ".jpg"); run("Close"); } On 22/05/2015 15:25, woojkim wrote: > Dear Volko Straub, > > I tried as you suggested, but it shows the same dialog box... > Any other suggestion? > Thank you very much! > > Woo Jae > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Merge-4-color-channels-tp5012905p5012915.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 |
Do either of these help?
http://microscopynotes.com/imagej/macros/color_channels_macro_v100.txt http://microscopynotes.com/imagej/macros/composite-colors/user-selected-contrast-colors-names-save8bit.html _________________________________________ Michael Cammer, Optical Microscopy Specialist http://ocs.med.nyu.edu/microscopy http://microscopynotes.com/ Cell: (914) 309-3270 ________________________________________ From: ImageJ Interest Group [[hidden email]] on behalf of Volko Straub [[hidden email]] Sent: Saturday, May 23, 2015 2:09 AM To: [hidden email] Subject: Re: Merge 4 color channels Okay, I had a closer look at your code. There were another few problems. First, when you use the Bio-Formats Importer the first time, you also need to combine the name variable with the rest of the string as I mentioned in my last email. I would also suggest that you put square brackets before and after the name variable to make sure that spaces in your file names are included. Second, all the commands are case sensitive. So, when you use color_mode to pass an arguement to the Bio-Formats Importer, you need to use color_mode, not Color_mode. The same applies to c2, c3 (not C2, C3) in the merge command. I modified the code below and it appears to be working. If you copy it, just be aware of any line breaks that might have been introduced by the email programme. Finally, the code seems rather complicated for what you want to achieve. If you simply want to assign specific colours to individual channels and adjust their brightness, why don't you just open the image with the Bio-Formats Importer without using the 'Split channel' option, but select 'Composite' as Color Mode? You could then use the 'Channels Tool - More' to assign specific colors to each channel. You should also be able to individually adjust the brightness/contrast for each channel. Hope this solves your issues, Volko Modified macro code: dir1 = getDirectory('Choose Directory '); list = getFileList(dir1); dir2 = dir1+"JPG"+File.separator; File.makeDirectory(dir2); setBatchMode(true); for (i=0; i<list.length; i++) { print(dir1 + list[i]); showProgress(i, list.length); name = dir1 + list[i]; run("Bio-Formats Importer", "open=["+name+"] view=Hyperstack stack_order=XYCZT color_mode=Default split_channels"); //name is variable; put [ ] around string variable to include potential spaces in filename; color_mode, not Colour mode name1 = list[i]; index = lastIndexOf(name1, "."); if (index!=-1) name2 = substring(name1, 0, index); path = dir2 + name2; print(path); selectWindow(name1 + ' - C=0'); run("Channels Tool..."); run("Green"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 50); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=1'); run("Channels Tool..."); run("Blue"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 150); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=2'); run("Channels Tool..."); run("Yellow"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 35); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow(name1 + ' - C=3'); run("Channels Tool..."); run("Magenta"); run("RGB Color"); run("Brightness/Contrast..."); setMinAndMax(8, 100); call("ij.ImagePlus.setDefault16bitRange", 8); selectWindow("B&C"); run("Close"); selectWindow("Channels"); run("Close"); selectWindow(name1 + ' - C=0'); green = getTitle; selectWindow(name1 + ' - C=1'); blue = getTitle; selectWindow(name1 + ' - C=2'); yellow = getTitle; selectWindow(name1 + ' - C=3'); magenta = getTitle; print(green); print(blue); print(yellow); print(magenta); run("Merge Channels...", "c2=["+green+"] c3=["+blue+"] c6=["+yellow+"] c7=["+magenta+"] keep"); // c2, not C2 (small c, commands are case sensitive); use [] around string variables to include spaces in filenames saveAs("jpeg", path + ".jpg"); run("Close"); } On 22/05/2015 15:25, woojkim wrote: > Dear Volko Straub, > > I tried as you suggested, but it shows the same dialog box... > Any other suggestion? > Thank you very much! > > Woo Jae > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Merge-4-color-channels-tp5012905p5012915.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 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Straub, Volko A. (Dr.)
Dear Volko, It works! Great! Thank you very much! Woo Jae From: Volko Straub
[via ImageJ] [mailto:ml-node+[hidden email]] Okay, I had a closer look at your code. There were another few problems.
If you reply to this email, your message will be added to the discussion below: http://imagej.1557.x6.nabble.com/Merge-4-color-channels-tp5012905p5012923.html
To unsubscribe from Merge 4 color channels,
click here. The information in this e-mail is intended only for the person to whom it is |
Free forum by Nabble | Edit this page |