Learn R Programming

⚠️There's a newer version (1.0.3) of this package.Take me there.

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.

Installing the package

Imager is now 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.

OS X

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

Windows

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:

  • Install Rtools
  • Install additional libraries for Rtools. You want the package that's called "local tree". Put those libraries somewhere gcc can find them.

Linux

To build under Linux make sure you have the headers for libX11 and libfftw3. On my Ubuntu system this seems to be enough:

sudo apt-get install libfftw3-dev libX11-dev

External dependencies

On its own imager only supports JPEG, PNG and BMP formats. If you need support for other file types install ImageMagick. To load videos you'll need ffmpeg, no file formats are supported natively.

Getting started

tennis <- load.image(system.file('extdata/tennis_sif.mpeg',package='imager'))
play(tennis)
#now filter in the time direction and pipe to play
deriche(tennis,10,axis="z") %>% play

Documentation is available here. To get a list of all package functions, run: ls(pos = "package:imager")

Important warning on memory usage

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.

Current status

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.

Test pictures

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.

Copy Link

Version

Install

install.packages('imager')

Monthly Downloads

12,098

Version

0.20

License

LGPL

Maintainer

Simon Barthelme

Last Published

April 28th, 2016

Functions in imager (0.20)

at

Return pixel value at coordinates
cimg.dimensions

Image dimensions
distance_transform

Compute Euclidean distance function to a specified value.
deriche

Apply recursive Deriche filter.
grayscale

Convert an RGB image to grayscale
get_hessian

Return image hessian.
iminfo

Return information on image file
mirror

Mirror image content along specified axis
get_gradient

Compute image gradient.
idply

Split an image along axis, apply function, return a data.frame
bucketfill

Bucket fill
autocrop

Autocrop image region
pad

Pad image with n pixels along specified axis
boats

Photograph of sailing boats from Kodak set
blur_anisotropic

Blur image anisotropically, in an edge-preserving way.
imhessian

Compute image hessian.
display

Display image using CImg library
play

Play a video
iiply

Split an image, apply function, recombine the results as an image
add.colour

Add colour channels to an grayscale image
cimg.extract

Various shortcuts for extracting colour channels, frames, etc
correlate

Correlation of image by filter
erode

Erode/dilate image by a structuring element.
imager.replace

Replace part of an image with another
frames

Split a video into separate frames
get.stencil

Return pixel values in a neighbourhood defined by a stencil
ilply

Split an image along axis, apply function, return a list
as.data.frame.cimg

Convert a pixel image to a data.frame
isoblur

Blur image isotropically.
load.example

Load example image
stencil.cross

A cross-shaped stencil
selectSimilar

Select a region of homogeneous colour
threshold

Threshold grayscale image
convolve

Convolve image by filter.
imnoise

Generate (Gaussian) white-noise image
watershed

Compute watershed transform.
save.image

Save image
center.stencil

Center stencil at a location
imwarp

Image warping
RGBtoHSL

Colour space conversions in imager
rotate_xy

Rotate image by an arbitrary angle, around a center point.
as.cimg.data.frame

Create an image from a data.frame
imsplit

Split an image along a certain axis (producing a list)
extract_patches

Return image patches
label

Label connected components.
imrotate

Rotate image by an arbitrary angle.
plot.cimg

Display an image using base graphics
pixel.grid

Return the pixel grid for an image
medianblur

Blur image with the median filter.
resize_doubleXY

Resize image uniformly
boxblur

Blur image with a box filter (square window)
imdirac

Generates a "dirac" image, i.e. with all values set to 0 except one.
imgradient

Compute image gradient
as.raster.cimg

Convert a cimg object to a raster object
as.cimg.function

Create an image by sampling a function
permute_axes

Permute image axes
imager

imager: an R library for image processing, based on CImg
diffusion_tensors

Compute field of diffusion tensors for edge-preserving smoothing.
imappend

Combine a list of images into a single image
imager.subset

Array subset operator for cimg objects
squeeze

Remove empty dimensions from an array
FFT

Compute the Discrete Fourier Transform of an image
haar

Compute Haar multiscale wavelet transform.
liply

Apply function to each element of a list, then combine the result as an image by appending along specified axis
pixel.index

Linear index in internal vector from pixel coordinates
im2cimg

Convert an image in spatstat format to an image in cimg format
as.cimg

Convert to cimg object
imshift

Shift image content.
interp

Interpolate image values
capture.plot

Capture the current R plot device as a cimg image
channels

Split a colour image into a list of separate channels
display_list

Display image list using CImg library
cimg2im

Convert cimg to spatstat im object
periodic.part

Compute the periodic part of an image, using the periodic/smooth decomposition of Moisan (2009)
vanvliet

Young-Van Vliet recursive Gaussian filter.
boxblur_xy

Blur image with a box filter.
displacement

Estimate displacement field between two images.
renorm

Renormalise image
resize

Resize image to new dimensions. If pd[x,y,z,v]<0, it corresponds to a percentage of the original size (the default value is -100).
warp

Warp image
get.locations

Return coordinates of subset of pixels
imager.combine

Combining images
crop.borders

Crop the outer margins of an image
imfill

Create an image of custom size by filling in repeated values
imsharpen

Sharpen image.
imdraw

Draw image on another image
as.cimg.array

Turn an numeric array into a cimg object
cimg

Create a cimg object
imsub

Select part of an image
im_split

Split an image along a certain axis (producing a list)
load.image

Load image from file or URL