Sequences Separation for Color Fluorescent Overlays

I have folders of images (hundreds in total). I took the images in the same sequences of bright field followed by GFP (green), TxRed (red), DAPI (blue). I would like to use Fiji to import the sequence or stack, etc. The images were all taken using a black and white camera. As such, I want to find a way to automate splitting the files and overlaying each set.

So, I would like to import a folder of images; eg: 100 images that represent 25 regions that I imaged with 4 images to overlay colored images on the bright field, resulting in 25 final composite images. I would like to be able to adjust the thresholding settings for each color channel overlay as needed to account for some imaging variations.

I’ve search around after reading the new posting guidelines and have read on using hyperstacks, concatenate but cannot seem to get it sorted.

Appreciate the help greatly!

-Phillip

Hi @pfry,

welcome to the forum!

The following macro should do the job you need.
When you run it,

  1. it will ask you for the folder in which your image sequence is located
  2. then it imports the image sequence and converts it to the 4 channel hyperstack
  3. then it extracts each channel combination one by one
  4. after each it asks you to do your adjustment
  5. one you press ok, it will save the new composite in a previously created folder inside your input folder
  6. it will close all images after processing
dir = getDir("Choose a directory");
fileList = getFileList(dir);
imageCount = fileList.length;

outputDirName = dir + "Converted";
outputDirPath = outputDirName + File.separator;

File.makeDirectory(outputDirName);

run("Image Sequence...", "open=[" + dir + fileList[0] + "] sort");
imageSequence = getTitle();

originalImageName = getTitle();
run("Stack Splitter", "number=4");
run("Merge Channels...", "c1=[stk_0003_" + originalImageName + "] c2=[stk_0002_" + originalImageName + "] c3=[stk_0004_" + originalImageName + "] c4=[stk_0001_" + originalImageName + "] create");
compositeImageName = getTitle();

slices = nSlices;
for (i = 1; i <= slices; i+=4) {
	selectWindow(compositeImageName);
	setSlice(i);
	run("Reduce Dimensionality...", "channels keep");
	waitForUser("Please adjust your images and press Ok");
	imageName = compositeImageName + "_" + ((i-1)/4);
	saveAs("tiff", outputDirPath + imageName);
	close(imageName + ".tif");
}
close(compositeImageName);
close(imageSequence);

Test and see if it works for you.
Hope it helps or gives you an idea where to start.

1 Like

Thank you so much for your help. I will try this today.

@biovoxxel

Thanks again. I am having one issue and have tried to explore options without any luck.
Image were split and I was prompted for edits as intended. I did notice that the black and white / ch 4 overlay was not matching up with the RGB channel overlays. I created a folder with simply 4 images (1 set) and ran the macro. The resulting error is attached. It seems as if it is having issues assigning the correct file to channel.

Debug.csv (773 Bytes)

Reference files if that helps:
Archive.zip (14.1 MB)

@biovoxxel

Just to add a note:
I’ve tried changing the syntax of the filename section without success. Was thinking the quotation characters would possibly fix it (seeing the error maybe wasn’t pulling the proper filename and folder instead?). Will keep toying with it.

I will check by tomorrow and get back to you. Sorry for the delay

@biovoxxel Thanks! No rush. I’m just thankful for the help.

Actually. I cannot reproduce the error. It just doesn’t work if there are only 4 images in the folder, but as soon as you have a bunch of images it works as intended.

I adjusted the macro, so that it should distinguish between the two conditions.

var originalImageName = "";
var compositeImageName = "";
var = imageSequence = "";

dir = getDir("Choose a directory");
fileList = getFileList(dir);
imageCount = fileList.length;

outputDirName = dir + "Converted";
outputDirPath = outputDirName + File.separator;

File.makeDirectory(outputDirName);

if (fileList.length > 4 && fileList.length%4 == 0) {
	run("Image Sequence...", "open=[" + dir + fileList[0] + "] sort");
	imageSequence = getTitle();

	originalImageName = getTitle();
	run("Stack Splitter", "number=4");
	run("Merge Channels...", "c1=[stk_0003_" + originalImageName + "] c2=[stk_0002_" + originalImageName + "] c3=[stk_0004_" + originalImageName + "] c4=[stk_0001_" + originalImageName + "] create");
	compositeImageName = getTitle();

	slices = nSlices;
	for (i = 1; i <= slices; i+=4) {
		selectWindow(compositeImageName);
		setSlice(i);
		run("Reduce Dimensionality...", "channels keep");
		waitForUser("Please adjust your images and press Ok");
		imageName = compositeImageName + "_" + ((i-1)/4);
		saveAs("tiff", outputDirPath + imageName);
		close(imageName + ".tif");
	}

	close(compositeImageName);
	close(imageSequence);
	
} else {
	for (i = 0; i < 4; i++) {
		open(dir + fileList[i]);
	}
	run("Merge Channels...", "c1=[" + fileList[2] + "] c2=[" + fileList[1] + "] c3=[" + fileList[3] + "] c4=[" + fileList[0] + "] create");
	compositeImageName = getTitle();
	waitForUser("Please adjust your images and press Ok");
	saveAs("tiff", outputDirPath + compositeImageName);
	close(compositeImageName + ".tif");
}

If the channels are not sorted correctly in your case, you can adjust the numbering in the run("Merge Channels...", ....... command. Premise is, that the sequence of your images is always the same.

1 Like