Skip to content

Commit

Permalink
Fix KMeansQuantization on fixed-point numbers (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill authored Aug 7, 2023
1 parent 35e722b commit 24ed751
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/clustering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ end

function _kmeans(alg::KMeansQuantization, cs::AbstractArray{<:Colorant{T,N}}) where {T,N}
data = reshape(channelview(cs), N, :)
R = kmeans(
result = kmeans(
data, alg.ncolors; maxiter=alg.maxiter, tol=alg.tol, init=alg.init, rng=alg.rng
)
return colorview(eltype(cs), R.centers)
centers = convert.(T, result.centers)
return colorview(eltype(cs), centers)
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Expand Down
1 change: 1 addition & 0 deletions test/references/KMeansQuantization8_N0f8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
████████
1 change: 1 addition & 0 deletions test/references/KMeansQuantization8_N0f8_HSV.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
████████
10 changes: 6 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using ColorQuantization
using Test
using Aqua
using Test, Aqua
using TestImages, ReferenceTests
using Colors
using Random, StableRNGs
using Colors: RGB, HSV
using FixedPointNumbers: N0f8

# Run Aqua.jl quality assurance tests
Aqua.test_all(ColorQuantization; ambiguities=false)
Expand All @@ -14,10 +14,12 @@ rng = StableRNG(123)
Random.seed!(rng, 34568)

img = testimage("peppers")
img_N0f8 = convert.(RGB{N0f8}, img)

algs_deterministic = Dict(
"UniformQuantization4" => UniformQuantization(4),
"KMeansQuantization8" => KMeansQuantization(8; rng=rng),
"KMeansQuantization8" => KMeansQuantization(8; rng=rng),
"KMeansQuantization8_N0f8" => KMeansQuantization(RGB{N0f8}, 8; rng=rng),
)

@testset "ColorQuantization.jl" begin
Expand Down

0 comments on commit 24ed751

Please sign in to comment.