Skip to content

Commit

Permalink
Merge pull request #505 from mronian/add-hist-eq
Browse files Browse the repository at this point in the history
Fixes Issues with Histeq, changes the dtype argument.
  • Loading branch information
timholy committed Jun 27, 2016
2 parents 557804b + 904f773 commit c016178
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1242,11 +1242,11 @@ imhist{T<:Union{Gray,Number}}(img::AbstractArray{T}, nbins, minval, maxval) = im
"""
```
hist_equalised_img = histeq(img, nbins, dtype = "8bit")
hist_equalised_img = histeq(img, nbins, minval, maxval, dtype = "8bit")
hist_equalised_img = histeq(img, nbins, minval, maxval, dtype = U8)
```
Returns a histogram equalised image with a granularity of approximately `nbins`
number of bins. An optional `dtype` argument (defaulting to 8bit) can be specified
number of bins. An optional `dtype` argument (defaulting to U8) can be specified
to choose the number of bits of the returned image.
The `histeq` function can handle a variety of input types. The returned image depends
Expand All @@ -1262,23 +1262,17 @@ If minval and maxval are specified then only the intensities in the range
"""

RET_TYPE = Dict("8bit" => Gray{U8}, "16bit" => Gray{FixedPointNumbers.UFixed{UInt16,16}})
Y_MIN = 16
Y_MAX = 235

function _prep_image_for_histeq(img::AbstractArray, dtype)
img_shape = size(img)
if dtype == "8bit"
img = [convert(base_colorant_type(c){FixedPointNumbers.UFixed{UInt8,8}}, c) for c in img]
elseif dtype == "16bit"
img = [convert(base_colorant_type(c){FixedPointNumbers.UFixed{UInt16,16}}, c) for c in img]
end
img = reshape(img, img_shape)
img = convert(Array{base_colorant_type(eltype(img)){dtype}}, img)
img
end

histeq{T<:Colorant}(img::AbstractArray{T}, nbins, dtype = "8bit") = histeq(_prep_image_for_histeq(img, dtype), nbins, zero(YCbCr), zero(YCbCr))
histeq{T<:Colorant}(img::AbstractArray{T}, nbins, minval, maxval, dtype = "8bit") = histeq(_prep_image_for_histeq(img, dtype), nbins, minval, maxval)
histeq{T<:Colorant, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, dtype::Type{D} = U8) = histeq(_prep_image_for_histeq(img, dtype), nbins, zero(YCbCr), zero(YCbCr))
histeq{T<:Colorant, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, minval, maxval, dtype::Type{D} = U8) = histeq(_prep_image_for_histeq(img, dtype), nbins, minval, maxval)

function recompose_y(c::Color, eq_Y::Float32)
c_ycbcr = convert(YCbCr, color(c))
Expand All @@ -1303,26 +1297,26 @@ function histeq{T<:Colorant}(img::AbstractArray{T}, nbins, minval, maxval)
hist_equalised_img
end

function histeq(img::AbstractImage, nbins, dtype = "8bit")
function histeq{D<:Union{U8, U16}}(img::AbstractImage, nbins, dtype::Type{D} = U8)
hist_equalised_img = histeq(data(img), nbins, dtype)
hist_equalised_img = shareproperties(img, hist_equalised_img)
hist_equalised_img
end

function histeq(img::AbstractImage, nbins, minval, maxval, dtype = "8bit")
function histeq{D<:Union{U8, U16}}(img::AbstractImage, nbins, minval, maxval, dtype::Type{D} = U8)
hist_equalised_img = histeq(data(img), nbins, minval, maxval, dtype)
hist_equalised_img = shareproperties(img, hist_equalised_img)
hist_equalised_img
end

function histeq{T<:TransparentGray}(img::AbstractArray{T}, args...)
opaque_img = [color(c) for c in img]
opaque_img = convert(Array{base_color_type(eltype(img))}, img)
hist_equalised_img = histeq(opaque_img, args...)
hist_equalised_img = reshape([convert(T, AGray(hist_equalised_img[i], alpha(c))) for (i, c) in enumerate(img)], size(img))
hist_equalised_img
hist_equalised_img
end

histeq{T<:Gray}(img::AbstractArray{T}, nbins, dtype = "8bit") = histeq(_prep_image_for_histeq(img, dtype), nbins, ColorVectorSpace.typemin(RET_TYPE[dtype]), ColorVectorSpace.typemax(RET_TYPE[dtype]))
histeq{T<:Gray, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, dtype::Type{D} = U8) = histeq(_prep_image_for_histeq(img, dtype), nbins, ColorVectorSpace.typemin(Gray{dtype}), ColorVectorSpace.typemax(Gray{dtype}))

function histeq{T<:Union{Gray,Number}}(img::AbstractArray{T}, nbins::Int, minval::T, maxval::T)
bins, histogram = imhist(img, nbins, minval, maxval)
Expand All @@ -1332,6 +1326,7 @@ function histeq{T<:Union{Gray,Number}}(img::AbstractArray{T}, nbins::Int, minval
hist_equalised_img = [max(1,Int(ceil((x-minval)*cdf_length/(maxval-minval)))) for x in img]
hist_equalised_img = [minval + ((cdf[x]-cdf[1])*(maxval-minval)/(cdf[end]-cdf[1])) for x in hist_equalised_img]
hist_equalised_img = reshape(hist_equalised_img, img_shape)
hist_equalised_img = convert(Array{T}, hist_equalised_img)
hist_equalised_img
end

Expand Down

0 comments on commit c016178

Please sign in to comment.