R: Image Analysis using EBImage
[This article was first published on Analysis with Programming, 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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Currently, I am taking Statistics for Image Analysis on my masteral, and have been exploring this topic in R. One package that has the capability in this field is the EBImage from Bioconductor, which will be showcased in this post.
For those using Ubuntu, you may likely to encounter this error:
It has something to do with the
Yes, this is the photo that we are going to use for our analysis. Needless to say, that’s me and my friends. In the proceeding section we will do image manipulation and other processing.
There are two sections (Summary and array of the pixels) in the above output, with the following entries for the first section:
The second section is the obtained values from mapping pixels in the image to the real line between 0 and 1 (inclusive). Both extremes of this interval [0, 1], are black and white colors, respectively. Hence, pixels with values closer to any of these end points are expected to be darker or lighter, respectively. And because pixels are contained in a large array, then we can do all matrix manipulations available in R for processing.
Installation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source("http://bioconductor.org/biocLite.R") | |
biocLite() | |
biocLite("EBImage") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In file included from common.c:1:0: | |
common.h:5:18: fatal error: tiff.h: No such file or directory | |
#include <tiff.h> |
tiff.h
C header file, but it’s not that serious since mytechscribblings has an effective solution for this, do check that out.Importing Data
To import a raw image, consider the following codes:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reading Image | |
Image <- readImage('~/Documents/R Files/Image Analysis/tinago.JPG') | |
display(Image) |
Output of display(Image) . |
Image Properties
So what do we get from our raw image? To answer that, simply runprint(Image)
. This will return the properties of the image, including the array of pixel values. With these information, we apply mathematical and statistical operations to do enhancement on the image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(Image) | |
#OUTPUT | |
Image | |
colormode: Color | |
storage.mode: double | |
dim: 1984 1488 3 | |
nb.total.frames: 3 | |
nb.render.frames: 1 | |
imageData(object)[1:5,1:6,1]: | |
[,1] [,2] [,3] [,4] [,5] [,6] | |
[1,] 0.3647059 0.3843137 0.3803922 0.3490196 0.3215686 0.3058824 | |
[2,] 0.4196078 0.4000000 0.3921569 0.4039216 0.3843137 0.3254902 | |
[3,] 0.4784314 0.4705882 0.4705882 0.4666667 0.4117647 0.3411765 | |
[4,] 0.4901961 0.5647059 0.5960784 0.5411765 0.4392157 0.3843137 | |
[5,] 0.4509804 0.5686275 0.6549020 0.6392157 0.5607843 0.4901961 |
Code | Value | Description |
---|---|---|
Table 1: Information from 1st section of print(Image) . | ||
colormode | Color | The type (Color/Grayscale) of the color of the image. |
storage.mode | double | Type of values in the array. |
dim | 1984 1488 3 | Dimension of the array, (x, y, z). |
nb.total.frames: | 3 | Number of channels in each pixel, z entry in dim . |
nb.render.frames | 1 | Number of channels rendered. |
The second section is the obtained values from mapping pixels in the image to the real line between 0 and 1 (inclusive). Both extremes of this interval [0, 1], are black and white colors, respectively. Hence, pixels with values closer to any of these end points are expected to be darker or lighter, respectively. And because pixels are contained in a large array, then we can do all matrix manipulations available in R for processing.
Adjusting Brightness
It is better to start with the basic first, one of which is the brightness. As discussed above, brightness can be manipulated using+
or -
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Brightness | |
Image1 <- Image + 0.2 | |
Image2 <- Image - 0.2 | |
display(Image1); display(Image2) |
Lighter | Darker | ||||
---|---|---|---|---|---|
Table 2: Adjusting Brightness. | |||||
|
|
Adjusting Contrast
Contrast can be manipulated using multiplication operator(*
):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image3 <- Image * 0.5 | |
Image4 <- Image * 2 | |
display(Image3); display(Image4) |
Low | High | ||||
---|---|---|---|---|---|
Table 3: Adjusting Contrast. | |||||
|
|
Gamma Correction
Gamma correction is the name of a nonlinear operation used to code and decode luminance or tristimulus values in video or still image systems, defined by the following power-law expression: begin{equation}nonumber V_{mathrm{out}} = AV_{mathrm{in}}^{gamma} end{equation} where $A$ is a constant and the input and output values are non-negative real values; in the common case of $A = 1$, inputs and outputs are typically in the range 0-1. A gamma value $gamma< 1$ is sometimes called an encoding gamma (Wikipedia, Ref. 1).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image5 <- Image ^ 2 | |
Image6 <- Image ^ 0.7 | |
display(Image5); display(Image6) |
$gamma = 2$ | $gamma = 0.7$ | ||||
---|---|---|---|---|---|
Table 4: Adjusting Gamma Correction. | |||||
|
|
Cropping
Slicing array of pixels, simply mean cropping the image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
display(Image[289:1547, 669:1406,]) |
![]() |
Output of the above code. |
Spatial Transformation
Spatial manipulation like rotate (rotate
), flip (flip
), and translate (translate
) are also available in the package. Check this out,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imagetr <- translate(rotate(Image, 45), c(50, 0)) | |
display(Imagetr) |

Color Management
Since the array of pixels has three axes in its dimension, for example in our case is 1984 x 1488 x 3. The third axis is the slot for the three channels: Red, Green and Blue, or RGB. Hence, transforming thecolor.mode
from Color
to Grayscale
, implies disjoining the three channels from single rendered frame (three channels for each pixel) to three separate array of pixels for red, green, and blue frames.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colorMode(Image) <- Grayscale | |
print(Image) | |
#OUTPUT | |
Image | |
colormode: Grayscale | |
storage.mode: double | |
dim: 1984 1488 3 | |
nb.total.frames: 3 | |
nb.render.frames: 3 | |
imageData(object)[1:5,1:6,1]: | |
[,1] [,2] [,3] [,4] [,5] [,6] | |
[1,] 0.3647059 0.3843137 0.3803922 0.3490196 0.3215686 0.3058824 | |
[2,] 0.4196078 0.4000000 0.3921569 0.4039216 0.3843137 0.3254902 | |
[3,] 0.4784314 0.4705882 0.4705882 0.4666667 0.4117647 0.3411765 | |
[4,] 0.4901961 0.5647059 0.5960784 0.5411765 0.4392157 0.3843137 | |
[5,] 0.4509804 0.5686275 0.6549020 0.6392157 0.5607843 0.4901961 |
Original | Red Channel | |
---|---|---|
Table 5: Color Mode Transformation. | ||
![]() | ![]() | |
Green Channel | Blue Channel | |
![]() | ![]() |
To revert the color mode, simply run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colorMode(Image) <- Color |
Filtering
In this section, we will do smoothing/blurring using low-pass filter, and edge-detection using high-pass filter. In addition, we will also investigate median filter to remove noise.Low-Pass (Blur) | ||||||||
---|---|---|---|---|---|---|---|---|
Table 6: Image Filtering. | ||||||||
![]()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ||||||||
High Pass | ||||||||
![]()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Original | Filtered | ||||
---|---|---|---|---|---|
Table 7: Median Filter. | |||||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image <- readImage("~/Documents/R Files/Image Analysis/peppersalt.jpg") | |
medFltr <- medianFilter(Image, 1.1) | |
display(medFltr) |

For the meantime, we will stop here, but hoping we can play more on this topic in the succeeding post.
References
- Gamma Correction. Wikipedia. Retrieved August 31, 2014.
- Gregoire Pau, Oleg Sklyar, Wolfgang Huber (2014). Introduction to EBImage, an image processing and analysis toolkit for R.
To leave a comment for the author, please follow the link and comment on their blog: Analysis with Programming.
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.