21 July | BioC 2015

Goals for this workshop

  • Learn how to read and write images in and out of R

  • Learn how images are represented in R and how to manipulate them

  • Understand how to apply filters and transformations to images

  • Apply these skills to microscopy images of cells to do segmentation and feature extraction

  • Explore spatial distributions of the position of cells

EBImage

Image processing and analysis toolbox for R

  • Reading and writing of image files
  • Interactive image viewer
  • Image manipulation, transformation and filtering
  • Object detection and feature extraction


Since Bioconductor 1.8 (2006)

Original developers:

Oleg Sklyar
Wolfgang Huber
Mike Smith
Gregoire Pau

Contributors:

Joseph Barry
Bernd Fischer
Ilia Kats
Philip A. Marais

Let's get started!

library(EBImage)

f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)

display(img)

Reading and displaying images

Reading images

Images can be read from local files or URLs.

bioc = readImage("http://www.bioconductor.org/images/logo/jpg/bioconductor_logo_rgb.jpg")
display(bioc)
EBImage supports JPEG, PNG and TIFF file formats.
For reading proprietary microscopy image data and metadata use RBioFormats.

Displaying images

  • interactive JavaScript viewer
  • R's build-in plotting device

The default display method can be set by options(EBImage.display).

options(EBImage.display = "raster")

Adding text labels

display(img, method = "raster")
text(x = 20, y = 20, label = "Parrots", adj = c(0,1), col = "orange", cex = 2)
filename = "parrots.jpg"
dev.print(jpeg, filename = filename , width = dim(img)[1], height = dim(img)[2])
file.size(filename)
## [1] 37820

Writing images

Supported file formats: JPEG, PNG and TIFF.

writeImage(img, "sample.jpeg", quality = 85)

writeImage(img, "sample.tiff")
writeImage(img, "sample_compressed.tiff", compression = "deflate")

files = list.files(pattern = "sample*")
data.frame(row.names=files, size=file.size(files))
##                          size
## sample.jpeg             49688
## sample.tiff            393384
## sample_compressed.tiff 293788

Image representation

Multi-dimensional pixel intensity arrays 

  • (x, y)
  • (x, y, z) z-stack
  • (x, y, t) time-lapse
  • (x, y, c) channels
  • (x, y, c, z, t, …)

Image representation

str(img)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:768, 1:512] 0.447 0.451 0.463 0.455 0.463 ...
##   ..@ colormode: int 0
getClassDef("Image")
## Class "Image" [package "EBImage"]
## 
## Slots:
##                           
## Name:      .Data colormode
## Class:     array   integer
## 
## Extends: 
## Class "array", from data part
## Class "structure", by class "array", distance 2
## Class "vector", by class "array", distance 3, with explicit coerce
dim(img)
## [1] 768 512

Image summary

img
## Image 
##   colorMode    : Grayscale 
##   storage.mode : double 
##   dim          : 768 512 
##   frames.total : 1 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:6]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.4470588 0.4627451 0.4784314 0.4980392 0.5137255 0.5294118
## [2,] 0.4509804 0.4627451 0.4784314 0.4823529 0.5058824 0.5215686
## [3,] 0.4627451 0.4666667 0.4823529 0.4980392 0.5137255 0.5137255
## [4,] 0.4549020 0.4666667 0.4862745 0.4980392 0.5176471 0.5411765
## [5,] 0.4627451 0.4627451 0.4823529 0.4980392 0.5137255 0.5411765
imageData(img)[1:3, 1:6]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.4470588 0.4627451 0.4784314 0.4980392 0.5137255 0.5294118
## [2,] 0.4509804 0.4627451 0.4784314 0.4823529 0.5058824 0.5215686
## [3,] 0.4627451 0.4666667 0.4823529 0.4980392 0.5137255 0.5137255

Image histogram

hist(img)
range(img)
## [1] 0 1

Color images

f = system.file("images", "sample-color.png", package="EBImage")
imgcol = readImage(f)
display(imgcol)
print(imgcol, short = TRUE)
## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 768 512 3 
##   frames.total : 3 
##   frames.render: 1

Image stacks

nuc = readImage(system.file("images", "nuclei.tif", package="EBImage"))
print(nuc, short = TRUE)
## Image 
##   colorMode    : Grayscale 
##   storage.mode : double 
##   dim          : 510 510 4 
##   frames.total : 4 
##   frames.render: 4
display(nuc)
## The image contains more than one frame: only the first one is displayed.
## To display all frames use 'all = TRUE'.