Skip to content

Commit 63d0953

Browse files
authored
Merge pull request #1988 from GenericMappingTools/fix-binarize
Fix a binarize hole where a TRPa image with 1 band was set to cutcube.
2 parents 0afacd3 + 1a00e49 commit 63d0953

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/img_funs.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,20 @@ function binarize(I::GMTimage, thresh::Int=0; band::Int=1, threshold::Int=0, rev
105105
(thresh == 0 && threshold > 0) && (thresh = threshold)
106106
_tresh = (thresh == 0) ? isodata(I, band=band) : thresh
107107
img = bool ? zeros(Bool, size(I, 1), size(I, 2)) : zeros(UInt8, size(I, 1), size(I, 2))
108-
if revert
109-
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .<= _tresh) : (slicecube(I, band).image .<= _tresh)
110-
else
111-
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .>= _tresh) : (slicecube(I, band).image .>= _tresh)
112-
end
108+
fun = revert ? (x -> x <= _tresh) : (x -> x >= _tresh)
109+
t = size(I, 3) == 1 ? (I.image .|> fun) : I.layout[3] == 'B' ? (view(I.image, :, :, band) .|> fun) : (slicecube(I, band).image .|> fun)
113110
img[t] .= bool ? true : 255
114111
return mat2img(img, I)
115112
end
116113
function binarize(I::GMTimage, thresh::Vector{Int}; band::Int=1, revert::Bool=false,
117114
bool::Bool=false)::Union{GMTimage{Bool, 2}, GMTimage{UInt8, 2}}
118115
@assert length(thresh) == 2 && thresh[1] > 0 && thresh[2] < 255 "The threshold vector must have two elements in the range ]0 255["
119116
img = bool ? zeros(Bool, size(I, 1), size(I, 2)) : zeros(UInt8, size(I, 1), size(I, 2))
120-
t = I.layout[3] == 'B' ? thresh[1] .<= (view(I.image, :, :, band) .<= thresh[2]) : (thresh[1] .<= slicecube(I, band).image .<= thresh[2])
117+
if (size(I, 3) == 1)
118+
t = thresh[1] .<= I.image .<= thresh[2]
119+
else
120+
t = I.layout[3] == 'B' ? thresh[1] .<= (view(I.image, :, :, band) .<= thresh[2]) : (thresh[1] .<= slicecube(I, band).image .<= thresh[2])
121+
end
121122
revert && for k = 1:numel(t) t[k] = !t[k] end
122123
img[t] .= bool ? true : 255
123124
return mat2img(img, I)

src/utils_types.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,8 @@ end
14321432
function mat2img(mat::Union{AbstractMatrix{UInt16},AbstractArray{UInt16,3}}; x=Float64[], y=Float64[], v=Float64[], hdr=Float64[], proj4::String="", wkt::String="", img8::AbstractMatrix{UInt8}=Matrix{UInt8}(undef,0,0), kw...)
14331433
_mat2img_u16(mat, vec(Float64.(x)), vec(Float64.(y)), vec(Float64.(v)), vec(Float64.(hdr)), proj4, wkt, img8, KW(kw))
14341434
end
1435-
function _mat2img_u16(@nospecialize(mat), x::Vector{Float64}, y::Vector{Float64}, v::Vector{Float64}, hdr::Vector{Float64}, proj4::String, wkt::String, @nospecialize(img8), d::Dict{Symbol,Any})
1435+
function _mat2img_u16(@nospecialize(mat), x::Vector{Float64}, y::Vector{Float64}, v::Vector{Float64},
1436+
hdr::Vector{Float64}, proj4::String, wkt::String, @nospecialize(img8), d::Dict{Symbol,Any})
14361437
# Take an array of UInt16 and scale it down to UInt8. Input can be 2D or 3D.
14371438
# If the kw variable 'stretch' is used, we stretch the intervals in 'stretch' to [0 255].
14381439
# Use this option to stretch the image histogram.
@@ -1453,8 +1454,12 @@ function _mat2img_u16(@nospecialize(mat), x::Vector{Float64}, y::Vector{Float64}
14531454
nz = 1
14541455
isa(mat, Array{UInt16,3}) ? (ny, nx, nz) = size(mat) : (ny, nx) = size(mat)
14551456

1456-
(vals == "auto" || vals == :auto || (isa(vals, Real) && vals == 1)) &&
1457-
(vals = [find_histo_limits(mat)...]) # Out is a tuple, convert to vector
1457+
is_auto = (vals == "auto" || vals == :auto || (isa(vals, Real) && vals == 1))
1458+
if (is_auto)
1459+
vals = [find_histo_limits(mat)...] # Out is a tuple, convert to vector
1460+
elseif (isa(vals, Tuple)) # Shit is that we must tell from [v1 v2] and (tresh1, tresh2)
1461+
vals = [find_histo_limits(mat, thresholds=(Float64(vals[1]),Float64(vals[2])))...]
1462+
end
14581463
len::Int = length(vals)
14591464

14601465
(len > 2*nz) && error("'stretch' has more elements then allowed by image dimensions")

0 commit comments

Comments
 (0)