|
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) |
3 | 4 | inv_step_size = 1/step(edges) |
4 | | - scale = (maxval - minval) / (cdf[end] - first(cdf)) |
5 | 5 | function transform(val) |
6 | 6 | val = gray(val) |
7 | | - if val >= edges[end] |
8 | | - newval = cdf[end] |
| 7 | + if val >= last_edge |
| 8 | + return last_newval |
9 | 9 | elseif val < first_edge |
10 | | - newval = first(cdf) |
| 10 | + return first_newval |
11 | 11 | else |
12 | 12 | index = floor(Int, (val-first_edge)*inv_step_size) + 1 |
13 | | - newval = cdf[index] |
| 13 | + @inbounds newval = newvals[index] |
| 14 | + return newval |
14 | 15 | end |
15 | | - # Scale the new intensity value to so that it lies in the range [minval, maxval]. |
16 | | - newval = minval + (newval - first(cdf)) * scale |
17 | 16 | 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] |
22 | 26 | end |
| 27 | + return T[minval + (x - first_cdf) * scale for x in cdf] |
23 | 28 | end |
24 | 29 |
|
25 | 30 | function construct_pdfs(img::GenericGrayImage, targetimg::AbstractArray, edges::AbstractRange) |
|
0 commit comments