View source: R/generic_support.r
index.colours | R Documentation |
Converts an image represented by a grDevices raster
object into a
matrix containing numeric indices of a quantised colour palette.
index.colours(
x,
length.out = 8,
palette = NULL,
background = "#FFFFFF",
dither = c("none", "floyd-steinberg", "JJN", "stucki", "atkinson", "burkse", "sierra",
"two-row-sierra", "sierra-lite"),
colour.depth = c("12 bit", "24 bit"),
...
)
x |
A raster object ( |
length.out |
A It can also be a |
palette |
A vector of no more than |
background |
On the Amiga, indexed images could not be semi-transparent. Only a single colour could be designated as being fully transparent. The “background'' argument should contain a background colour with which semi-transparent colours should be mixed, before colour quantisation. It is white by default. |
dither |
Dither the output image using the algorithm specified here.
See the usage section for possible options. By default no dithering (" |
colour.depth |
A This argument is overruled when |
... |
Arguments that are passed onto |
Determines the optimal limited palette by clustering colours in an image
with stats::kmeans()
. The result of the optimisation routine
will depend on the randomly chosen cluster centres by this algorithm. This
means that the result may slightly differ for each call to this function. If
you want reproducible results, you may want to reset the random seed
(set.seed()
) before each call to this function.
Returns a matrix
with the same dimensions as x
containing
numeric
index values. The corresponding palette is returned as attribute,
as well as the index value for the fully transparent colour in the palette.
When x
is a list
a list
of matrices is returned.
Pepijn de Vries
Other colour.quantisation.operations:
dither()
Other raster.operations:
AmigaBitmapFont
,
as.raster.AmigaBasicShape()
,
bitmapToRaster()
,
dither()
,
rasterToAmigaBasicShape()
,
rasterToAmigaBitmapFont()
,
rasterToBitmap()
,
rasterToHWSprite()
,
rasterToIFF()
## Not run:
## first: Let's make a raster out of the 'volcano' data, which we can use in the example:
volcano.raster <- as.raster(t(matrix(terrain.colors(1 + diff(range(volcano)))[volcano -
min(volcano) + 1], nrow(volcano))))
## This will create an image of the original raster using an indexed palette:
volcano.index <- index.colours(volcano.raster)
## The index values can be converted back into colours, using the palette:
volcano.index <- as.raster(apply(volcano.index, 2,
function(x) attributes(volcano.index)$palette[x]))
## Create an indexed image using dithering
volcano.dith <- index.colours(volcano.raster, dither = "floyd-steinberg")
volcano.dith <- as.raster(apply(volcano.dith, 2,
function(x) attributes(volcano.dith)$palette[x]))
## plot the images side by side for comparison
par(mfcol = c(1, 3))
plot(volcano.raster, interpolate = F)
plot(volcano.index, interpolate = F)
plot(volcano.dith, interpolate = F)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.