Last active
July 30, 2018 12:48
-
-
Save JEFworks/a15db6442595768a5c9f9925ad4449c6 to your computer and use it in GitHub Desktop.
Pointillism filter: Convert an image (RGB array) into array of points in a style reminiscent of pointillism
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
#' 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[,6]/k) | |
} |
Yes, Now it worked. Thanks a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following error came while running the function.
Error in mat.melt[vi, 6] : invalid subscript type 'closure'