Image Processing In R With The imager Package

in #programming7 years ago (edited)

Hi everyone. This post is about me playing around with image processing and the imager package in R. Someone on Linkedin asked me how to work with images in R. I didn't have a solution from the top of my head but a quick Google search led me to this package called imager in R.

I try out some of the functions from this imager package.


Thumbnail Image Source

Sections


  • Setup
  • Some Functions
  • My Thoughts

Setup


The setup does require some file management. When you work with your image file, make sure it is in the right folder. The image you work with would be in a folder named something like C:\...\R\win-library\3.4\imager\extdata.

I am (mostly) working with this Mario image.



Mario Image


Here is the code for plotting the Mario image into R.

# Imager Package For Image Processing In R [Experimentation]
# References:
# https://cran.r-project.org/web/packages/imager/vignettes/gettingstarted.html
# https://dahtah.github.io/imager/
# https://dahtah.github.io/imager/imager.html
# Mario Image 1: https://supermariorun.com/img/stage/mario03.png
# Mario Image 2: https://upload.wikimedia.org/wikipedia/en/9/99/MarioSMBW.png
# R Documentation

library(imager)

# Saved image into the folder C:\...\R\win-library\3.4\imager\extdata folder:

sample_image <- system.file('extdata/mario03.png',package='imager')

mario_img <- load.image(sample_image)

plot(mario_img)

marioPlot01.png

I don't know why the background got tampered with. Ahhhhhh!

Some Functions


Blurry Mario

The isoblur() function from imager makes the image more blurry based on increasing values for sigma.

### Make Image blurry:
# isoblur(im, sigma, neumann = TRUE, gaussian = FALSE)
# sigma = standard deviation of the blur

mario_blurry <- isoblur(mario_img, sigma = 5) #Blurry Mario.
plot(mario_blurry)

blurryMario01.png

mario_blurry2 <- isoblur(mario_img, sigma = 15) # More Blurry Mario!
plot(mario_blurry2)

blurryMario02.png


Image Co-ordinates

> # Image Coordinates:
> 
> width(mario_img) # Horizontal on x axis
[1] 727
> 
> height(mario_img) # Vertical on y axis
[1] 898
> 
> depth(mario_img) # z axis
[1] 1

Image Splitting

Images can be split into pieces with the imsplit() function. The %>% pipe operator is used for coding convenience.

# Image splits:

# Split into three sub images vertically:

imsplit(mario_img, "y", 3) %>% plot

splitMario01.png

# Split into two sub images horizontally:

imsplit(mario_img, "x", 2) %>% plot

splitMario02.png


Image Rotations

# Rotate image: 
# imrotate(im, angle, cx, cy, interpolation = 1L, boundary = 0L)
# angle in degrees

imrotate(mario_img, angle = 15) %>% plot(main="Rotated Mario")

rotatedMario01.png

imrotate(mario_img, angle = 30) %>% plot(main="Rotated Mario")

rotatedMario02.png


Image Shifting

Image shifting can be achieved with the imshift() function from imager.

# Shifting:
#imshift(im, delta_x = 0L, delta_y = 0L, delta_z = 0L, delta_c = 0L, boundary_conditions = 0L)

imshift(mario_img, delta_x = 50, delta_y = -100) %>% plot(main="Shifted Mario Image")

shiftedMario01.png

imshift(mario_img, delta_x = 150, delta_y = -150) %>% plot(main="Shifted Mario Image")

shiftedMario02.png


Repeated Patterns & Image Shifts

From the imshift() function, you can achieve repeated patterns and image shift by using the boundary argument.

# Repeated Pattern Shifting:

imshift(mario_img, delta_x = 400, delta_y = -200, boundary = 2) %>% 
  plot(main="Shifted Mario Image")

repeatMario01.png

imshift(mario_img, delta_x = 300, delta_y = -300, boundary = 2) %>% 
  plot(main="Shifted Mario Image")

repeatMario02.png

My Thoughts


  • Although image processing is not my speciality, I had an interesting time with this imager package in R.
  • I do think there are other programs out there that do a better job with handling images. MATLAB comes to mind as one choice.
  • My main reference is this link: https://dahtah.github.io/imager/imager.html and the R documentation.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 57850.91
ETH 2358.42
USDT 1.00
SBD 2.43