# Introducing imagemetrics

**Just Another R Blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

library(devtools)<br /><br />install_bitbucket("imagemetrics", "persican")<br />library(imagemetrics)<br />

**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<br />r = brick(system.file("external/rlogo.grd", package = "raster"))<br />r = mean(r)<br /><br />## Plot the raster (optional)<br />plot(r, useRaster = FALSE, col = gray((0:100)/100))<br />

**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)<br />v = getImagePixels(r, side = 3)<br /><br />str(v)<br />

## List of 2<br />## $ reference_vector: num [1:7676] 255 255 255 255 255 255 255 255 255 255 ...<br />## $ neighbour_vector: num [1:7676] 255 255 255 255 255 255 255 255 255 255 ...<br />

**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.

suggestMaximumBins(r)<br />

## [1] 8<br />

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

**Step 5: Compute the metrics**

Metrics are thereafter calculated using the object returned by *calculateHisto*.

contagion(r.prob)<br />

## [1] 0.3469<br />

contrast(r.prob)<br />

## [1] 0.02727<br />

evenness(r.prob)<br />

## [1] 0.8393<br />

homogeneity(r.prob)<br />

## [1] 0.848<br />

jointEntropy(r.prob)<br />

## [1] 2.341<br />

marginalEntropy(r.prob)<br />

## [1] 1.504<br />

maximumMutualInformation(r.prob)<br />

## [1] 0.3724<br />

meanInformationGain(r.prob)<br />

## [1] 0.4669<br />

## References

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, 792803.

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, 270284.

**leave a comment**for the author, please follow the link and comment on their blog:

**Just Another R Blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.