Skip to content

Commit

Permalink
Adopt ImageBase for minfinite/maxfinite (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy authored Jul 23, 2023
1 parent 64f8123 commit 70d52cf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 95 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "ImageContrastAdjustment"
uuid = "f332f351-ec65-5f6a-b3d1-319c6670881a"
authors = ["Dr. Zygmunt L. Szpak <[email protected]>"]
version = "0.3.11"
version = "0.3.12"

[deps]
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Expand Down
2 changes: 1 addition & 1 deletion src/HistogramAdjustmentAPI/HistogramAdjustmentAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module HistogramAdjustmentAPI
using ImageCore # ColorTypes is sufficient

# TODO Relax this to all image color types
const GenericGrayImage = AbstractArray{<:Union{Number, AbstractGray}}
using ..ImageContrastAdjustment: GenericGrayImage

"""
AbstractImageAlgorithm
Expand Down
7 changes: 4 additions & 3 deletions src/ImageContrastAdjustment.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module ImageContrastAdjustment

using ImageCore
using ImageBase
using ImageTransformations: imresize
# Where possible we avoid a direct dependency to reduce the number of [compat] bounds
using ImageCore.MappedArrays
using Parameters: @with_kw # Same as Base.@kwdef but works on Julia 1.0

# TODO Relax this to all image color types
const GenericGrayImage = AbstractArray{<:Union{Number, AbstractGray}}

# TODO: port HistogramAdjustmentAPI to ImagesAPI
include("HistogramAdjustmentAPI/HistogramAdjustmentAPI.jl")
import .HistogramAdjustmentAPI: AbstractHistogramAdjustmentAlgorithm,
adjust_histogram, adjust_histogram!

# TODO Relax this to all image color types
const GenericGrayImage = AbstractArray{<:Union{Number, AbstractGray}}

include("core.jl")
include("build_histogram.jl")
include("algorithms/common.jl")
include("algorithms/adaptive_equalization.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function construct_pdfs(img::GenericGrayImage, targetimg::AbstractArray, nbins::
if eltype(img) <: AbstractGray
imin, imax = 0, 1
else
imin, imax = min(minfinite(img), minfinite(targetimg)), max(maxfinite(img), maxfinite(targetimg))
imin, imax = min(minimum_finite(img), minimum_finite(targetimg)), max(maximum_finite(img), maximum_finite(targetimg))
end
edges, histogram = build_histogram(img, nbins, minval = imin, maxval = imax)
_, target_histogram = build_histogram(targetimg, edges)
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/linear_stretching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ end
function (f::LinearStretching)(out::GenericGrayImage, img::GenericGrayImage)
T = eltype(out)
FT = eltype(floattype(T))
img_min, img_max = minfinite(img), maxfinite(img)
img_min, img_max = minimum_finite(img), maximum_finite(img)
# explicit annotation is needed because the ?: line mixes three value types:
# Nothing, T, and typeof(f.src_minval)
src_minval::FT = isnothing(f.src_minval) ? img_min : f.src_minval
Expand Down
8 changes: 4 additions & 4 deletions src/build_histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ edges, counts = build_histogram(r, 256, minval = 0, maxval = 1)
[1] E. Herrholz, "Parsimonious Histograms," Ph.D. dissertation, Inst. of Math. and Comp. Sci., University of Greifswald, Greifswald, Germany, 2011.
"""
function build_histogram(img::GenericGrayImage, nbins::Integer = 256;
minval::Union{Real,AbstractGray}=minfinite(img),
maxval::Union{Real,AbstractGray}=maxfinite(img))
minval::Union{Real,AbstractGray}=minimum_finite(img),
maxval::Union{Real,AbstractGray}=maximum_finite(img))
edges = partition_interval(nbins, minval, maxval)
build_histogram(img, edges)
end
Expand All @@ -172,8 +172,8 @@ build_histogram(img::AbstractArray{C}) where C<:Color{N0f8} =
build_histogram(mappedarray(Gray{N0f8}, img))

function build_histogram(img::AbstractArray{T}, nbins::Integer;
minval::Union{Real,AbstractGray}=minfinite(img),
maxval::Union{Real,AbstractGray}=maxfinite(img)) where T<:Union{N0f8, AbstractGray{N0f8}}
minval::Union{Real,AbstractGray}=minimum_finite(img),
maxval::Union{Real,AbstractGray}=maximum_finite(img)) where T<:Union{N0f8, AbstractGray{N0f8}}
edgesraw, countsraw = build_histogram(img)
return rebin(edgesraw, countsraw, nbins, minval, maxval)
end
Expand Down
74 changes: 0 additions & 74 deletions src/core.jl

This file was deleted.

20 changes: 10 additions & 10 deletions test/core.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
@testset "Core" begin
A = [NaN 1 2 3;
NaN 6 5 4]
@test ImageContrastAdjustment.minfinite(A) == 1
@test ImageContrastAdjustment.maxfinite(A) == 6
@test ImageContrastAdjustment.minimum_finite(A) == 1
@test ImageContrastAdjustment.maximum_finite(A) == 6
A = rand(10:20, 5, 5)
@test ImageContrastAdjustment.minfinite(A) == minimum(A)
@test ImageContrastAdjustment.maxfinite(A) == maximum(A)
@test ImageContrastAdjustment.minimum_finite(A) == minimum(A)
@test ImageContrastAdjustment.maximum_finite(A) == maximum(A)
A = reinterpret(N0f8, rand(0x00:0xff, 5, 5))
@test ImageContrastAdjustment.minfinite(A) == minimum(A)
@test ImageContrastAdjustment.maxfinite(A) == maximum(A)
@test ImageContrastAdjustment.minimum_finite(A) == minimum(A)
@test ImageContrastAdjustment.maximum_finite(A) == maximum(A)
A = rand(Float32,3,5,5)
img = colorview(RGB, A)
dc = ImageContrastAdjustment.minfinite(img)-RGB{Float32}(minimum(A, dims=(2,3))...)
dc = ImageContrastAdjustment.minimum_finite(img)-RGB{Float32}(minimum(A, dims=(2,3))...)
@test norm(dc) < 1e-6
dc = ImageContrastAdjustment.maxfinite(img)-RGB{Float32}(maximum(A, dims=(2,3))...)
dc = ImageContrastAdjustment.maximum_finite(img)-RGB{Float32}(maximum(A, dims=(2,3))...)
@test norm(dc) < 1e-6
@test ImageContrastAdjustment.minfinite(x->x^2,[NaN,10,2]) == 4
@test ImageContrastAdjustment.maxfinite(x->x^2,[NaN,10,2]) == 100
@test ImageContrastAdjustment.minimum_finite(x->x^2,[NaN,10,2]) == 4
@test ImageContrastAdjustment.maximum_finite(x->x^2,[NaN,10,2]) == 100
end

2 comments on commit 70d52cf

@timholy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/88126

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.12 -m "<description of version>" 70d52cf4776333bc678fe77b16a9c30b368e6bf5
git push origin v0.3.12

Please sign in to comment.