63
63
64
64
# ---------------------------------------------------------------------------------------------------
65
65
"""
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
67
67
68
68
Convert an image to a binary image (black-and-white) using a threshold.
69
69
70
70
### Args
71
71
- `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.
74
74
75
75
### Kwargs
76
76
- 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=??)`.
77
80
- `revert`: If `true`, values below the threshold are set to 255, and values above the threshold are set to 0.
78
81
79
82
### Return
@@ -88,13 +91,14 @@ grdimage(I, figsize=6)
88
91
grdimage!(Ibw, figsize=6, xshift=6.1, show=true)
89
92
```
90
93
"""
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
93
97
img = zeros (UInt8, size (I, 1 ), size (I, 2 ))
94
98
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 )
96
100
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 )
98
102
end
99
103
img[t] .= 255
100
104
return mat2img (img, I)
0 commit comments