Image Processing In R With The imager Package
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.
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.
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)
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)
mario_blurry2 <- isoblur(mario_img, sigma = 15) # More Blurry Mario!
plot(mario_blurry2)
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
# Split into two sub images horizontally:
imsplit(mario_img, "x", 2) %>% plot
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")
imrotate(mario_img, angle = 30) %>% plot(main="Rotated Mario")
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")
imshift(mario_img, delta_x = 150, delta_y = -150) %>% plot(main="Shifted Mario Image")
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")
imshift(mario_img, delta_x = 300, delta_y = -300, boundary = 2) %>%
plot(main="Shifted Mario Image")
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.