Introducing imagemetrics

September 25, 2013

(This article was first published on Just Another R Blog, and kindly contributed to R-bloggers)


In my recent projects, I had the opportunity to work with the professor Raphaël Proulx who introduced me to several metrics commonly used in landscape ecology for quantifying image texture. In order to make my life easier, I decided to implement them as a R package. At this time, the package is still under development and so better documentation/debugging/testing are still needed. Nevertheless, here is a short tutorial on how to use the package.

Step 1: Install the package

The package is currently hosted on Bitbucket and can be installed using devtools.


install_bitbucket("imagemetrics", "persican")

Step 2: Open a raster image

Indeed, the first thing to do is to open the image on which you want to calculate the metrics.

## Open the R logo and average on calculate the average on R,G,B channels
r = brick(system.file("external/rlogo.grd", package = "raster"))
r = mean(r)

## Plot the raster (optional)
plot(r, useRaster = FALSE, col = gray((0:100)/100))

plot of chunk chunk_9b

Step 3: Extract pixels from the image to calculate occurrence probabilities

It is worth mentioning that the metrics are calculated on a probability matrix that represents the chances of getting a specific pair of pixel values. For each pixel in the image, we have to choose a neighbor that is located either to the right, bottom right or bottom of a reference pixel (see the following image).

Before calculating such probabilities, we have to extract the values of both reference and neighbor pixels. To do so, the user can use getImagePixels(r, side). The function takes as parameters a raster image (r) and side, a numeric value specifying the neighbor to use. side = 1 for the right pixel, side = 2 for the lower right pixel, side = 3 the bottom pixel.

## Get bottom pixel (side = 3)
v = getImagePixels(r, side = 3)

## List of 2
## $ reference_vector: num [1:7676] 255 255 255 255 255 255 255 255 255 255 ...
## $ neighbour_vector: num [1:7676] 255 255 255 255 255 255 255 255 255 255 ...

Step 4: Calculate occurrence probabilities

To calculate the probabilities, simply use calculateHisto(reference_vector, neighbour_vector, nbins, where reference_vector and neighbour_vector are vectors returned by getImagePixels and nbins is a numerical value indicating the bin size used to compute histograms.

From Mellin et al. 2012:

“For \( M \) classes of values, the number of possible configurations in a \( k-pixel \) neighborhood is \( M^k \). To ensure that each possible configuration has a reasonable probability of occurring in an image, it is generally recommended that the ratio of the total number of pixels in the image to \( M^k \) be greater than 100.”

suggestMaximumBins® can be used to determine the number of maximum bins to use.

## [1] 8

## Calculate probability
r.prob = calculateHisto(reference_vector = v$reference_vector, neighbour_vector = v$neighbour_vector,
nbins = 6)

Step 5: Compute the metrics

Metrics are thereafter calculated using the object returned by calculateHisto.

## [1] 0.3469
## [1] 0.02727
## [1] 0.8393
## [1] 0.848
## [1] 2.341
## [1] 1.504
## [1] 0.3724
## [1] 0.4669


Mellin, C., Parrott, L., Andréfouët, S., Bradshaw, C.J.A. a, MacNeil, M.A. & Caley, M.J. (2012) Multi-scale marine biodiversity patterns inferred efficiently from habitat image processing. Ecological Applications, 22, 792–803.

Proulx, R. & Parrott, L. (2008) Measures of structural complexity in digital images for monitoring the ecological signature of an old-growth forest ecosystem. Ecological Indicators, 8, 270–284.

To leave a comment for the author, please follow the link and comment on their blog: Just Another R Blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)