-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 493-get_new23-final
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |