Skip to content

Commit

Permalink
fix: revert reconstruct.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaniaguam committed Nov 6, 2024
1 parent e64ccf6 commit 32874c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
10 changes: 3 additions & 7 deletions src/reconstruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function imcomplement(img::Matrix{T}) where {T<:Union{Unsigned,Int}}
end

function imcomplement(img::Matrix{Gray{Float64}})
return 1 .- Float64.(img)
return 1 .- img
end

function reconstruct(img, se, type, invert::Bool=true)
Expand All @@ -20,13 +20,9 @@ function reconstruct(img, se, type, invert::Bool=true)
img = imcomplement(img)
end

return sk_morphology.reconstruction(morphed, img)
end

function reconstruct_dilation(img, se)
return reconstruct(img, se, "dilation", true)
return IceFloeTracker.MorphSE.mreconstruct(IceFloeTracker.MorphSE.dilate, morphed, img)
end

function reconstruct_erosion(img, se)
return reconstruct(img, se, "erosion", false)
return reconstruct(img, se, "dilation")
end
45 changes: 21 additions & 24 deletions test/test-reconstruct.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
using IceFloeTracker: imcomplement, reconstruct, reconstruct_dilation
using IceFloeTracker: imcomplement, reconstruct, reconstruct_erosion

r = ZipFile.Reader("test_inputs/coins.zip")
coins = readdlm(r.files[1], ',', Int)
coins_gray = Gray.(coins ./ 255)
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
Expand All @@ -13,33 +25,18 @@ close(r)
@test imcomplement(coins_gray) == 1 .- coins_gray
end

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

_round(x) = Int(round(x, RoundNearestTiesAway))

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

_reconstruct(img, se, type) = reconstruct(img, se, type, false)

# TODO: fix this test so sk_morphology.reconstruction works with Gray{Float64} images
@testset "open_by_reconstruction" begin
test_cases = [(coins, 7552396)]#, (coins_gray, 29617.239215686277)]
test_cases = [(coins, 7552396), (coins_gray, 29617.239215686277)]
run_tests(test_cases, (img, se) -> _reconstruct(img, se, "erosion"), se_disk1)
end

# TODO: fix this test
# @testset "close_by_reconstruction" begin
# test_cases = [(coins, 7599858)]#, (coins_gray, 29803.36470588235)]
# run_tests(test_cases, (img, se) -> _reconstruct(img, se, "dilation"), se_disk1)
# end
@testset "close_by_reconstruction" begin
test_cases = [(coins, 7599858), (coins_gray, 29803.36470588235)]
run_tests(test_cases, (img, se) -> _reconstruct(img, se, "dilation"), se_disk1)
end

@testset "reconstruct_dilation" begin
@testset "reconstruct_erosion" begin
test_cases = [(coins, 11179481), (coins_gray, 43841.10196078432)]
run_tests(test_cases, reconstruct_dilation, se_disk1)
run_tests(test_cases, reconstruct_erosion, se_disk1)
end
end
end

0 comments on commit 32874c4

Please sign in to comment.