imager package in R and examples..

19
Prepared by Volkan OBAN Loading a picture (.png) on R > library(png) > img = readPNG("itu.png") > if (exists("rasterImage")) { # can plot only in R 2.11.0 and higher plot(1:2, type='n') if (names(dev.cur()) == "windows") { # windows device doesn't support semi-transparency so we'll need # to flatten the image transparent <- img[,,4] == 0 img <- as.raster(img[,,1:3]) img[transparent] <- NA rasterImage(img, 1.2, 1.27, 1.8, 1.73, interpolate=FALSE) } else { # any reasonable device will be fine using alpha rasterImage(img, 1.2, 1.27, 1.8, 1.73) rasterImage(img.n, 1.5, 1.5, 1.9, 1.8) } }

Transcript of imager package in R and examples..

Page 1: imager package in R and examples..

Prepared by Volkan OBAN

Loading a picture (.png) on R

> library(png)> img = readPNG("itu.png")

> if (exists("rasterImage")) { # can plot only in R 2.11.0 and higher

plot(1:2, type='n')

if (names(dev.cur()) == "windows") {

# windows device doesn't support semi-transparency so we'll need

# to flatten the image

transparent <- img[,,4] == 0

img <- as.raster(img[,,1:3])

img[transparent] <- NA

rasterImage(img, 1.2, 1.27, 1.8, 1.73, interpolate=FALSE)

} else {

# any reasonable device will be fine using alpha

rasterImage(img, 1.2, 1.27, 1.8, 1.73)

rasterImage(img.n, 1.5, 1.5, 1.9, 1.8)

}

}

Page 2: imager package in R and examples..

Reference: https://github.com/dahtah/imager

http://dahtah.github.io/imager/gettingstarted.html

https://www.r-bloggers.com/new-package-for-image-processing-in-r/

http://stackoverflow.com/questions/15283447/how-to-read-jpeg-in-r-2-15

imager package in R

Example:

Page 3: imager package in R and examples..

> library(imager)> im <- load.image(system.file('extdata/parrots.png',package='imager'))> layout(t(1:3)) > plot(im,main="Original image")> grad <- grayscale(im) %>% get_gradient("xy")> names(grad) <- paste("Gradient along",c("x","y")) > l_ply(names(grad),function(n) plot(grad[[n]],main=n))

Example:

> library(imager)> plot(boats)

Page 4: imager package in R and examples..

> layout(t(1:2))> plot(boats)> plot(boats/2)

Page 5: imager package in R and examples..

layout(t(1:2))> boats.s <- boats/255 #scale to 0..1> plot(boats.s,rescale=FALSE)

Page 6: imager package in R and examples..

> plot(boats.s/2,rescale=FALSE)

> rgb(0,1,0)[1] "#00FF00"

Page 7: imager package in R and examples..

> cscale <- function(r,g,b) rgb(g,r,b)> plot(boats.s,colourscale=cscale,rescale=FALSE)

> cscale <- scales::gradient_n_pal(c("red","purple","lightblue"),c(0,.5,1))

Page 8: imager package in R and examples..

> #cscale is now a function returning colour codes> cscale(0)[1] "#FF0000"> grayscale(boats) %>% plot(colourscale=cscale,rescale=FALSE)> grayscale(boats) %>% plot(colourscale=cscale,rescale=FALSE)

Page 9: imager package in R and examples..

> boats.g <- grayscale(boats)> f <- ecdf(boats.g)> plot(f,main="Empirical CDF of luminance values")> f <- ecdf(boats.g)> plot(f,main="Empirical CDF of luminance values")> f(boats.g) %>% str num [1:98304] 0.175 0.169 0.168 0.168 0.17 ...> f(boats.g) %>% as.cimg(dim=dim(boats.g)) %>% plot(main="With histogram equalisation")

Page 10: imager package in R and examples..

> gr <- imgradient(boats.g,"xy")> plot(gr,layout="row")

Page 11: imager package in R and examples..

> dx <- imgradient(boats.g,"x")> dy <- imgradient(boats.g,"y")> grad.mag <- sqrt(dx^2+dy^2)> plot(grad.mag,main="Gradient magnitude")

Page 12: imager package in R and examples..

library(ggplot2)> df <- grayscale(boats) %>% as.data.frame> p <- ggplot(df,aes(x,y))+geom_raster(aes(fill=value))> p

Page 13: imager package in R and examples..

> df <- as.data.frame(boats) > p <- ggplot(df,aes(x,y))+geom_raster(aes(fill=value))+facet_wrap(~ cc)> p+scale_y_reverse()

Page 14: imager package in R and examples..

Example:

>hub <- load.example("hubble") %>% grayscale> plot(hub,main="Hubble Deep Field")

Page 15: imager package in R and examples..

Example:

fpath <- system.file('extdata/parrots.png',package='imager')> parrots <- load.image(fpath)> plot(parrots)

Page 16: imager package in R and examples..

Example:

> library(imager)> library(purrr)

> parrots <- load.example("parrots")> plot(parrots)> #Define a function that converts to YUV, blurs a specific channel, and converts back> bchan <- function(im,ind,sigma=5) { + im <- RGBtoYUV(im)+ channel(im,ind) <- isoblur(channel(im,ind),sigma); + YUVtoRGB(im)+ }> #Run the function on all three channels and collect the results as a list> blurred <- map_il(1:3,~ bchan(parrots,.))> names(blurred) <- c("Luminance blur (Y)","Chrominance blur (U)","Chrominance blur (V)")> plot(blurred