Skip to content

Commit

Permalink
Merge pull request #53 from JuliaImages/teh/hoist
Browse files Browse the repository at this point in the history
More optimizations
  • Loading branch information
zygmuntszpak authored Sep 14, 2021
2 parents 37000d7 + 776b4e4 commit d545b4e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
31 changes: 18 additions & 13 deletions src/algorithms/common.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
function transform_density!(img::GenericGrayImage, edges::AbstractArray, cdf::AbstractArray, minval::Union{Real,AbstractGray}, maxval::Union{Real,AbstractGray})
first_edge = first(edges)
function transform_density!(out::GenericGrayImage, img::GenericGrayImage, edges::AbstractRange, newvals::AbstractVector)
first_edge, last_edge = first(edges), last(edges)
first_newval, last_newval = first(newvals), last(newvals)
inv_step_size = 1/step(edges)
scale = (maxval - minval) / (cdf[end] - first(cdf))
function transform(val)
val = gray(val)
if val >= edges[end]
newval = cdf[end]
if val >= last_edge
return last_newval
elseif val < first_edge
newval = first(cdf)
return first_newval
else
index = floor(Int, (val-first_edge)*inv_step_size) + 1
newval = cdf[index]
@inbounds newval = newvals[index]
return newval
end
# Scale the new intensity value to so that it lies in the range [minval, maxval].
newval = minval + (newval - first(cdf)) * scale
end
if eltype(img) <: Integer
map!(val->ceil(transform(val)), img, img)
else
map!(transform, img, img)
map!(transform, out, img)
end

function build_lookup(cdf, minval::T, maxval::T) where T
first_cdf = first(cdf)
# Scale the new intensity value to so that it lies in the range [minval, maxval].
scale = (maxval - minval) / (cdf[end] - first_cdf)
if T <: Integer
return T[ceil(minval + (x - first_cdf) * scale) for x in cdf]
end
return T[minval + (x - first_cdf) * scale for x in cdf]
end

function construct_pdfs(img::GenericGrayImage, targetimg::AbstractArray, edges::AbstractRange)
Expand Down
7 changes: 4 additions & 3 deletions src/algorithms/equalization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ imshow(imgeq)
end

function (f::Equalization)(out::GenericGrayImage, img::GenericGrayImage)
edges, histogram = build_histogram(img, f.nbins, minval = f.minval, maxval = f.maxval)
minval, maxval = convert(eltype(out), f.minval), convert(eltype(out), f.maxval)
edges, histogram = build_histogram(img, f.nbins, minval = minval, maxval = maxval)
lb = first(axes(histogram,1))
ub = last(axes(histogram,1))
N = length(img)
cdf = cumsum(histogram[lb:ub]/N)
out .= img
transform_density!(out, edges, cdf, f.minval, f.maxval)
newvals = build_lookup(cdf, minval, maxval)
transform_density!(out, img, edges, newvals)
end

function (f::Equalization)(out::AbstractArray{<:Color3}, img::AbstractArray{<:Color3})
Expand Down
11 changes: 5 additions & 6 deletions src/build_histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,16 @@ function build_histogram(img::GenericGrayImage, edges::AbstractRange)
Base.has_offset_axes(edges) && throw(ArgumentError("edges must be indexed starting with 1"))
lb = first(axes(edges,1))-1
ub = last(axes(edges,1))
first_edge = first(edges)
first_edge, last_edge = first(edges), last(edges)
inv_step_size = 1/step(edges)
counts = fill(0, lb:ub)
for val in img
@inbounds for val in img
if isnan(val)
continue
else
if val >= edges[end]
counts[end] += 1
continue
elseif val < first(edges)
if val >= last_edge
counts[ub] += 1
elseif val < first_edge
counts[lb] += 1
else
index = floor(Int, gray((val-first_edge)*inv_step_size)) + 1
Expand Down

0 comments on commit d545b4e

Please sign in to comment.