Skip to content

Commit

Permalink
Merge branch 'main' into 493-get_new23-final
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaniaguam authored Nov 18, 2024
2 parents d98eac0 + c7c43dc commit e9c2853
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/IceFloeTracker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ include("special_strels.jl")
include("tilingutils.jl")
include("histogram_equalization.jl")
include("regularize-final.jl")
include("reconstruction.jl")
include("watershed.jl")
include("brighten.jl")
include("morph_fill.jl")
Expand Down
32 changes: 32 additions & 0 deletions src/reconstruction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
reconstruct(img, se, type, invert)
Perform closing/opening by reconstruction on `img`.
# Arguments
- `img::AbstractArray`: The input image.
- `se::AbstractArray`: The structuring element.
- `type::String`: The type of morphological operation to perform. Must be either "dilation" (close by reconstruction) or `"erosion"` (open by reconstruction).
- `invert::Bool=true`: Invert marker and mask before reconstruction.
"""
function reconstruct(img, se, type, invert::Bool=true)
!(type == "dilation" || type == "erosion") &&
throw(ArgumentError("Invalid type: $type. Must be 'dilation' or 'erosion'."))

type == "dilation" && (
morphed = to_uint8(
IceFloeTracker.sk_morphology.dilation(img; footprint=collect(se))
)
)
type == "erosion" && (
morphed = to_uint8(IceFloeTracker.sk_morphology.erosion(img; footprint=collect(se)))
)

invert && (morphed = imcomplement(to_uint8(morphed)); img = imcomplement(img))

type == "dilation" && return IceFloeTracker.MorphSE.mreconstruct(
IceFloeTracker.MorphSE.dilate, morphed, img
)

return IceFloeTracker.sk_morphology.reconstruction(morphed, img)
end
37 changes: 37 additions & 0 deletions test/test-reconstruction-workflows.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using IceFloeTracker: imcomplement, reconstruct
using ZipFile

r = ZipFile.Reader("test_inputs/coins.zip")
coins = readdlm(r.files[1], ',', Int)
close(r)

se_disk1 = IceFloeTracker.MorphSE.StructuringElements.strel_diamond((3, 3))

_round(x) = Int(round(x, RoundNearestTiesAway))
_reconstruct(img, se, type) = reconstruct(img, se, type, false)

function run_tests(test_cases, func, se)
for (img, expected) in test_cases
result = func(img, se)
@test _round(Float64(sum(result))) == _round(expected)
end
end

@testset "reconstruct" begin
@testset "imcomplement" begin
@test imcomplement(coins) == 255 .- coins

coins_gray = Gray.(coins ./ 255)
@test imcomplement(coins_gray) == 1 .- coins_gray
end

@testset "open_by_reconstruction" begin
test_cases = [(coins, 7552396)]
run_tests(test_cases, (img, se) -> _reconstruct(img, se, "erosion"), se_disk1)
end

@testset "close_by_reconstruction" begin
test_cases = [(coins, 7599858)]
run_tests(test_cases, (img, se) -> _reconstruct(img, se, "dilation"), se_disk1)
end
end

0 comments on commit e9c2853

Please sign in to comment.