Skip to content

Commit 24ed751

Browse files
authored
Fix KMeansQuantization on fixed-point numbers (#14)
1 parent 35e722b commit 24ed751

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

src/clustering.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ end
5454

5555
function _kmeans(alg::KMeansQuantization, cs::AbstractArray{<:Colorant{T,N}}) where {T,N}
5656
data = reshape(channelview(cs), N, :)
57-
R = kmeans(
57+
result = kmeans(
5858
data, alg.ncolors; maxiter=alg.maxiter, tol=alg.tol, init=alg.init, rng=alg.rng
5959
)
60-
return colorview(eltype(cs), R.centers)
60+
centers = convert.(T, result.centers)
61+
return colorview(eltype(cs), centers)
6162
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[deps]
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
4+
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
45
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
56
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
67
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
████████
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
████████

test/runtests.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using ColorQuantization
2-
using Test
3-
using Aqua
2+
using Test, Aqua
43
using TestImages, ReferenceTests
5-
using Colors
64
using Random, StableRNGs
5+
using Colors: RGB, HSV
6+
using FixedPointNumbers: N0f8
77

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

1616
img = testimage("peppers")
17+
img_N0f8 = convert.(RGB{N0f8}, img)
1718

1819
algs_deterministic = Dict(
1920
"UniformQuantization4" => UniformQuantization(4),
20-
"KMeansQuantization8" => KMeansQuantization(8; rng=rng),
21+
"KMeansQuantization8" => KMeansQuantization(8; rng=rng),
22+
"KMeansQuantization8_N0f8" => KMeansQuantization(RGB{N0f8}, 8; rng=rng),
2123
)
2224

2325
@testset "ColorQuantization.jl" begin

0 commit comments

Comments
 (0)