-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This has one breaking change: while the docs advertise that "All the segmentation algorithms (except Fuzzy C-means) return a struct SegmentedImage," that's not been true for `kmeans`. This changes the output so that `kmeans` does return a `SegmentedImage`. This is a breaking change, because the types are not interchangeable. This became apparent when trying to add tests for `kmeans`, which have been lacking. Before releasing this, we may want to make a second breaking change: currently, ImageSegmentation provides a new meaning for `Matrix{Gray{T}}` than Clustering.jl provides for `Matrix{T}`. This breaks our abstraction that `Gray ≈ Number`. A way to fix that would be to have `ImageSegmentation.kmeans` be a different function from `Clustering.kmeans`, and obviously have the one in ImageSegmentation call the one in Clustering.
- Loading branch information
Showing
5 changed files
with
32 additions
and
8 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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
function img_to_data(img::AbstractArray{T,N}) where T<:Colorant where N | ||
AT = accum_type(T) | ||
aimg = AT.(img) | ||
pimg = parent(aimg) | ||
ch = channelview(pimg) | ||
data = reshape(ch, :, *(size(pimg)...)) | ||
aimg = of_eltype(accum_type(T), img) | ||
ch = channelview(aimg) | ||
return reshape(ch, :, *(size(img)...)) | ||
end | ||
|
||
kmeans(img::AbstractArray{T,N}, args...; kwargs...) where {T<:Colorant,N} = | ||
kmeans(img_to_data(img), args...; kwargs...) | ||
SegmentedImage(kmeans(img_to_data(img), args...; kwargs...), img) | ||
|
||
fuzzy_cmeans(img::AbstractArray{T,N}, args...; kwargs...) where {T<:Colorant,N} = | ||
fuzzy_cmeans(img_to_data(img), args...; kwargs...) |
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,17 @@ | ||
@testset "Clustering" begin | ||
# Test the example in the docs | ||
path = download("https://github.com/JuliaImages/juliaimages.github.io/raw/source/docs/src/pkgs/segmentation/assets/flower.jpg") | ||
img = load(path) | ||
r = fuzzy_cmeans(img, 3, 2) | ||
@test size(r.centers) == (3,3) | ||
@test size(r.weights, 1) == length(img) | ||
@test all(≈(1), sum(r.weights; dims=2)) | ||
cmin, cmax = extrema(sum(r.weights; dims=1)) | ||
@test cmax < 3*cmin | ||
|
||
# Also with kmeans | ||
r = kmeans(img, 3) | ||
nc = last.(sort([pr for pr in segment_pixel_count(r)]; by=first)) | ||
@test sum(nc) == length(img) | ||
@test 3*minimum(nc) > maximum(nc) | ||
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