Skip to content

Commit d545b4e

Browse files
authored
Merge pull request #53 from JuliaImages/teh/hoist
More optimizations
2 parents 37000d7 + 776b4e4 commit d545b4e

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/algorithms/common.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
function transform_density!(img::GenericGrayImage, edges::AbstractArray, cdf::AbstractArray, minval::Union{Real,AbstractGray}, maxval::Union{Real,AbstractGray})
2-
first_edge = first(edges)
1+
function transform_density!(out::GenericGrayImage, img::GenericGrayImage, edges::AbstractRange, newvals::AbstractVector)
2+
first_edge, last_edge = first(edges), last(edges)
3+
first_newval, last_newval = first(newvals), last(newvals)
34
inv_step_size = 1/step(edges)
4-
scale = (maxval - minval) / (cdf[end] - first(cdf))
55
function transform(val)
66
val = gray(val)
7-
if val >= edges[end]
8-
newval = cdf[end]
7+
if val >= last_edge
8+
return last_newval
99
elseif val < first_edge
10-
newval = first(cdf)
10+
return first_newval
1111
else
1212
index = floor(Int, (val-first_edge)*inv_step_size) + 1
13-
newval = cdf[index]
13+
@inbounds newval = newvals[index]
14+
return newval
1415
end
15-
# Scale the new intensity value to so that it lies in the range [minval, maxval].
16-
newval = minval + (newval - first(cdf)) * scale
1716
end
18-
if eltype(img) <: Integer
19-
map!(val->ceil(transform(val)), img, img)
20-
else
21-
map!(transform, img, img)
17+
map!(transform, out, img)
18+
end
19+
20+
function build_lookup(cdf, minval::T, maxval::T) where T
21+
first_cdf = first(cdf)
22+
# Scale the new intensity value to so that it lies in the range [minval, maxval].
23+
scale = (maxval - minval) / (cdf[end] - first_cdf)
24+
if T <: Integer
25+
return T[ceil(minval + (x - first_cdf) * scale) for x in cdf]
2226
end
27+
return T[minval + (x - first_cdf) * scale for x in cdf]
2328
end
2429

2530
function construct_pdfs(img::GenericGrayImage, targetimg::AbstractArray, edges::AbstractRange)

src/algorithms/equalization.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ imshow(imgeq)
104104
end
105105

106106
function (f::Equalization)(out::GenericGrayImage, img::GenericGrayImage)
107-
edges, histogram = build_histogram(img, f.nbins, minval = f.minval, maxval = f.maxval)
107+
minval, maxval = convert(eltype(out), f.minval), convert(eltype(out), f.maxval)
108+
edges, histogram = build_histogram(img, f.nbins, minval = minval, maxval = maxval)
108109
lb = first(axes(histogram,1))
109110
ub = last(axes(histogram,1))
110111
N = length(img)
111112
cdf = cumsum(histogram[lb:ub]/N)
112-
out .= img
113-
transform_density!(out, edges, cdf, f.minval, f.maxval)
113+
newvals = build_lookup(cdf, minval, maxval)
114+
transform_density!(out, img, edges, newvals)
114115
end
115116

116117
function (f::Equalization)(out::AbstractArray{<:Color3}, img::AbstractArray{<:Color3})

src/build_histogram.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,16 @@ function build_histogram(img::GenericGrayImage, edges::AbstractRange)
188188
Base.has_offset_axes(edges) && throw(ArgumentError("edges must be indexed starting with 1"))
189189
lb = first(axes(edges,1))-1
190190
ub = last(axes(edges,1))
191-
first_edge = first(edges)
191+
first_edge, last_edge = first(edges), last(edges)
192192
inv_step_size = 1/step(edges)
193193
counts = fill(0, lb:ub)
194-
for val in img
194+
@inbounds for val in img
195195
if isnan(val)
196196
continue
197197
else
198-
if val >= edges[end]
199-
counts[end] += 1
200-
continue
201-
elseif val < first(edges)
198+
if val >= last_edge
199+
counts[ub] += 1
200+
elseif val < first_edge
202201
counts[lb] += 1
203202
else
204203
index = floor(Int, gray((val-first_edge)*inv_step_size)) + 1

0 commit comments

Comments
 (0)