Fast image processing for images in up to 4 dimensions (two spatial dimensions, one time/depth dimension, one colour dimension). Provides most traditional image processing tools (filtering, morphology, transformations, etc.) as well as various functions for easily analysing image data using R. The package wraps 'CImg', < http://cimg.eu>, a simple, modern C++ library for image processing.
Imager is an image/video processing package for R, based on CImg, a C++ library by David Tschumperlé. CImg provides an easy-to-use and consistent API for image processing, which imager largely replicates. CImg supports images in up to four dimensions, which makes it suitable for applications like video processing/hyperspectral imaging/MRI.
Imager is on CRAN, so
install.packages("imager")
should do the trick. You may also want to install ImageMagick and ffmpeg, see "External Dependencies" below.
The version of CRAN will often lag the one on github. If you'd like to install the latest version, you'll have to build the package from source.
Install the devtools package if you haven't already. Run:
devtools::install_github("dahtah/imager")
If that doesn't work then you're probably missing a build environment or a library, see below.
Install XQuartz if you haven't already (it's required for the interactive functions). You'll need Xcode (OS X's development environment) to compile source packages. The FFTW library is needed, and the easiest way to install it is via Homebrew. Install Homebrew, then run: brew install fftw
Optionally, install libtiff for better support of TIFF files.
Building R packages on Windows is a bit of a pain so you're probably better off with the binary package (which may not be up-to-date). If you need the latest version of imager, you'll have to:
To build under Linux make sure you have the headers for libX11 and libfftw3 (optionally, libtiff as well). On my Ubuntu system this seems to be enough:
sudo apt-get install libfftw3-dev libX11-dev libtiff-dev
OS X users need XQuartz. On its own imager supports JPEG, PNG, TIFF and BMP formats. If you need support for other file types install ImageMagick. To load and save videos you'll need ffmpeg, no file formats are supported natively.
Here's a small demo that actually demonstrates an interesting property of colour perception:
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)
We're much more sensitive to luminance edges than we are to colour edges.
Documentation is available here. To get a list of all package functions, run: ls(pos = "package:imager")
All images are stored as standard R numeric vectors (i.e., double-precision), meaning that they take up a lot of memory. It's easy to underestimate how much storage you need for videos, because they take up so little space in a compressed format. Before you can work on it in R a video has to be fully decompressed and stored as double-precision floats. To get a sense of the size, consider a low-resolution (400x300), colour video lasting 120 sec. The video might take up a few MBs when compressed. To store it in memory, you'll need: (400x300) x (25x120) x 3 values, corresponding to (space)x(time)x(colour). In addition, each value costs 8 bytes of storage, for a grand total of 8GB of memory.
For out-of-memory processing of videos, see the experimental package imagerstreams.
Imager is fully functional but still young, so the API might change. Open an issue on Github or email me if you've found a bug or would like to suggest a feature.
If you want to add features or fix a bug, just fork this repository and send me a pull request (they're welcome). Consider contributing documentation too: imager has got quite large over time, and it's in need of more how-to's and tutorials!
The package's author is Simon Barthelmé (Gipsa-lab, CNRS). The following people have gracefully contributed code, bug fixes or testing:
Let me know if you're missing from the list!
Imager ships with four test pictures and a video. Two (parrots and boats) come from the Kodak set. Another is a sketch of birds by Leonardo, from Wikimedia. Also from Wikimedia: the Hubble Deep field. The test video comes from xiph.org's collection.
Functions based on plyr are gradually being phased out in favour of ones based on purrr and dplyr. plyr is no longer loaded by default, which solves some conflicts, but may break existing code! If that's the case just add require(plyr)
somewhere.
new functions imeval and imchange. These create custom execution environments for functions, and simplify image pipelines: for example,
imeval(boats,~ xs*.)
is equivalent to: (Xc(boats)/width(boats))*boats
and creates a fading effect.
imchange(boats,~ c==1,~ xs*.)
is the same thing but applied only to the first colour channel (R)
(experimental feature) quick-and-dirty interactive interfaces are now easy to program using the interact() function. Use it to explore filter parameters, interactive segmentation. For more sophisticated needs, use e.g. shiny.
(experimental feature) as.igraph methods for images and pixsets convert images into graph representations. Nodes are pixels, and edges are drawn between neighbouring pixels. On pixsets, this allows some interesting morphological operations, e.g. contour tracing. On images, it could be used for various graph-based image processing algorithms, like spectral graph clustering
im <- boats
im[sample(nPix(im),1e4)] <- NA
inpaint(im,1) %>% imlist(im,.) %>%
setNames(c("before","after")) %>%
plot(layout="row")
isoblur now has optional na.rm argument, and can ignore missing values.
new function: colorise, to fill in regions with a certain colour. Also comes with a formula interface, just like imeval. Ex: highlight central region in image
colorise(boats,~ sqrt(xc^2+yc^2) < 140,"blue",alpha=.2) %>% plot
see ?colorise for more.
new function: load.dir, to load all images in a directory
new function: Hough transforms for circles and lines are now available, see hough_circle and hough_line
most functions that take a colour argument now accept colour names, e.g.:
imfill(10,10,val="red")
or autocrop(im,col="black")
fixed problem with recent versions of ImageMagick that weren't detected properly
* added crop.bbox for cropping image to the bounding box of a pixset
* updated CImg: fixes issues with Intel C compiler and libtiff giving pop-up warnings on Windows
* threshold now has an "adjust" argument, to adjust the auto-thresholding
* switched to C++11
* Updated CImg, imager should now compile on Solaris
* Fixed memory access bug in px.flood
imfill(10,10,val='red')
imfill(10,10)[3,1]
does what you'd expect (i.e., the same as imfill(10,10)[3,1,1,1]
)