If your'e looking for a simple adaptive thresholding, then you have 2 parameters:
- C - criterion function - mean, median, max, min, or other
- WS - neighborhood/window size (WS.x, WS.y) - 3X3, 5X5, etc...
Now you need to create an image of the adaptive threshold - AT.
To generate AT - For the whole image you apply filtering based on your criterion.
For each pixel p - AT(p) = the median/mean/etc.. of the local neighborhood:
[(p-WS.x, p-WS.y), (p+WS.x, p+WS.y)]
if criterion is median - it is median filter (non linear) -
AT = medfilter (IM, WS)
else if the criterion is averae - average filter (linear) using a kernel (the average kernel)
AV = createAverageKernel (WS)
AT = IM * AV ("*" is a convolution)
There may be a special filter, for any other kind of criterion
Then, you can apply the thresholding of the image IM, using AT.
B_IM (p) = (IM (p) > AT (p)) ? 1 : 0
As for the adaptive thresholding algorithm in the question - it is a histogram based iterative algorithm to compute the threshold.
However, the histogram can be applied on the whole image (global), or on a neighborhood of the pixel (local).
So this algorithm can be applied either global or local. So in your case, this is the criterion function. It's just a more sophisticated function then media, mean, etc..
For the algorithm:
First you initialize theta (the threshold) with uniform value - the weighted average of the histogram.
While condition - you iterate until the former threshold equals the current threshold.
In the loop - .
Define lower value and upper value. Lower value is the weighted average of the histogram part that is lower than the current threshold value, and in similar manner the upper value.
The current theta value is the average between the lower and the upper weighted average