Some considerations regarding a very classic method : Thresholding
= Why bothering using **Threshold** ? =
Because you would like to identify different kind of objects within your image
= Should I fixe a value an apply the same to all my images?=
You can run the code below in ImageJ and get why it may not be such a good idea.
```lang = javascript
// 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...");
```
= How do I choose an Algorythm ?=
You can run the code below in ImageJ and know more about the Automatic Method that exist to determine a threshold.
```lang = javascript
// 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");
selectImage("Montage")
roiManager("Show All");
```
or using as an ouput a ROI :
``` lang=javascript
// 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");
```