Skip to content

Commit

Permalink
Merge 044ec95 into 919f480
Browse files Browse the repository at this point in the history
  • Loading branch information
mronian committed Jul 13, 2016
2 parents 919f480 + 044ec95 commit 6fe34eb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/src/function_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ findlocalminima
```@docs
imhist
histeq
adjust_gamma
```

# Filtering kernels
Expand Down
3 changes: 2 additions & 1 deletion src/Images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export # types
imgradients,
imhist,
histeq,
adjust_gamma,
imlaplacian,
imlineardiffusion,
imlog,
Expand Down Expand Up @@ -289,7 +290,7 @@ Algorithms:
- Resizing: `restrict`, `imresize` (not yet exported)
- Filtering: `imfilter`, `imfilter_fft`, `imfilter_gaussian`, `imfilter_LoG`, `imROF`, `ncc`, `padarray`
- Filtering kernels: `ando[345]`, `guassian2d`, `imaverage`, `imdog`, `imlaplacian`, `prewitt`, `sobel`
- Exposure : `imhist`, `histeq`
- Exposure : `imhist`, `histeq`, `adjust_gamma`
- Gradients: `backdiffx`, `backdiffy`, `forwarddiffx`, `forwarddiffy`, `imgradients`
- Edge detection: `imedge`, `imgradients`, `thin_edges`, `magnitude`, `phase`, `magnitudephase`, `orientation`, `canny`
- Corner detection: `imcorner`
Expand Down
68 changes: 54 additions & 14 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1240,18 +1240,6 @@ end

imhist{T<:Union{Gray,Number}}(img::AbstractArray{T}, nbins, minval, maxval) = imhist(img, nbins, convert(T, minval), convert(T, maxval))

Y_MIN = 16
Y_MAX = 235

function _prep_image_for_histeq{D<:Union{U8, U16}}(img::AbstractArray, dtype::Type{D})
img_shape = size(img)
img = convert(Array{base_colorant_type(eltype(img)){dtype}}, img)
img
end

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))
ret_c = YCbCr(eq_Y, c_ycbcr.cb, c_ycbcr.cr)
Expand All @@ -1264,6 +1252,12 @@ function recompose_y(c::TransparentColor, eq_Y::Float32)
ret_c
end

Y_MIN = 16
Y_MAX = 235

histeq{T<:Colorant, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, dtype::Type{D} = U8) = histeq(convert(Array{base_colorant_type(eltype(img)){dtype}}, img), nbins, zero(YCbCr), zero(YCbCr))
histeq{T<:Colorant, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, minval, maxval, dtype::Type{D} = U8) = histeq(convert(Array{base_colorant_type(eltype(img)){dtype}}, img), nbins, minval, maxval)

function histeq{T<:Colorant}(img::AbstractArray{T}, nbins, minval, maxval)
Y = map(c -> convert(YCbCr, color(c)).y, img)
if maxval == zero(YCbCr{Float32})
Expand Down Expand Up @@ -1294,7 +1288,7 @@ function histeq{T<:TransparentGray}(img::AbstractArray{T}, args...)
converted_img
end

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}))
histeq{T<:Gray, D<:Union{U8, U16}}(img::AbstractArray{T}, nbins, dtype::Type{D} = U8) = histeq(convert(Array{base_colorant_type(eltype(img)){dtype}}, img), nbins, ColorVectorSpace.typemin(Gray{dtype}), ColorVectorSpace.typemax(Gray{dtype}))

function _histeq_pixel_rescale{T<:Union{Gray,Number}}(pixel::T, cdf, minval::T, maxval::T, cdf_length::Integer)
bin_pixel = max(1,Int(ceil((pixel-minval)*cdf_length/(maxval-minval))))
Expand Down Expand Up @@ -1337,6 +1331,52 @@ histeq{T<:Number}(img::AbstractArray{T}, nbins) = histeq(img, nbins, minfinite(i

histeq{T<:Union{Gray,Number}}(img::AbstractArray{T}, nbins, minval, maxval) = histeq(img, Int(nbins), convert(T, minval), convert(T, maxval))

adjust_gamma{T<:Colorant, D<:Union{U8, U16}}(img::AbstractArray{T}, gamma::Number, dtype::Type{D} = U8) = map(c -> convert(T, recompose_y(convert(base_colorant_type(typeof(c)){dtype}, c), gamma_pixel_rescale(convert(YCbCr, color(convert(base_colorant_type(typeof(c)){dtype}, c))).y, gamma, Y_MIN, Y_MAX))), img)

function adjust_gamma{D<:Union{U8, U16}}(img::AbstractImage, gamma::Number, dtype::Type{D} = U8)
gamma_corrected_img = adjust_gamma(data(img), gamma, dtype)
gamma_corrected_img = shareproperties(img, gamma_corrected_img)
gamma_corrected_img
end

function adjust_gamma{T<:TransparentGray, D<:Union{U8, U16}}(img::AbstractArray{T}, gamma::Number, dtype::Type{D} = U8)
opaque_img = convert(Array{base_color_type(eltype(img))}, img)
gamma_corrected_img = adjust_gamma(opaque_img, gamma, dtype)
converted_img = map((gc, o) -> convert(T, AGray(gc, alpha(o))), gamma_corrected_img, img)
converted_img
end

adjust_gamma{T<:Number}(img::AbstractArray{T}, gamma::Number, minval::Number, maxval::Number) = map(i -> gamma_pixel_rescale(i, gamma, minval, maxval), img)

gamma_pixel_rescale(original_val::Number, gamma::Number, minval::Number, maxval::Number) = Float32(minval + (maxval - minval) * ((original_val - minval) / (maxval - minval)) ^ gamma)

"""
```
gamma_corrected_img = adjust_gamma(img, gamma, dtype = "8bit")
```
Returns a gamma corrected image. An optional `dtype` argument (defaulting to 8bit) can be
specified to choose the number of bits of the returned image.
The `adjust_gamma` function can handle a variety of input types. The returned image depends
on the input type. If the input is an `Image` then the resulting image is of the same type
and has the same properties.
For coloured images, the input is converted to YCbCr type and the Y channel is gamma corrected.
This is the combined with the Cb and Cr channels and the resulting image converted to the same
type as the input.
"""
function adjust_gamma{T<:FixedPointNumbers.UFixed, D<:Union{U8, U16}}(img::AbstractArray{Gray{T}}, gamma::Number, dtype::Type{D} = U8)
raw_type = FixedPointNumbers.rawtype(dtype)
gamma_inv = 1.0 / gamma
table = [dtype((i / typemax(raw_type)) ^ gamma_inv) for i in 0:typemax(raw_type)]
gamma_corrected_img = map(x -> Gray(table[convert(base_colorant_type(typeof(x)){dtype}, x).val.i + 1]), img)
gamma_corrected_img
end

adjust_gamma{T<:AbstractFloat}(img::AbstractArray{Gray{T}}, gamma::Number) = map(x -> Gray(x.val ^ gamma), img)

"""
`imgr = restrict(img[, region])` performs two-fold reduction in size
along the dimensions listed in `region`, or all spatial coordinates if
Expand Down Expand Up @@ -1976,4 +2016,4 @@ function integral_image(img::AbstractArray)
cumsum!(integral_img, integral_img, sd[i])
end
integral_img
end
end
3 changes: 3 additions & 0 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ facts("Algorithms") do
@fact imhist(img, 5) --> (0.0:0.2:1.0,[0,1,1,1,0,1,0])
img = reinterpret(Gray{U8}, [0x00,0x40,0x80,0xff])
@fact imhist(img, 6) --> (0.0:0.2:1.2,[0,1,1,1,0,0,1,0])



end

context("Array padding") do
Expand Down

0 comments on commit 6fe34eb

Please sign in to comment.