Macro for batch processing of histograms

Hi everyone,

I’m new to imagej, so I kindly ask for your help.

I am trying to create a macro to process thousands of images in one folder. I can do it for every single image but it takes me several days to do it. And now I already spent several days to make a macro, unfortunately without success.

These are the steps I want to do:

  1. Open all images (tif) in a folder
  2. Split the channels
    (3. Close green and blue channels)
  3. Create a histogram of the red channels
  4. Make ONE excel sheet containing two columns: 1. Name of the file, 2. Sum of pixels from greyscale 100 to 255
  5. Save the excel sheet

Can anybody help me with making this macro/batch?

Thanks in advance

Lisa

Hi @Lisa_no_pro,

Welcome.

Did you use the Plugin>Macro>Record to record the individual steps of your procedure?

Assuming all your images have the same size:
Did you import Image Sequence to open all images in a folder at once?
Possibly use a virtual stack if you have very many images?

Did you creat a newArray() of titles,
Which you filled with titleArray[i]=getInfo(“image.subtitle”); for every slice, to be used later to create the report?

Did you color>Split Channels to separate red, green and blue?
Then create a for loop that loops over every slice, takes histograms using getHistogram(values, counts, nBins[, histMin, histMax])
Then loop from 100 to 255 over the values and sums the counts

Finally print() the results to a table and saveAs(“results”, filePath); the table. A text file can be imported in Excel.

Tell us how far you get and/or where you have different starting points, so we can help you further.

Hi @eljonco,

thank you very, very much for your quick response.

Yes, I tried but I failed to generalize the macro for my folder as I am not very experienced. That is why I cannot really cope with your hints, I’m sorry.

Yes, all the images have the same size. I have 3000 images with 4MB.

Yes, I did that.

Would you mind to write a macro so that I can try to fit it to my situations?

Ah, OK, one step at a time then.

  1. In ImageJ1 (and in FIJI) you can record the commands you perform. To that end, you activate the macro recorder. Plugins>Macro>Record...
  2. All images being equal, you can open all images in a folder in one command. File> Import >Image Sequence. Then indicate where the images are. In the next step, the Sequence Options dialog, the number of images is already shown. Also the amount of memory needed is calculated.
  3. Loading all images in memory likely asks for 3000(images)x4(MB)x3(colours)=36 GB of memory, which might be a bit large.
  4. In the dialog you can tick the Use virtual stack box. Not all commands are possible using Virtual Stacks. So this might or might not prevent step 5, but it helps to accommodate very large data sets and is worth a try. So tick and try to open the images by pressing OK.
  5. With the now-open image, split the stack of RGB into three stacks, R, G and B by using Image> Color> Split channels
  6. If this succeeds, close the green and blue stacks.

Do you get any complaints from ImageJ so far? If step 5 fails, we go into programming (or you can look for answers concerning `Process> Batch> Macro…’)

Edit: what is the file extension of your images?

Thank you very much, I just made a try with 12 tif-images and Steps 1-6 worked out (I used “Use virtual stack”). After Step 6 I made histograms of the 12 images, clicked List, and saved as a csv. I guess, unfortunately the results were presented altogether like that:

Is it possible to separate the histograms and count grey scales from 60-255? I was hoping for a summary similiar to this:

beispiel

Is it possible?

Best regards,
Lisa

Here is a macro that will work irrespective of number of images, number of folders (within folders) and amount of memory. Within reason, of course.

See if your images are correctly opened as RGB, transformed to RGB stack and then measured.

If you have seen enough windows flashing by, you can change the false in the third line to true. This will hide all opening and closing of the images and just perform the calculations.

var lowBin=60;//lowest bin in sum
var highBin=255;//highest bin in sum
	speedup = false;fileType="tif";
	startPath = getDirectory("Choose a Directory");
	windowName = "Histogram Results";f = "["+windowName+"]";
	while(isOpen(windowName)){close(windowName);}
	run("New... ", "name=["+windowName+"] type=Table");
	print(f,"File Name"+"\t"+"Histogram sum (bin "+lowBin+"-"+highBin+")\n");//header line for spreadsheet
	setBatchMode(speedup);
	nFilesProcessed = processFolder(startPath,fileType);
	selectWindow(windowName);run("Text...", "save=["+startPath+windowName+".txt]");
	setBatchMode(!speedup);
	showMessage("Finished "+nFilesProcessed+" histograms");

function processFolder(inPath,extension){
	outPath=inPath;processedFiles=0;
	arrayOfNames = getFileList( inPath );
	for(i=0;i<arrayOfNames.length;i++){
		thisName=arrayOfNames[i];//shows up in Debug window
		if(File.isDirectory(inPath + arrayOfNames[i])){
			processedFiles=processedFiles+processFolder(inPath + arrayOfNames[i] );
		}
	}
	for(i=0;i<arrayOfNames.length;i++){
		thisName = arrayOfNames[i];
		if(endsWith(thisName, extension)){
			processFile(thisName,inPath);
			processedFiles++;
		}
	}
	return processedFiles;
}
function processFile(imgName,path){
	close("*");
	open(path+imgName);
	run("RGB Color");//might be unnecessary
	run("RGB Stack");
	setSlice(2);
	getHistogram(values, counts, 256);
	sum=0;	close("*");

	for(bucket=100;bucket<256;bucket++){
		sum += counts[bucket];
	}
	print(f,imgName+"\t"+sum+"\n");
}

Hi eljonco,

thank you very much for for your very detailed help. It took some time for me to get into it but now I processed around 3000 images and saved lots of time :+1:t3:

Best regards,
Lisa