Guest User

Untitled

a guest
Jul 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #' Pointillism filter
  2. #'
  3. #' @description Convert an image (RGB array) into array of points in a style reminiscent of pointillism
  4. #'
  5. #' @param img Image array.
  6. #' @param k Point size. Bigger k means bigger point / less detail.
  7. #' @param seed Random seed for voxel color sampling.
  8. #'
  9. #' @examples {
  10. #' ## Read image
  11. #' library(jpeg)
  12. #' img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
  13. #' ## Plot original image
  14. #' plot.new()
  15. #' lim <- par()
  16. #' rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
  17. #' ## Generate and plot pointillist image
  18. #' pointillism(img)
  19. #' }
  20. pointillism <- function(img, k=max(2, round(nrow(img)/100)), seed=0) {
  21. mat.melt <- do.call(rbind, lapply(seq(1, nrow(img)-k, by=k), function(row) {
  22. do.call(rbind, lapply(seq(1, ncol(img)-k, by=k), function(col) {
  23. voxel <- img[row:(row+k), col:(col+k),]
  24. set.seed(seed)
  25. randrow <- sample(seq_len(nrow(voxel)), 1)
  26. randcol <- sample(seq_len(ncol(voxel)), 1)
  27. c(row, col, voxel[randrow, randcol,], k)
  28. }))
  29. }))
  30. par(mfrow=c(1,1), mar=rep(0,4))
  31. plot(mat.melt[,2], rev(mat.melt[,1]),
  32. col=rgb(mat.melt[,c(3,4,5)]),
  33. pch=16,
  34. cex=mat.melt[vi,6]/k)
  35. }
Add Comment
Please, Sign In to add comment