From 0233872961f2aa90be43bac88488d119d69e8341 Mon Sep 17 00:00:00 2001 From: Carlos Paniagua Date: Tue, 22 Oct 2024 00:17:12 -0400 Subject: [PATCH] feat: get_image_peaks, imhist --- src/tilingutils.jl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/tilingutils.jl b/src/tilingutils.jl index 7dde3e6e..6adef23f 100644 --- a/src/tilingutils.jl +++ b/src/tilingutils.jl @@ -244,3 +244,38 @@ function imbrighten(img, brighten_mask, bright_factor) img[brighten_mask] .= img[brighten_mask] * bright_factor return img = to_uint8(img) end + +function imhist(img, imgtype="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) + # use range(0, stop=1, length=256) for grayscale images + + # build histogram + d = Dict(k => 0 for k in rng) + for i in img + d[i] = d[i] + 1 + end + + # sort by key (bins) + k, heights = collect.([Base.keys(d), Base.values(d)]) + order = sortperm(k) + k, heights = k[order], heights[order] + + return k, heights +end + +function get_image_peaks(arr, imgtype="uint8") + # TODO: add validation for arr: either uint8 0:255 or grayscale 0:1 + + _, heights = imhist(arr, imgtype) + + locs, heights, _ = Peaks.findmaxima(heights) + + # TODO: make this conditional on input args + order = sortperm(heights; rev=true) + locs, heights = locs[order], heights[order] + + return locs, heights +end