The display shows three ways of thresholding a gray-level image: using an interactive slider (top right), using the max-contour-contrast algorithm (middle right), or using the well-known Otsu min-variance algorithm (bottom right). Both algorithms work by examining image histograms.
The max-contrast algorithm calculates the average contrast along the boundary for each possible threshold value (center plot) and chooses a threshold at the greatest value (dashed blue line).
The Otsu algorithm sums the variance of the black pixels (less than the threshold) and the white pixels for each threshold value (bottom, center plot), and chooses a threshold at the minimum (dashed purple line).
Max-contour-contrast is a simple and useful little algorithm. Here is how it works:
The algorithm first histograms the 3×3 erosion and 3×3 dilation of the original image, then forms the cumulative sum of the difference of these two histograms. This array of values has the property that a pixel is counted whenever it is on a contour of the original image (why?). In other words, the algorithm counts the number of pixels on the contour, which is the contour length (left, center plot).
The algorithm finds the total contrast along a contour in a similar way, with a 2-input histogram, using the eroded and dilated images for bin values, and accumulating the corresponding contrast values. Dividing this array by the contour length gives the average contour contrast. Please see the code for details.
Unlike Otsu's, this algorithm can pick up small high-contrast areas. To dampen this effect slightly for the Demonstration, the contour contrast is multiplied by a weighting factor in the range 0-1, equal to the normalized contour length, raised to the power 0.25 (center plot).
R. M. Haralick and L. G. Shapiro, Computer and Robot Vision, vol. 1, Reading, MA: Addison-Wesley, 1992 pp. 14-23.
J. R. Parker, Algorithms for Image Processing and Computer Vision, New York: Wiley Computer Pub., 1997 pp. 116-128.