-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ColorQuantization.jl for clustering (#98)
* Use ColorQuantization.jl for clustering * Update dependencies: * replace ImageBase with ImageCore * remove TiledIteration * remove LazyModules * drop IndirectArrays v0.5
- Loading branch information
Showing
5 changed files
with
21 additions
and
62 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
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,59 +1,27 @@ | ||
# These functions are lazily loaded from Clustering.jl using LazyModules.jl | ||
# Code adapted from @cormullion's [ColorSchemeTools](https://github.com/JuliaGraphics/ColorSchemeTools.jl). | ||
function get_colorscheme( | ||
img, | ||
ncolors; | ||
maxiter=Clustering._kmeans_default_maxiter, | ||
tol=Clustering._kmeans_default_tol, | ||
)::Vector{Lab} | ||
# Cluster in Lab color space | ||
|
||
# Clustering on the downsampled image already generates good enough colormap estimation | ||
# This significantly reduces the algorithmic complexity. | ||
img = _restrict_to(img, ncolors * 100) | ||
data = reshape(channelview(Lab.(img)), 3, :) | ||
R = Clustering.kmeans(data, ncolors; maxiter=maxiter, tol=tol) | ||
|
||
# Make color scheme out of cluster centers | ||
return [Lab(c...) for c in eachcol(R.centers)] | ||
end | ||
|
||
function _restrict_to(img, n) | ||
length(img) <= n && return img | ||
out = restrict(img) | ||
while length(out) > n | ||
out = restrict(out) | ||
end | ||
return out | ||
function _colordither(::Type{T}, img, alg, ncolors::Integer; kwargs...) where {T} | ||
quantizer = KMeansQuantization(ncolors) | ||
return _colordither(T, img, alg, quantizer; kwargs...) | ||
end | ||
|
||
function _colordither( | ||
::Type{T}, | ||
img, | ||
alg, | ||
ncolors::Integer; | ||
maxiter=Clustering._kmeans_default_maxiter, | ||
tol=Clustering._kmeans_default_tol, | ||
kwargs..., | ||
) where {T} | ||
cs = get_colorscheme(img, ncolors; maxiter=maxiter, tol=tol) | ||
function _colordither(::Type{T}, img, alg, q::AbstractColorQuantizer; kwargs...) where {T} | ||
cs = quantize(img, q) | ||
return _colordither(T, img, alg, cs; kwargs...) | ||
end | ||
|
||
""" | ||
dither!([out,] img, alg::AbstractDither, ncolors; maxiter, tol, kwargs...) | ||
Dither image `img` using algorithm `alg`. | ||
A color palette with `ncolors` is computed by Clustering.jl's K-means clustering. | ||
The amount of `maxiter` and tolerance `tol` default to those exported by Clustering.jl. | ||
A color palette of size `ncolors` is computed by ColorQuantization.jl's `KMeansQuantization`, | ||
which applies K-means clustering. | ||
""" | ||
dither!(img, alg::AbstractDither, ncolors::Integer; kwargs...) | ||
|
||
""" | ||
dither([T::Type,] img, alg::AbstractDither, ncolors; maxiter, tol, kwargs...) | ||
Dither image `img` using algorithm `alg`. | ||
A color palette with `ncolors` is computed by Clustering.jl's K-means clustering. | ||
The amount of `maxiter` and tolerance `tol` default to those exported by Clustering.jl. | ||
A color palette of size `ncolors` is computed by ColorQuantization.jl's `KMeansQuantization`, | ||
which applies K-means clustering. | ||
""" | ||
dither(::Type, img, alg::AbstractDither, ncolors::Integer; 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