6363
6464# ---------------------------------------------------------------------------------------------------
6565"""
66- Ibw = binarize(I::GMTimage, threshold::Int=0; band=1, revert=false) -> GMTimage
66+ Ibw = binarize(I::GMTimage, threshold::Int=0; band=1, , threshold::Int=0, revert=false) -> GMTimage
6767
6868Convert an image to a binary image (black-and-white) using a threshold.
6969
7070### Args
7171- `I::GMTimage`: input image of type UInt8.
72- - `threshold::Int`: A number in the range [0 255]. If the default (`nothing` ) is maintained,
73- the threshold is computed using the ``isodata`` method.
72+ - `threshold::Int`: A number in the range [0 255]. If the default (0 ) is kept and the keyword
73+ `threshold` is not used, then the threshold is computed using the ``isodata`` method.
7474
7575### Kwargs
7676- band: If the `I` image has more than one band, use `band` to specify which one to binarize.
77+ By default the first band is used.
78+ - `threshold`: Alternative keyword argument to the positional `threshold` argument. Meaning, one can either
79+ use the `binarize(I, =??)` or `binarize(I, threshold=??)`.
7780- `revert`: If `true`, values below the threshold are set to 255, and values above the threshold are set to 0.
7881
7982### Return
@@ -88,13 +91,14 @@ grdimage(I, figsize=6)
8891grdimage!(Ibw, figsize=6, xshift=6.1, show=true)
8992```
9093"""
91- function binarize (I:: GMTimage , threshold:: Int = 0 ; band= 1 , revert= false ):: GMTimage
92- thresh = (threshold == 0 ) ? isodata (I, band= band) : threshold
94+ function binarize (I:: GMTimage , thresh:: Int = 0 ; band= 1 , threshold:: Int = 0 , revert= false ):: GMTimage
95+ (thresh == 0 && threshold > 0 ) && (thresh = threshold)
96+ _tresh = (thresh == 0 ) ? isodata (I, band= band) : thresh
9397 img = zeros (UInt8, size (I, 1 ), size (I, 2 ))
9498 if revert
95- t = I. layout[3 ] == ' B' ? (view (I. image, :, :, band) .< thresh ) : (slicecube (I, band). image .< thresh )
99+ t = I. layout[3 ] == ' B' ? (view (I. image, :, :, band) .< _tresh ) : (slicecube (I, band). image .< _tresh )
96100 else
97- t = I. layout[3 ] == ' B' ? (view (I. image, :, :, band) .> thresh ) : (slicecube (I, band). image .> thresh )
101+ t = I. layout[3 ] == ' B' ? (view (I. image, :, :, band) .> _tresh ) : (slicecube (I, band). image .> _tresh )
98102 end
99103 img[t] .= 255
100104 return mat2img (img, I)
0 commit comments