Create macros to split, edit, and merge channels

macro
imagej
Tags: #<Tag:0x00007fb878310a78> #<Tag:0x00007fb878310938>

#1

I’m trying to create a macro to split channels from a file which I then set the brightness and contrast on two of them and merge them back. I then save the file as a tiff. The following is what I have so far using the process template. I’m very new to this and could definitely use some help.

function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list)
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder("" + input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
title = getTitle();	
run("Split Channels"); 
three = "C3-" +title; 
two = "C2-" +title; 
one = "C1-" + title;
selectWindow("C1-"+title); 
setMinAndMax(100, 375);
selectWindow("C2-"+title);
setMinAndMax(150, 1500);
run("Merge Channels...", "c1=["+one+"] c2=["+two+"] c3= ["+three+'] keep");
saveAs("tiff",path+Title);
}

#2

Hi @cycy888,

welcome to the forum.

The following macro code should basically do what you want to. The frame is taken from the ListFilesRecursively example from the Built-in macro functions page.

Hope it helps to get a little more insight and as a kick-start for using functions and macros in ImageJ.


input = getDirectory("Choose an input directory");
output = getDirectory("Choose an output directory");

processFolder(input); 

function processFolder(dir) {
	list = getFileList(dir);
	for (i=0; i<list.length; i++) {
    	        if(endsWith(list[i], ".tif")) { //add the file ending for your images
    		    processFile(dir, output, list[i]);
		} else if(endsWith(list[i], "/") && !matches(output, ".*" + substring(list[i], 0, lengthOf(list[i])-1) + ".*")) {
   		    //if the file encountered is a subfolder, go inside and run the whole process in the subfolder
    		    processFolder(""+dir+list[i]);
		} else {
    		    //if the file encountered is not an image nor a folder just print the name in the log window
       		    print(dir + list[i]);
		}
    }
}


function processFile(inputFolder, output, file) {
	open(inputFolder + file);
	title = getTitle();
	run("Split Channels");
	three = "C3-" +title;
	two = "C2-" +title;
	one = "C1-" + title;
	selectWindow("C1-"+title);
	setMinAndMax(100, 375);
	selectWindow("C2-"+title);
	setMinAndMax(150, 1500);
	run("Merge Channels...", "c1=["+one+"] c2=["+two+"] c3=["+three+"]");
	saveAs("tiff", output + file);
	close(file);
}

#3

Thank you for the help, but for some reason when I run it I can no longer scroll though all through the three channels to ensure it worked properly. I switched the file type to .dv as needed but after its all set it only opens up as a single image rather than three merged stacks like when I do it manually. I also tried using stack to images instead of split channels and the same issue occurs.


#4

Actually it’s necessary to include create at the end of the merge channels otherwise it won’t preserve the 16 bit of the image.


#5

Well, since I did not know what kind of images, image type and format you use, I could not define it that specifically. I am not familiar with the .dv image format and its modalities. But it seems you solved the rest.