R Presentation

Jason Jylee
2014/11/26

Outline

  • EBImage

  • image

  • levelplot (Lattice)

  • PCA for image

  • Google map

EBImage

EBImage

## Load the library by the following command
## source("http://bioconductor.org/biocLite.R")
## biocLite("EBImage")
library(EBImage)
  • an R package which provides general purpose functionality for the reading, writing, processing and analysis of images.

  • offers tools to transform the images, segment cells and extract quantitative cellular descriptors.

  • Author: Andrzej Oles, Gregoire Pau, Mike Smith, Oleg Sklyar, Wolfgang Huber, with contributions from Joseph Barry and Philip A. Marais.

Image Properties

Image <- readImage("C:/Users/asusnb/Desktop/picture.JPG")
print(Image)
Image
  colormode: Color 
  storage.mode: double 
  dim: 960 720 3 
  nb.total.frames: 3 
  nb.render.frames: 1 

imageData(object)[1:5,1:6,1]:
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,] 0.5921569 0.5921569 0.5843137 0.5803922 0.5372549 0.4470588
[2,] 0.5490196 0.5568627 0.5607843 0.5843137 0.5803922 0.5333333
[3,] 0.5921569 0.5882353 0.5803922 0.5921569 0.5921569 0.5647059
[4,] 0.6117647 0.6039216 0.5803922 0.5803922 0.5843137 0.5647059
[5,] 0.5568627 0.5568627 0.5490196 0.5607843 0.5882353 0.5882353

Our Image

#Use display(Image) to show the picture.

Brightness

Lighter_Image <- Image + 0.2
Darker_Image <- Image - 0.2
#display(Lighter_Image); display(Darker_Image)
                 Brighter                     Darker

Contrast

Low_Image <- Image*0.5
High_Image <- Image*2
#display(Low_Image); display(High_Image)
                     Low              High

Gamma

Image2 <- Image^2
Image0_7 <- Image^0.7
#display(Image2); display(Image0_7)
                Gamma2                  Gamma0_7

Cropping

#display(Image[200:850, 200:500,])

Spatial Transformation

Imagetr <- translate(rotate(Image, 45), c(50, 0))
#display(Imagetr)

Image

Image(Default color)

m <- matrix(rnorm(400), ncol=10, byrow=FALSE)
image(1:10, 1:40, t(m)[, nrow(m):1])

plot of chunk unnamed-chunk-9

Image(Rainbow color)

m <- matrix(rnorm(400), ncol=10, byrow=FALSE)
image(1:10, 1:40, t(m)[, nrow(m):1], col=rainbow(10))

plot of chunk unnamed-chunk-10

Rainbow Example

l <- matrix(1:10, ncol=2)
image(t(l)[, 5:1], col=rainbow(10))

plot of chunk unnamed-chunk-11

Heatmap

heatmap(m)

plot of chunk unnamed-chunk-12

Add Pattern

m2 <- matrix(0, nrow=40, ncol=10)
for(i in 1:10){
    m2[, i] <- rnorm(40, i, 1)
}

Image

image(1:10, 1:40, t(m2)[, nrow(m2):1])

plot of chunk unnamed-chunk-14

Heatmap

heatmap(m2)

plot of chunk unnamed-chunk-15

levelplot(lattice)

levelplot from Lattice Package

library(lattice)
levelplot
function (x, data, ...) 
UseMethod("levelplot")
<bytecode: 0x0000000008ad2f10>
<environment: namespace:lattice>

Previous Example(m2)

levelplot(m)

plot of chunk unnamed-chunk-17

Previous Example(m)

levelplot(m2)

plot of chunk unnamed-chunk-18

Principal Component Analysis

Principal Component Analysis

plot of chunk unnamed-chunk-19

Singular Value Decomposition

svd1 <- svd(scale(faceData))
#plot(svd1$d^2/sum(svd1$d^2), pch = 19, 
#     xlab = "Singular vector",
#     ylab = "Variance explained")

Component Selection

approx1 <- svd1$u[, 1] %*% t(svd1$v[, 1]) * svd1$d[1]
approx5 <- svd1$u[, 1:5] %*% diag(svd1$d[1:5]) %*% t(svd1$v[, 1:5])
approx10 <- svd1$u[, 1:10] %*% diag(svd1$d[1:10]) %*% t(svd1$v[, 1:10])

Plot

#par(mfrow = c(1, 4))
#image(approx1[, ncol(approx1):1], main = "(a)")
#image(approx5[, ncol(approx5):1], main = "(b)")
#image(approx10[, ncol(approx10):1], main = "(c)")
#image(faceData[, ncol(faceData):1], main = "(d)")

Map

Map China

library("maps")
library("mapdata")
map('china')

plot of chunk unnamed-chunk-23

Map Japan

map('japan')

plot of chunk unnamed-chunk-24

Map USA

map('usa')
map('rivers', add=TRUE,col="blue")

plot of chunk unnamed-chunk-25

Mail Data

##http://data.gov.tw/node/7964
maildata=read.csv("C:/Users/asusnb/Desktop/maildata.csv")
data=maildata[,-c(2,3,4,5,6,7,8,9,10,11,12)]
#head(maildata)
#head(data)

Mail Data

data[,1][which(data[,1]=="2")]=1
data[,1][which(data[,1]=="3")]=2
data[,1][which(data[,1]=="4")]=2
data[,1][which(data[,1]=="5")]=3
data[,1][which(data[,1]=="6")]=3
data[,1][which(data[,1]=="7")]=4
data[,1][which(data[,1]=="8")]=4

Mail Data

data1=data[which(data[,1]=="1"),]
data2=data[which(data[,1]=="2"),]
data3=data[which(data[,1]=="3"),]
data4=data[which(data[,1]=="4"),]

Mailbox in Taiwan

##draw Taiwan
map("world2Hires",xlim=c(118,124),ylim=c(22,26))
##points data2
points(data2[,2],data2[,3],col="green",pch="20",cex=0.5)
##points data3
points(data3[,2],data3[,3],col="yellow",pch="20",cex=0.5)
##points data4
points(data4[,2],data4[,3],col="blue",pch="20",cex=0.5)
##points data1
points(data1[,2],data1[,3],col="red",pch="20",cex=0.5)

plot of chunk unnamed-chunk-29

get_nearist Function

get_nearist=function(place) { 
    where=NULL
    all=NULL
      for( i in 1:8988){
      where=(place[,1]-data[i,2])^2+(place[,2]-data[i,3])^2 
      all=c(all,where)
      }
    maildata[which(all==min(all)),]
}

Get nearist mailbox of NTU

library(ggmap)
(NTU=geocode('National Taiwan university'))
       lon      lat
1 121.5398 25.01734
#get_nearist(NTU)

WhereRU Function

library(ggplot2)
library(mapproj)
whereRU=function(place) { 
  map<-get_map(location = c(place[,1],place[,2]), zoom = 15, maptype = 'roadmap')
  ggmap(map)+
    geom_point(aes(x=here[,1],y=here[,2]),colour = 'black',size=4)+
    geom_point(data=data1,aes(x=data1[,2],y=data1[,3]),colour = 'red',size=4)+
    geom_point(data=data2,aes(x=data2[,2],y=data2[,3]),colour = 'green',size=4)+
    geom_point(data=data3,aes(x=data3[,2],y=data3[,3]),colour = 'blue',size=4)+
    geom_point(data=data4,aes(x=data4[,2],y=data4[,3]),colour = 'yellow',size=4)
}

NTU in Google Map

here <- NTU
#whereRU(NTU)

Taipei Station in Google Map

TS=geocode(' TAIPEI STATION ')
here <- TS
#whereRU(TS)

NCCU in Google Map

NCCU=geocode('cheng chi university')
here <- NCCU
#whereRU(NCCU)

Pay attention to this!

Bigger whereRU function

##find NCCU something wrong
whereRU=function(place) { 
  map<-get_map(location = c(place[,1],place[,2]), zoom = 17, maptype = 'roadmap')
  ggmap(map)+
    geom_point(aes(x=here[,1],y=here[,2]),colour = 'black',size=4)+
    geom_point(data=data1,aes(x=data1[,2],y=data1[,3]),colour = 'red',size=4)+
    geom_point(data=data2,aes(x=data2[,2],y=data2[,3]),colour = 'green',size=4)+
    geom_point(data=data3,aes(x=data3[,2],y=data3[,3]),colour = 'blue',size=4)+
    geom_point(data=data4,aes(x=data4[,2],y=data4[,3]),colour = 'yellow',size=4)
}

Find NCCU Something Wrong

here <- NCCU
#whereRU(NCCU)

What is wrong?

Reference