RNotes | EBImage


library(EBImage)

Color and Image Color Mode Conversions

“Two Macaws” from Kodak True Color Image Suite http://r0k.us/graphics/kodak/ http://r0k.us/graphics/kodak/kodim23.html

macaws <- readImage(system.file("images", "sample-color.png", package="EBImage"))
str(macaws)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:768, 1:512, 1:3] 0.455 0.459 0.471 0.467 0.471 ...
##   ..@ colormode: int 2
display(macaws)

redPlane   <- channel(macaws, "red")
greenPlane <- channel(macaws, "green")
bluePlane  <- channel(macaws, "blue")

str(redPlane)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:768, 1:512] 0.455 0.459 0.471 0.467 0.471 ...
##   ..@ colormode: int 0
str(greenPlane)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:768, 1:512] 0.455 0.459 0.471 0.463 0.471 ...
##   ..@ colormode: int 0
str(bluePlane)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:768, 1:512] 0.345 0.349 0.361 0.357 0.361 ...
##   ..@ colormode: int 0
display(redPlane)

display(greenPlane)

display(bluePlane)

Read and display sample image. The image is stored in an S4 Image class with pixels in .Data. The colormode is Grayscale when the colormode slot is 0.

par(mfrow=c(1,2))
grayScaleImage <- readImage(system.file("images", "shapes.png", package="EBImage"))
str(grayScaleImage)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:512, 1:384] 0 0 0 0 0 0 0 0 0 0 ...
##   ..@ colormode: int 0
colorMode(grayScaleImage)
## [1] 0
display(grayScaleImage)

Convert the Grayscale image above to a Color image with the hue asgreen.

greenHueImage <- channel(grayScaleImage, 'asgreen')
str(greenHueImage)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:512, 1:384, 1:3] 0 0 0 0 0 0 0 0 0 0 ...
##   ..@ colormode: int 2
colorMode(greenHueImage)
## [1] 2
display(greenHueImage)

Note the change from a single plane with grayScaleImage to three planes (with red, green, blue) with a Color image in greenHueImage.

rgbImage

par(mfrow=c(1,2))
x <- readImage(system.file('images', 'nuclei.tif', package='EBImage'))
y <- readImage(system.file('images', 'cells.tif', package='EBImage'))
display(x, title='Cell nuclei', all=TRUE)
display(y, title='Cell bodies', all=TRUE)

cells <- rgbImage(green=1.5*y, blue=x)
display(cells, title='Cells', all=TRUE)

colorMode example

x <- readImage(system.file('images', 'nuclei.tif', package='EBImage'))
x <- x[,,1:3]
display(x, title='Cell nuclei', all=TRUE)

colorMode(x) = Color
display(x, title='Cell nuclei in RGB', all=TRUE)

Color Code Labels

x <- readImage(system.file('images', 'shapes.png', package='EBImage'))
x <- x[110:512,1:130]
y <- bwlabel(x)
z <- colorLabels(y)
display(z, title='Colored segmentation')


packageVersion("EBImage")
## [1] '4.8.2'

efg
2015-01-19 2345