## 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))))
## let's dither the image, using a predefined two colour palette:
volcano.dither <- dither(volcano.raster,
method = "floyd-steinberg",
palette = c("yellow", "green"))
## Convert the indices back into a raster object, such that we can plot it:
volcano.dither <- as.raster(apply(volcano.dither, 2, function(x) c("yellow", "green")[x]))
par(mfcol = c(1, 2))
plot(volcano.raster, interpolate = FALSE)
plot(volcano.dither, interpolate = FALSE)
## results will get better when a better matching colour palette is used.
## for that purpose use the function 'index.colours'.
Run the code above in your browser using DataLab