machinevisiontoolbox.Image.threshold
- Image.threshold(t=None, opt='binary')
Image threshold
- Parameters:
t (scalar, str) – threshold value
option (str, optional) – threshold option, defaults to ‘binary’
- Returns:
thresholded image
- Return type:
Image
Apply a threshold
t
to the image. Various thresholding options are supported:Option
Function
'binary'
\(Y_{u,v} = \left\{ \begin{array}{l} m \mbox{, if } X_{u,v} > t \\ 0 \mbox{, otherwise} \end{array} \right.\)
'binary_inv'
\(Y_{u,v} = \left\{ \begin{array}{l} 0 \mbox{, if } X_{u,v} > t \\ m \mbox{, otherwise} \end{array} \right.\)
'truncate'
\(Y_{u,v} = \left\{ \begin{array}{l} t \mbox{, if } X_{u,v} > t \\ X_{u,v} \mbox{, otherwise} \end{array} \right.\)
'tozero'
\(Y_{u,v} = \left\{ \begin{array}{l} X_{u,v} \mbox{, if } X_{u,v} > t \\ 0 \mbox{, otherwise} \end{array} \right.\)
'tozero_inv'
\(Y_{u,v} = \left\{ \begin{array}{l} 0 \mbox{, if } X_{u,v} > t \\ X_{u,v} \mbox{, otherwise} \end{array} \right.\)
where \(m\) is the maximum value of the image datatype.
If threshold
t
is a string then the threshold is determined automatically:threshold
algorithm
'otsu'
Otsu’s method finds the threshold that minimizes the within-class variance. This technique is effective for a bimodal greyscale histogram.
The triangle method constructs a line between the histogram peak and the farthest end of the histogram. The threshold is the point of maximum distance between the line and the histogram. This technique is effective when the object pixels produce a weak peak in the histogram.
'triangle'
Example:
>>> from machinevisiontoolbox import Image >>> img = Image([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> img.threshold(5).image array([[ 0, 0, 0], [ 0, 0, 255], [255, 255, 255]], dtype=uint8)
- Note:
The threshold is applied to all color planes
If threshold is ‘otsu’ or ‘triangle’ the image must be greyscale, and the computed threshold is also returned.
- References:
A Threshold Selection Method from Gray-Level Histograms, N. Otsu. IEEE Trans. Systems, Man and Cybernetics Vol SMC-9(1), Jan 1979, pp 62-66.
Automatic measurement of sister chromatid exchange frequency” Zack (Zack GW, Rogers WE, Latt SA (1977), J. Histochem. Cytochem. 25 (7): 741–53.
Robotics, Vision & Control for Python, Section 12.1.1, P. Corke, Springer 2023.
- Seealso:
threshold_interactive
threshold_adaptive_
otsu
opencv.threshold