Advertisement
cwchen

naive image filter

Nov 14th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.92 KB | None | 0 0
  1. filterImage <- function(imgArray, imgFilter) {
  2.   x <- dim(imgArray)[1]
  3.   y <- dim(imgArray)[2]
  4.   z <- dim(imgArray)[3]
  5.  
  6.   dx <- floor(dim(imgFilter)[1] / 2)
  7.   dy <- floor(dim(imgFilter)[2] / 2)
  8.  
  9.   newImgArray <- array(0, dim = c(x, y, z))
  10.   for (i in 1:x) {
  11.     for (j in 1:y) {
  12.       for (k in 1:z) {
  13.         for (p in (0 - dx):(0 + dx)) {
  14.           for (q in (0 - dy):(0 + dy)) {
  15.             if (i + p < 1 || i + p > x || j + q < 1 || j + q > y) {
  16.               next
  17.             }
  18.             newImgArray[i, j, k] <- newImgArray[i, j, k] +
  19.               imgArray[(i + p), (j + q), k] * imgFilter[dx + p + 1, dy + q + 1]
  20.           }
  21.         }
  22.       }
  23.     }
  24.   }
  25.   newImgArray
  26. }
  27.  
  28. library('png')
  29. imgFilter <- array(1/9, dim=c(3, 3)) # mean filter
  30. rawImageArray <- readPNG('background.png')
  31. filteredImageArray <- filterImage(rawImageArray, imgFilter)
  32. writePNG(filteredImageArray, target = 'backgroundFiltered.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement