Skip to content

Commit

Permalink
fix: restore imhist
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaniaguam committed Nov 18, 2024
1 parent c72fc97 commit f3d2460
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/histogram_equalization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f3d2460

Please sign in to comment.