From f5fb0062ad3d2db27e2ed4802ed31cb9a7438a6b Mon Sep 17 00:00:00 2001 From: cpaniaguam Date: Mon, 11 Nov 2024 14:31:40 -0500 Subject: [PATCH 1/4] feat: add rgb2gray function to convert RGB channels to grayscale --- src/histogram_equalization.jl | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index 64f59bee..17b81602 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -1,9 +1,21 @@ -function to_uint8(arr::AbstractMatrix{T}) where {T<:AbstractFloat} - img = Int.(round.(arr, RoundNearestTiesAway)) - img = clamp.(img, 0, 255) +function to_uint8(arr::AbstractMatrix{T}) where {T<:Union{AbstractFloat,Int,Signed}} + img = to_uint8.(arr) return img end +""" + rgb2gray(rgbchannels::Array{Float64, 3}) + +Convert an array of RGB channel data to grayscale in the range [0, 255]. +""" +function rgb2gray(rgbchannels::Array{Float64,3}) + # Could probably use Gray if image is in right format, but that is the challenge. CP + r, g, b = [to_uint8(rgbchannels[:, :, i]) for i in 1:3] + # Reusing the r array to store the equalized gray image + r .= to_uint8(0.2989 * r .+ 0.5870 * g .+ 0.1140 * b) + return r +end + function anisotropic_diffusion_3D(I) rgbchannels = get_rgb_channels(I) From 6ba13c09efe6a6bb30220fcfb44e67e78e592529 Mon Sep 17 00:00:00 2001 From: cpaniaguam Date: Mon, 11 Nov 2024 14:33:02 -0500 Subject: [PATCH 2/4] feat: return gamma green channel --- src/histogram_equalization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index 17b81602..e425a8a2 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -163,7 +163,7 @@ function _process_image_tiles( end end - return rgbchannels + return (equalized_gray=rgb2gray(rgbchannels), gammagreen=rgbchannels[:, :, 2]) end """ From 0f1d72f06f269670dc80c1f71ec951d94dfc7af3 Mon Sep 17 00:00:00 2001 From: Carlos Paniagua Date: Mon, 11 Nov 2024 23:25:20 -0500 Subject: [PATCH 3/4] feat: refactor rgb2gray function and add rgb2gray_uint8 for grayscale conversion --- src/histogram_equalization.jl | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index e425a8a2..3cc5dc9c 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -1,19 +1,10 @@ function to_uint8(arr::AbstractMatrix{T}) where {T<:Union{AbstractFloat,Int,Signed}} - img = to_uint8.(arr) - return img + return to_uint8.(arr) end -""" - rgb2gray(rgbchannels::Array{Float64, 3}) - -Convert an array of RGB channel data to grayscale in the range [0, 255]. -""" -function rgb2gray(rgbchannels::Array{Float64,3}) - # Could probably use Gray if image is in right format, but that is the challenge. CP - r, g, b = [to_uint8(rgbchannels[:, :, i]) for i in 1:3] - # Reusing the r array to store the equalized gray image - r .= to_uint8(0.2989 * r .+ 0.5870 * g .+ 0.1140 * b) - return r +function to_uint8(num::T) where {T<:Union{AbstractFloat,Int,Signed}} + num = Int(round(num, RoundNearestTiesAway)) + return clamp(num, 0, 255) end function anisotropic_diffusion_3D(I) @@ -278,6 +269,21 @@ function _get_false_color_cloudmasked(; return channels end +""" + rgb2gray_uint8(rgbchannels::Array{Float64, 3}) + +Convert an array of RGB channel data to grayscale in the range [0, 255]. + +For preprocessing_tiling: (equalized_gray=rgb2gray_uint8(rgbchannels), gammagreen=rgbchannels[:, :, 2])) +""" +function rgb2gray_uint8(rgbchannels) + # Could probably use Gray if image is in right format, but that is the challenge. CP + r, g, b = [to_uint8(rgbchannels[:, :, i]) for i in 1:3] + # Reusing the r array to store the equalized gray image + r .= to_uint8(0.2989 * r .+ 0.5870 * g .+ 0.1140 * b) + return r +end + """ rgb2gray(img::Matrix{RGB{Float64}}) @@ -313,10 +319,9 @@ Parsimonious version of `imhist` that uses the maximum value of the image to det """ function imhistp(img) nbins = nextpow(2, maximum(img) + 1) - return _imhist(img, 0:(nbins-1)) + return _imhist(img, 0:(nbins - 1)) end - """ histeq(img) Histogram equalization of `img` according to [1]. From 5548cea41260b0f0e634a9dfb298d97eea66ca6f Mon Sep 17 00:00:00 2001 From: Carlos Paniagua Date: Mon, 11 Nov 2024 23:25:33 -0500 Subject: [PATCH 4/4] refactor: update _process_image_tiles to return rgbchannels instead of equalized_gray --- src/histogram_equalization.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index 3cc5dc9c..9f3e80fb 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -154,7 +154,7 @@ function _process_image_tiles( end end - return (equalized_gray=rgb2gray(rgbchannels), gammagreen=rgbchannels[:, :, 2]) + return rgbchannels end """ @@ -201,7 +201,6 @@ function conditional_histeq( entropy_threshold, white_fraction_threshold, ) - return rgbchannels_equalized end