Uses KMeans clustering to determine color clusters that minimize the sum of distances between pixels and their assigned clusters. Useful for parsing common color motifs in an object.
getKMeanColors(
path,
n = 10,
sample.size = 20000,
plotting = TRUE,
lower = c(0, 0.55, 0),
upper = c(0.24, 1, 0.24),
iter.max = 50,
nstart = 5,
return.clust = TRUE,
color.space = "rgb",
from = "sRGB",
ref.white
)
Path to an image (JPG or PNG).
Number of KMeans clusters to fit. Unlike getImageHist
,
this represents the actual final number of bins, rather than the number of
breaks in each channel.
Number of pixels to be randomly sampled from filtered pixel
array for performing fit. If set to FALSE
, all pixels are fit, but
this can be time-consuming, especially for large images.
Logical. Should the results of the KMeans fit (original image + histogram of colors and bin sizes) be plotted?
RGB triplet specifying the lower bounds for background pixels. Default upper and lower bounds are set to values that work well for a bright green background (RGB [0, 1, 0]).
RGB triplet specifying the upper bounds for background pixels. Default upper and lower bounds are set to values that work well for a bright green background (RGB [0, 1, 0]). Determining these bounds may take some trial and error, but the following bounds may work for certain common background colors:
Black: lower=c(0, 0, 0); upper=c(0.1, 0.1, 0.1)
White: lower=c(0.8, 0.8, 0.8); upper=c(1, 1, 1)
Green: lower=c(0, 0.55, 0); upper=c(0.24, 1, 0.24)
Blue: lower=c(0, 0, 0.55); upper=c(0.24, 0.24, 1)
If no background filtering is
needed, set bounds to some non-numeric value (NULL
, FALSE
,
"off"
, etc); any non-numeric value is interpreted as NULL
.
Inherited from kmeans
. The maximum
number of iterations allowed.
Inherited from kmeans
. How many random
sets should be chosen?
Logical. Should clusters be returned? If FALSE
,
results are plotted but not returned.
The color space ("rgb"
, "hsv"
, or
"lab"
) in which to cluster pixels.
Display color space of image if clustering in CIE Lab space, probably either "sRGB" or "Apple RGB", depending on your computer.
The reference white passed to
convertColorSpace
; must be specified if using CIE
Lab space. See convertColorSpace.
A kmeans
fit object.
# NOT RUN {
colordistance::getKMeanColors(system.file("extdata",
"Heliconius/Heliconius_B/Heliconius_07.jpeg", package="colordistance"), n=3,
return.clust=FALSE, lower=rep(0.8, 3), upper=rep(1, 3))
# }
Run the code above in your browser using DataLab