From f3d24601395c4446372de6d89b2539c45c9afdb8 Mon Sep 17 00:00:00 2001 From: Carlos Paniagua Date: Mon, 18 Nov 2024 11:23:42 -0500 Subject: [PATCH] fix: restore imhist --- src/histogram_equalization.jl | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index 1a5ebb0e..95645d33 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -289,3 +289,46 @@ Histogram equalization of `img` using `nbins` bins. function histeq(img::S; nbins=64)::S where {S<:AbstractArray{<:Integer}} return to_uint8(sk_exposure.equalize_hist(img, nbins=nbins) * 255) end + +function _imhist(img, rng) + d = Dict(k => 0 for k in rng) + for i in img + d[i] = d[i] + 1 + end + k, heights = collect.([keys(d), values(d)]) + order = sortperm(k) + k, heights = k[order], heights[order] + return k, heights +end + +""" + imhist(img, imgtype::AbstractString="uint8") + +Compute the histogram of an image where each possible value is represented in the histogram. The function returns a tuple with the bins and counts of each bin. + +# Example +```jldoctest; setup = :(using IceFloeTracker) +julia> img = [ + 4 4 4 4 4 + 3 4 5 4 3 + 3 5 5 5 3 + 3 4 5 4 3 + 4 4 4 4 4 +] + +julia> bins, heights = imhist(img); + +julia> [bins[heights .> 0] heights[heights .>0]] # display only non-zero bins and heights +3×2 Matrix{Int64}: + 3 6 + 4 14 + 5 5 + +""" +function imhist(img, imgtype::AbstractString="uint8") + + # TODO: add validation for arr: either uint8 0:255 or grayscale 0:1 + rng = imgtype == "uint8" ? range(0, 255) : range(0; stop=1, length=256) + + return _imhist(img, rng) +end