Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- filterImage <- function(imgArray, imgFilter) {
- x <- dim(imgArray)[1]
- y <- dim(imgArray)[2]
- z <- dim(imgArray)[3]
- dx <- floor(dim(imgFilter)[1] / 2)
- dy <- floor(dim(imgFilter)[2] / 2)
- newImgArray <- array(0, dim = c(x, y, z))
- for (i in 1:x) {
- for (j in 1:y) {
- for (k in 1:z) {
- for (p in (0 - dx):(0 + dx)) {
- for (q in (0 - dy):(0 + dy)) {
- if (i + p < 1 || i + p > x || j + q < 1 || j + q > y) {
- next
- }
- newImgArray[i, j, k] <- newImgArray[i, j, k] +
- imgArray[(i + p), (j + q), k] * imgFilter[dx + p + 1, dy + q + 1]
- }
- }
- }
- }
- }
- newImgArray
- }
- library('png')
- imgFilter <- array(1/9, dim=c(3, 3)) # mean filter
- rawImageArray <- readPNG('background.png')
- filteredImageArray <- filterImage(rawImageArray, imgFilter)
- writePNG(filteredImageArray, target = 'backgroundFiltered.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement