Gray image thresholding using the Triangle Method
28 Jun 2010
12 Jul 2010)
Computes a threshold the Triangle method.
% Triangle algorithm
% This technique is due to Zack (Zack GW, Rogers WE, Latt SA (1977),
% "Automatic measurement of sister chromatid exchange frequency",
% J. Histochem. Cytochem. 25 (7): 74153, )
% A line is constructed between the maximum of the histogram at
% (b) and the lowest (or highest depending on context) value (a) in the
% histogram. The distance L normal to the line and between the line and
% the histogram h[b] is computed for all values from a to b. The level
% where the distance between the histogram and the line is maximal is the
% threshold value (level). This technique is particularly effective
% when the object pixels produce a weak peak in the histogram.
% Use Triangle approach to compute threshold (level) based on a
% 1D histogram (lehisto). num_bins levels gray image.
% lehisto : histogram of the gray level image
% num_bins: number of bins (e.g. gray levels)
% level : threshold value in the range [0 1];
% Dr B. Panneton, June, 2010
% Agriculture and Agri-Food Canada
% St-Jean-sur-Richelieu, Qc, Canad
% Find maximum of histogram and its location along the x axis
xmax=round(mean(xmax)); %can have more than a single value!
% Find location of first and last non-zero values.
% Values<h/10000 are considered zeros.
% Pick side as side with longer tail. Assume one tail is longer.
if rspan>lspan % then flip lehisto
% Compute parameters of the straight line from first non-zero to peak
% To simplify, shift x axis by a (bin number axis)
% Compute distances
% Obtain threshold as the location of maximum L.
% Flip back if necessary