Skip to content

Commit

Permalink
Merge pull request #485 from WilhelmusLab/484-refactor-kmeans_segment…
Browse files Browse the repository at this point in the history
…ation

feat: refactor k-means segmentation
  • Loading branch information
cpaniaguam authored Nov 8, 2024
2 parents 3e74b31 + 78455a0 commit 03bd0d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
35 changes: 24 additions & 11 deletions src/segmentation_a_direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,37 @@ Apply k-means segmentation to a gray image to isolate a cluster group representi
"""
function kmeans_segmentation(
gray_image::Matrix{Gray{Float64}}, ice_labels::Vector{Int64}
gray_image::Matrix{Gray{Float64}},
ice_labels::Vector{Int64},
k::Int64=4,
maxiter::Int64=50,
)::BitMatrix
Random.seed!(45) # this seed generates consistent clusters for the final output
gray_image_height, gray_image_width = size(gray_image)
gray_image_1d = vec(gray_image)
@info("Done with reshape")
segmented = kmeans_segmentation(gray_image; k=k, maxiter=maxiter)

## NOTE(tjd): this clusters into 4 classes and solves iteratively with a max of 50 iterations
## Isolate ice floes and contrast from background
segmented_ice = get_segmented_ice(segmented, ice_labels)
return segmented_ice
end

function kmeans_segmentation(
gray_image::Matrix{Gray{Float64}}; k::Int64=4, maxiter::Int64=50
)
Random.seed!(45)

## NOTE(tjd): this clusters into k classes and solves iteratively with a max of maxiter iterations
feature_classes = Clustering.kmeans(
gray_image_1d, 4; maxiter=50, display=:none, init=:kmpp
vec(gray_image), k; maxiter=maxiter, display=:none, init=:kmpp
)

class_assignments = assignments(feature_classes)

## NOTE(tjd): this reshapes column major vector of kmeans classes back into original image shape
segmented = reshape(class_assignments, gray_image_height, gray_image_width)
## NOTE(tjd): this clusters into 4 classes and solves iteratively with a max of 50 iterations
segmented = reshape(class_assignments, size(gray_image))

return segmented
end

function get_segmented_ice(segmented::Matrix{Int64}, ice_labels::Vector{Int64})
## Isolate ice floes and contrast from background
nlabel = StatsBase.mode(segmented[ice_labels])
segmented_ice = segmented .== nlabel
Expand Down Expand Up @@ -76,9 +91,7 @@ function segmentation_A(

segmented_bridged = IceFloeTracker.bridge(segmented_opened_branched)

#segmented_ice_filled = .!bwareamaxfilt(.!segmented_bridged)
segmented_ice_filled = IceFloeTracker.MorphSE.fill_holes(segmented_bridged)
@info "Done filling segmented_ice"

diff_matrix = segmented_ice_opened .!= segmented_ice_filled

Expand Down
3 changes: 0 additions & 3 deletions test/test-segmentation-a.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
@time segmented_ice_cloudmasked = IceFloeTracker.segmented_ice_cloudmasking(
ice_water_discriminated_image, cloudmask, ice_labels
)
IceFloeTracker.@persist segmented_ice_cloudmasked "./test_outputs/segmented_a_ice_cloudmasked.png" true

@time segmented_A = IceFloeTracker.segmentation_A(segmented_ice_cloudmasked)
IceFloeTracker.@persist segmented_A "./test_outputs/segmented_a.png" true

@time segmented_ice = IceFloeTracker.kmeans_segmentation(
ice_water_discriminated_image, ice_labels
)
IceFloeTracker.@persist segmented_ice "./test_outputs/segmented_a_ice.png" true

@test typeof(segmented_A) == typeof(matlab_segmented_A_bitmatrix)
@test test_similarity(matlab_segmented_A_bitmatrix, segmented_A, 0.039)
Expand Down

0 comments on commit 03bd0d8

Please sign in to comment.