Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #' Pointillism filter
- #'
- #' @description Convert an image (RGB array) into array of points in a style reminiscent of pointillism
- #'
- #' @param img Image array.
- #' @param k Point size. Bigger k means bigger point / less detail.
- #' @param seed Random seed for voxel color sampling.
- #'
- #' @examples {
- #' ## Read image
- #' library(jpeg)
- #' img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
- #' ## Plot original image
- #' plot.new()
- #' lim <- par()
- #' rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
- #' ## Generate and plot pointillist image
- #' pointillism(img)
- #' }
- pointillism <- function(img, k=max(2, round(nrow(img)/100)), seed=0) {
- mat.melt <- do.call(rbind, lapply(seq(1, nrow(img)-k, by=k), function(row) {
- do.call(rbind, lapply(seq(1, ncol(img)-k, by=k), function(col) {
- voxel <- img[row:(row+k), col:(col+k),]
- set.seed(seed)
- randrow <- sample(seq_len(nrow(voxel)), 1)
- randcol <- sample(seq_len(ncol(voxel)), 1)
- c(row, col, voxel[randrow, randcol,], k)
- }))
- }))
- par(mfrow=c(1,1), mar=rep(0,4))
- plot(mat.melt[,2], rev(mat.melt[,1]),
- col=rgb(mat.melt[,c(3,4,5)]),
- pch=16,
- cex=mat.melt[vi,6]/k)
- }
Add Comment
Please, Sign In to add comment