Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/img_funs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ function binarize(I::GMTimage, thresh::Int=0; band::Int=1, threshold::Int=0, rev
(thresh == 0 && threshold > 0) && (thresh = threshold)
_tresh = (thresh == 0) ? isodata(I, band=band) : thresh
img = bool ? zeros(Bool, size(I, 1), size(I, 2)) : zeros(UInt8, size(I, 1), size(I, 2))
if revert
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .<= _tresh) : (slicecube(I, band).image .<= _tresh)
else
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .>= _tresh) : (slicecube(I, band).image .>= _tresh)
end
fun = revert ? (x -> x <= _tresh) : (x -> x >= _tresh)
t = size(I, 3) == 1 ? (I.image .|> fun) : I.layout[3] == 'B' ? (view(I.image, :, :, band) .|> fun) : (slicecube(I, band).image .|> fun)
img[t] .= bool ? true : 255
return mat2img(img, I)
end
function binarize(I::GMTimage, thresh::Vector{Int}; band::Int=1, revert::Bool=false,
bool::Bool=false)::Union{GMTimage{Bool, 2}, GMTimage{UInt8, 2}}
@assert length(thresh) == 2 && thresh[1] > 0 && thresh[2] < 255 "The threshold vector must have two elements in the range ]0 255["
img = bool ? zeros(Bool, size(I, 1), size(I, 2)) : zeros(UInt8, size(I, 1), size(I, 2))
t = I.layout[3] == 'B' ? thresh[1] .<= (view(I.image, :, :, band) .<= thresh[2]) : (thresh[1] .<= slicecube(I, band).image .<= thresh[2])
if (size(I, 3) == 1)
t = thresh[1] .<= I.image .<= thresh[2]
else
t = I.layout[3] == 'B' ? thresh[1] .<= (view(I.image, :, :, band) .<= thresh[2]) : (thresh[1] .<= slicecube(I, band).image .<= thresh[2])
end
revert && for k = 1:numel(t) t[k] = !t[k] end
img[t] .= bool ? true : 255
return mat2img(img, I)
Expand Down
11 changes: 8 additions & 3 deletions src/utils_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,8 @@ end
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...)
_mat2img_u16(mat, vec(Float64.(x)), vec(Float64.(y)), vec(Float64.(v)), vec(Float64.(hdr)), proj4, wkt, img8, KW(kw))
end
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})
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})
# Take an array of UInt16 and scale it down to UInt8. Input can be 2D or 3D.
# If the kw variable 'stretch' is used, we stretch the intervals in 'stretch' to [0 255].
# Use this option to stretch the image histogram.
Expand All @@ -1453,8 +1454,12 @@ function _mat2img_u16(@nospecialize(mat), x::Vector{Float64}, y::Vector{Float64}
nz = 1
isa(mat, Array{UInt16,3}) ? (ny, nx, nz) = size(mat) : (ny, nx) = size(mat)

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

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