diff --git a/src/IceFloeTracker.jl b/src/IceFloeTracker.jl index 7b11a895..7c605e28 100644 --- a/src/IceFloeTracker.jl +++ b/src/IceFloeTracker.jl @@ -71,9 +71,10 @@ include("branch.jl") include("special_strels.jl") include("tilingutils.jl") include("histogram_equalization.jl") -include("imadjust.jl") +include("brighten.jl") include("morph_fill.jl") include("imcomplement.jl") +include("imadjust.jl") function get_version_from_toml(pth=dirname(dirname(pathof(IceFloeTracker))))::VersionNumber toml = TOML.parsefile(joinpath(pth, "Project.toml")) diff --git a/src/brighten.jl b/src/brighten.jl new file mode 100644 index 00000000..634dd22e --- /dev/null +++ b/src/brighten.jl @@ -0,0 +1,28 @@ +""" + get_brighten_mask(equalized_gray_reconstructed_img, gamma_green) +# Arguments +- `equalized_gray_reconstructed_img`: The equalized gray reconstructed image (uint8 in Matlab). +- `gamma_green`: The gamma value for the green channel (also uint8). +# Returns +Difference equalized_gray_reconstructed_img - gamma_green clamped between 0 and 255. +""" +function get_brighten_mask(equalized_gray_reconstructed_img, gamma_green) + return to_uint8(equalized_gray_reconstructed_img - gamma_green) +end + +""" + imbrighten(img, brighten_mask, bright_factor) +Brighten the image using a mask and a brightening factor. +# Arguments +- `img`: The input image. +- `brighten_mask`: A mask indicating the pixels to brighten. +- `bright_factor`: The factor by which to brighten the pixels. +# Returns +- The brightened image. +""" +function imbrighten(img, brighten_mask, bright_factor) + img = Float64.(img) + brighten_mask = brighten_mask .> 0 + img[brighten_mask] .= img[brighten_mask] * bright_factor + return img = to_uint8(img) +end diff --git a/src/histogram_equalization.jl b/src/histogram_equalization.jl index 64f59bee..fe1f0fef 100644 --- a/src/histogram_equalization.jl +++ b/src/histogram_equalization.jl @@ -4,6 +4,11 @@ function to_uint8(arr::AbstractMatrix{T}) where {T<:AbstractFloat} return img end +function to_uint8(arr::AbstractMatrix{T}) where {T<:Integer} + img = clamp.(arr, 0, 255) + return img +end + function anisotropic_diffusion_3D(I) rgbchannels = get_rgb_channels(I) diff --git a/test/test-brighten.jl b/test/test-brighten.jl new file mode 100644 index 00000000..2b29fd88 --- /dev/null +++ b/test/test-brighten.jl @@ -0,0 +1,22 @@ +using IceFloeTracker: get_brighten_mask, imbrighten + +@testset "brighten tests" begin + @testset "get_brighten_mask" begin + img = rand(0:255, 5, 5) + bumped_img = img .+ 1 + mask = get_brighten_mask(img, bumped_img) + @test all(mask .== 0) + end + + @testset "imbrighten tests" begin + img = [1 2; 3 4] + brighten_mask = [1 0; 1 0] + + test_cases = [(1.25, [1 2; 4 4]), (0.1, [0 2; 0 4]), (0.9, img)] + + for (bright_factor, expected_result) in test_cases + result = imbrighten(img, brighten_mask, bright_factor) + @test result == expected_result + end + end +end \ No newline at end of file diff --git a/test/test-imadjust.jl b/test/test-imadjust.jl index e2acdcf2..470e880c 100644 --- a/test/test-imadjust.jl +++ b/test/test-imadjust.jl @@ -1,5 +1,5 @@ @testset "imadjust" begin Random.seed!(123) img = rand(0:255, 100, 100) - sum(IceFloeTracker.imadjust(img)) == 1291155 + @test sum(IceFloeTracker.imadjust(img)) == 1291155 end