Batch processing plug in

plugin
batch-processing
fiji
macro
Tags: #<Tag:0x00007fd542c176d0> #<Tag:0x00007fd542c17568> #<Tag:0x00007fd542c17428> #<Tag:0x00007fd542c172e8>

#1

Hi there

I am fairly new to ImageJ/Fiji and am wondering if anyone knows of a plugin which will batch process as per the below

  1. Threshold image based on pixel intensity
  2. Measure average pixel intensity of each image for comparison

Thanks in advance!
Natasha


#2

Hi Natasha,

you can use the plugin Batch_Process in Process :arrow_forward: Batch :arrow_forward: Macro… , to process a folder of images.

Using the macro recorder you can find the macro language command corresponding to:

  • “Threshold image based on pixel intensity” using Auto_Threshold
  • “Measure intensity”, set measurements and use function measure.

All this action are Macro recordable, so you could use the corresponding text command in the Batch_Process.

I recommand you to have a look to this previous topics :

Cheers,

Romain


#3

Hi Romain,

thanks for your response to Natasha’s post. I looked at your previous topics and it suggests that Auto_Threshold has issues with bias. We were planning on setting a threshold based on a positive control and applying that across a series of images from one experiment. Since we will have hundreds to a thousand images, we are looking to batch process as much as possible. So is it possible to record a macro that sets a particular threshold and then apply this to the folder of images? Or is using Auto_Threshold the best option in this scenario?

regards
Paul.


#4

Hi Paul,
Maybe something is not clear enough in the previous topic, it’s not that “Auto_Threshold has issues with bias”, it’s more that you can introduce bias if you use poorly the Auto_Threshold.
What @oburri and I suggest is that:
“If you want to “Measure average pixel intensity” of a channel you should not use the same channel to define the area of measurement.

That is why you should use an “independent” (i.e. that is NOT sensitive to your test) channel.
Auto_treshold is just a start to help you choose the Algorithm you would like to use.

You’ll find below some code that you can copy and run in the script editor.

Regarding, Automatic Thresholding using Algorithm, to detect Area.

There are 2-3 families of algorithm (see code below)

// clear the environment
run("Close All");
roiManager("Reset");

//open an image
run("Fly Brain (1MB)");
// make it composite, duplicate one slice and get information
run("Make Composite");
run("Duplicate...", "duplicate channels=1 slices=27");
getDimensions(width, height, channels, slices, frames);

// here we run the Auto_Threshold 
run("Auto Threshold", "method=[Try all] white");
getDimensions(output_Width, output_Height, output_channels, output_slices,output_frames);

// I will highlith the categories with some ROIs of different colors
xSize = 258;
ySize = 274;
// so I define the categories and their colors
arrayCatNbr = newArray(1,2,3,1,  2,1,1,3  ,1,2,1,2  ,1,2,2,1)
arrayRoiColor = newArray("50FFFF00", "5000FF00", "50FF00FF");
// adding ROIs
i = 0;	
for (x = 0 ; x < output_Width ; x+=xSize){
	for (y = 0 ; y < output_Height ; y+=ySize){
		makeRectangle(x, y, xSize, ySize);
		Roi.setName("Cat-"+arrayCatNbr[i]);
		Roi.setFillColor(arrayRoiColor[arrayCatNbr[i]-1]);
		roiManager("Add");
		i++;
	}
}
// Sort the ROi and Tile the images
roiManager("Sort");
run("Tile");
roiManager("Show All");

Using the recorder you can generate code using the Threshold of your choice, i.e. Default, Otus… like in the code below

// clear the environment
run("Close All");
roiManager("Reset");

//open an image
run("Fly Brain (1MB)");
// make it composite, duplicate one slice and get information
run("Make Composite");
run("Duplicate...", "duplicate channels=1 slices=27");
getDimensions(width, height, channels, slices, frames);

threshold_List = getList("threshold.methods");

for (i = 0 ; i < lengthOf(threshold_List) ; i++){
	currentThresholdName = threshold_List[i];
	setAutoThreshold(currentThresholdName+" dark");
	run("Create Selection");
	if (selectionType >= 0){
		Roi.setName(threshold_List[i]);
		roiManager("Add");
	}
}

roiManager("Show None");
waitForUser("Select ROIs in the roiManager to look at the different results");

What we recommend is to select one algorithm, that “works well” for most of your images.
Here, “works well” means if you output the result of the thresholding (through images or ROIs) on several images from BOTH conditions you want to compare (10 of each for example) and this output should be in good accordance to what a human would define manually.

NB-1: One need to find an algorythm that works for the two conditions you want to compare.
NB-2: One can end up with different algorithms that are working fine with the dataset (it’s a good thing actually, you can then run them all and see if the results are consistent)

Then you can apply on your whole dataset.

Image quality is key for image processing and you should avoid saturation, see code below.

// clear the environment
run("Close All");
run("Clear Results")
roiManager("Reset");
// Set the measurements of intererest
run("Set Measurements...", "area mean standard min limit display redirect=None decimal=3");


run("Gel (105K)");
rename("Original");

waitForUser("There are several factors than can create variability in acquisition,"+
			"\nthat will make grey levels a little bit different"+
			"\nfrom one image to another even if the object is the same."
			);
			
run("Gel (105K)");
rename("Lower");
run("Add...", "value=-8");

run("Gel (105K)");
rename("Higher");
run("Add...", "value=25");

run("Tile");

waitForUser("Let's now look at the corresponding histograms");

image_Nbr = nImages;
for (i = 1 ; i <= image_Nbr ; i++){
	selectImage(i);
	run("Histogram");
}

run("Tile");
waitForUser("We can see that 'min' and 'max' are different for the 3 images"+
			"\nBUT the histograms are equivalent."+
			"\nLet's apply an arbitrary threshold on the images, like 81 ! "
			);

for (i = 1 ; i <= image_Nbr ; i++){
	selectImage(i);
	setThreshold(0, 81);
	run("Measure");
}	

waitForUser("We can see that '81' is maybe note the 'right' value for all the images"+
			"\nLet's apply the same algorithm threshold on the images (Shanbhag)"
			);

for (i = 1 ; i <= image_Nbr ; i++){
	selectImage(i);
	setAutoThreshold("Shanbhag");
	run("Measure");
}	
	
waitForUser("We can now realize that the Area is the same for all the images"+
			"\nwhile other parameters (like the mean) can show difference."+
			"\nThat's why we like so much 'Auto_Thresholding'"+
			"\nit's able to compensate for small fluctuations between images"+
			"\nand still robust results."+
			"\n..."+
			"\nLet's see now what happen if you work with saturated images."
			);

selectImage("Lower");
setMinAndMax(50, 255);
run("Apply LUT");

selectImage("Higher");
setMinAndMax(0, 200);
run("Apply LUT");

selectImage("Histogram of Lower");
close();
selectImage("Histogram of Higher");
close();

for (i = 2 ; i <= image_Nbr ; i++){
	selectImage(i);
	run("Histogram");
}

run("Tile");

waitForUser("With 'some' saturation we see that the histograms are dramatically affected"+
			"\nLet's check the result of thresholding."
			);
			
for (i = 1 ; i <= image_Nbr ; i++){
	selectImage(i);
	setAutoThreshold("Shanbhag");
	run("Measure");
}

waitForUser("Nothing has much more sense anymore...");


I hope it’s a bit more clear now, but do not hesitate if you still have questions.

Cheers,

Romain


#5

Thanks Romain,

That has been very helpful. What we are doing is measuring the level of autoantibodies in serum that are directed against neuronal antigens like the NMDA receptor (NMDAR). So we have biochips that have HEK cells transfected with the NMDAR and so there should be relatively constant proportion of NMDAR+ HEK cells on each biochip. We have a positive control for the NMDAR that we would run for each experiment. Then in the same experiment we would test serum from patients to see how many have autoantibodies to the NMDAR. We will use our own observations to make a call on whether it is positive or not down the microscope. But we thought it would also be useful to set the threshold using the NMDAR positive control and apply that to the patient samples to get a measure of relative fluorescence intensity. That way we could compare the patient samples back to control and gain a measure of how much fluorescence is there relative to the control. That is, does the patient have high or low levels of autoantibodies.

We weren’t planning on using DAPI since some of the patients may have auto-nuclear antibodies and thought this might limit our ability to detect this. But from what I’ve described do you still recommend that we don’t set the threshold on the green fluorescence and that we use a marker such as DAPI to set the threshold on another channel? We already know that a proportion of our cells won’t have any fluorescence, we want to know how brightly labelled the ones that do are? We also were not planning on setting ROIs, just after a mean measure of fluorescence intensity across one image for the serum samples.

regards Paul.


#6

Hi Paul,

So we have biochips that have HEK cells transfected with the NMDAR and so there should be relatively constant proportion of NMDAR+ HEK cells on each biochip

From one field to another, you can have more or less cells, and with various amount of expression (if they are transiently transfected with the expression vector. Or are you using a cell line selected for a stable expression?)

But we thought it would also be useful to set the threshold using the NMDAR positive control and apply that to the patient samples to get a measure of relative fluorescence intensity.

You should also consider having a negative control (what is the minimal level you can detect from background fluorescence / cells auto fluorescence)

That way we could compare the patient samples back to control and gain a measure of how much fluorescence is there relative to the control.

So you plan to make a titration of your control? Diluting your antibodies and get a calibration curve?

We weren’t planning on using DAPI[…]

I don’t say you have to use DAPI. I recommend that you have another staining that allow you to perform your measurement accurately.
In your case, you could measure intensity of cells that express the NMDAR.
Is the protein expressed with a fluorescent protein tag? Or could you use a marker of successful transfection?

We already know that a proportion of our cells won’t have any fluorescence, we want to know how brightly labelled the ones that do are?

This exactly the bias, I tried to explain previously.

We also were not planning on setting ROIs, just after a mean measure of fluorescence intensity across one image for the serum samples.

Doing so, you will have variability in your results due to inhomogeneity of cells seeding and expression of the receptor.

I do not say you can’t do what you plan to do… it just that your test will be less accurate (having more false positive and/or false negative)

Best of luck!

Romain