Mahbubul Majumder, PhD
Nov 18, 2014
We used data to create image.
Some times image itself could be data
Every image can be expressed as a two dimensional layout of colors
Vector image based on specific shapes like circle, rectangle, line and texts
Raster image based on pixels
Array of colors
Vector images are not always preferred
library(maps)
par(mar=rep(0, 4))
map(region="Bangladesh", col="black", fill=TRUE)
library(png)
bdFlag <- readPNG("bangladesh-flag.png")
flagRaster <- as.raster(bdFlag)
library(grid)
grid.raster(flagRaster)
bdFlag <- readPNG("bangladesh-flag.png")
bdMap <- readPNG("bangladesh-map.png")
flagRaster <- as.raster(bdFlag)
mapRaster <- as.raster(bdMap)
grid.raster(flagRaster)
grid.raster(mapRaster)
dm <- dim(mapRaster)
flagRaster <- flagRaster[1:dm[1], 134:(dm[2] + 133)]
dim(flagRaster)
[1] 600 686
dim(mapRaster)
[1] 600 686
flagRaster[1:3,1:5]
[,1] [,2] [,3] [,4] [,5]
[1,] "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF"
[2,] "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF"
[3,] "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF" "#036A4EFF"
indx <- mapRaster =="#00000000"
flagRaster[indx] <- "transparent"
grid.raster(flagRaster)
A full body MRI scan taken from Beautiful Visualization edited by Julie at el.
bulletHole <- readPNG("bullet-hole.png")
bulletHole <- as.raster(bulletHole)
grid.raster(bulletHole)
Picture source:
https://www.demongraphics.co.uk/index.php?main_page=product_info&cPath=68&products_id=802
holes <- bulletHole[270:470, 250:890]
grid.raster(holes)
indx <- as.matrix(holes) > "#857C7DFF"
bholes <- holes
bholes[indx] <- "transparent"
grid.raster(bholes)
dm <- dim(holes)
cl <- matrix(1,ncol=dm[2], nrow=dm[1])
cl[indx] <- 0
x <- dm[1]:1
y <- 1:dm[2]
dat <- data.frame(expand.grid(x,y), z = as.vector(cl))
names(dat) <- c("x","y","z")
ggplot(dat, aes(y, x, z = z)) + stat_contour()
Often we have low resolution image to investigate
This bullet case study provides critical information that can be computed
d <- dist(cl)
hc <- hclust(d)
groups <- cutree(hc, k = 14)
plot(hc)
rect.hclust(hc, border="red",k=14)
## source("http://bioconductor.org/biocLite.R")
## biocLite("EBImage")
library(EBImage)
f = system.file("images", "lena.png", package="EBImage")
lena = readImage(f)
grid.raster(lena)
print(lena)
Image
colorMode : Grayscale
storage.mode : double
dim : 512 512
frames.total : 1
frames.render: 1
imageData(object)[1:5,1:6]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.5372549 0.5372549 0.5372549 0.5372549 0.5372549 0.5490196
[2,] 0.5372549 0.5372549 0.5372549 0.5372549 0.5372549 0.5490196
[3,] 0.5372549 0.5372549 0.5372549 0.5372549 0.5372549 0.5137255
[4,] 0.5333333 0.5333333 0.5333333 0.5333333 0.5333333 0.5098039
[5,] 0.5411765 0.5411765 0.5411765 0.5411765 0.5411765 0.5333333
lena1 <- lena + 0.5
grid.raster(lena1)
Increase in color means more light or white in color
lena2 <- lena * 3
grid.raster(lena2)
Multiplication gets the difference multiplied which is causing the increased contrasts
rotate()
, flip()
, translate()
and filter2()
fhi = matrix(1, nc=3, nr=3)
fhi[2,2] = -8
lenafhi = filter2(lena, fhi)
grid.raster(lenafhi)
Raster Images in R Graphics
http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf
Introduction to EBImage, an image processing and analysis toolkit for R
http://www.bioconductor.org/packages/release/bioc/vignettes/EBImage/inst/doc/EBImage-introduction.pdf