Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor k-means segmentation #485

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad to get hardcoded values out

)

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
Loading