imager, an R package for image processing, has been updated to v0.20 on CRAN. It’s a major upgrade with a lot of new features, better documentation and a more consistent API.
imager now has 130 functions, and I myself keep forgetting all that’s in there. I’ve added a tutorial vignette that should help you get started. It goes through a few basic tasks like plotting and histogram equalisation and builds up to a multi-scale blob detector. It also covers plotting with ggplot2 and has a thematic list of functions.
New features added in the last months include new assignment functions, a utility for getting information on image files (iminfo), auto-thresholding based on k-means, much better array subset operators, updated docs and a reorganised codebase. Windows support should also have improved. Last but not least, you can now interrupt lengthy computations by hitting Ctrl+c or the stop button in RStudio.
imager now has some easy-to-use replacement functions, meaning you can now do set image channels or change frames using a convenient R-like syntax:
library(imager) boats.cp = boats #Make a copy of the boats image R(boats.cp) = 0 #Set red channel to 0 G(boats.cp) = 0 #Set blue channel to 0 plot(boats.cp,main="Just the blue channel") R(boats.cp) = G(boats) G(boats.cp) = R(boats) plot(boats.cp,main="Swapping red and green channels")
see ?imager.replace for more.
Auto-thresholding finds an optimal threshold for converting an image to binary values, based on k-means (it’s essentially a variant of Otsu’s method).
Here’s an illustration on a sketch by Thomas Gainsborough:
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Study_of_willows_by_Thomas_Gainsborough.jpg/375px-Study_of_willows_by_Thomas_Gainsborough.jpg" im <- load.image(url) layout(t(1:2)) plot(im) grayscale(im) %>% threshold %>% plot
Point-wise reductions are useful for combining a list of images into a single output image. For example, enorm(list(A,B,C)) computes , ie. the Euclidean norm. Here’s how you can use it to compute gradient magnitude:
imgradient(im,"xy") %>% enorm %>% plot("Gradient magnitude")
See also parmax, parmin, add, etc .
A note on compiling imager: if for some reason R tries to install imager from source (Linux or Mac), you will need the fftw library. On a Mac the easiest way is to grab it via Homebrew (“brew install fftw”), in Ubuntu “sudo apt-get install libfftw3-dev” should do it.
Hi,
I was wondering if you might have an example available of reading an AVI file frame-by-frame (assuming ffmpeg is installed)? Thanks!
You can do that using the imagerstreams package: https://github.com/dahtah/imagerstreams. It’s pretty rough around the edges but should do the job. It’s not on CRAN, you’ll have to compile from source using devtools::install_github(“dahtah/imagerstreams”)
Example:
library(imagerstreams)
fpath <- system.file("extdata/tennis_sif.mpeg",package="imager")
vd <- openvideo(fpath)
frame1 <- getFrame(vd)
frame2 <- getFrame(vd)
etc.
You can also try the videoplayR package by Simon Garnier (https://github.com/swarm-lab/videoplayR). If you need to convert a videoplayer image to something imager can work with use:
im.imager = img2r(im.vplayer) %>% as.cimg %>% imrotate(90) %>% mirror("x")
Hope that helps.
Great – thank you for those pointers.
Thanks for your reply. For what it is worth I had some trouble getting imagerstreams to work (I’m on a Windows install). Instead, to get the frames out of an AVI file I did the following (since ffmpeg was already installed) :
library(imager)
mov2 <- 'm02.avi'
imgcmd = paste('ffmpeg -i ',mov2,' -y -vcodec png -vframes 12 -f image2 imagefile%05d.png',sep='')
system(imgcmd, wait=FALSE)
imgfiles <- list.files(pattern='^imagefile.*\\.png$')
for (i in 1:12) {
im <- load.image(imgfiles[i])
plot(im)
}
This takes the AVI file "m02.avi" – which is in the current working directory, uses ffmpeg to pull the first 12 frames out as PNG files, saves them according the the naming convention "imagefilexxxxx.png", creates a list of the file names created and loops through them, displaying each.
So now I can do a variety of analyses on each frame and deal with them in whatever sequence I want.
Regards,
Matt