DATA605_HW4_Linear Transformation and Representation

library(OpenImageR)
library(jpeg)
library(EBImage)
library(ReadImages)
library(pracma)

Load the Shoes and Resize to 120x250.

files <- list.files(pattern = "\\jpg$")

height=120; 
width=250;
fhi = matrix(1, nrow = 3, ncol = 3)
fhi[2, 2] = -8
plot_jpeg = function(path, add=FALSE)
{ jpg = readJPEG(path, native=T) # read the file
  res = dim(jpg)[2:1] # get the resolution, [x, y]
  if (!add) # initialize an empty plot area if add==FALSE
    plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
  rasterImage(jpg,1,1,res[1],res[2])
}
im=array(rep(0,length(files)*height*width*3), dim=c(length(files), height, width,3))

for (i in 1:length(files)){

  temp= EBImage::resize(readJPEG(files[i]),120, 250)
  im[i,,,]=array(temp,dim=c(1, 120, 250,3))
}
flat=matrix(0, length(files), prod(dim(im)))
for (i in 1:length(files)){
  r=as.vector(im[i,,,1]); g=as.vector(im[i,,,2]);b=as.vector(im[i,,,3])
  flat[i,] <- t(c(r, g, b))
}
shoes=as.data.frame(t(flat))
par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))
for (i in 1:length(files)){ 
plot_jpeg(writeJPEG(im[i,,,]))
}

Eigenimagery for 80 percent of the variability

scaled=scale(shoes, center = TRUE, scale = TRUE)
Sigma_=cor(scaled)
myeigen=eigen(Sigma_)
cumsum(myeigen$values) / sum(eigen(Sigma_)$values)
##  [1] 0.6916309 0.7911549 0.8442585 0.8714110 0.8903922 0.9070168 0.9213980
##  [8] 0.9335995 0.9433646 0.9523060 0.9606716 0.9685299 0.9760625 0.9826631
## [15] 0.9891121 0.9950668 1.0000000

We see that using the 17 images improved the variability accounted for. These images seem a more reflective of the actual differences.

scaling=diag(myeigen$values[1:5]^(-1/2)) / (sqrt(nrow(scaled)-1))
eigenshoes=scaled%*%myeigen$vectors[,1:5]%*%scaling

newdata=im
dim(newdata)=c(length(files),height*width*3)
mypca=princomp(t(as.matrix(newdata)), scores=TRUE, cor=TRUE)


pcaScores=t(mypca$scores)
dim(pcaScores)=c(length(files),height,width,3)
par(mfrow=c(5,5))
par(mai=c(.001,.001,.001,.001))
for (i in 1:length(files)){
plot_jpeg(writeJPEG(pcaScores[i,,,], bg="white"))  
}

Transformation

n=length(files)

# shoes dim
height=1200
width=2500
scale=20

# Pokemon dim
#height=224
#width=224
#scale=2

plot_jpeg = function(path, add=FALSE)
{ jpg = readJPEG(path, native=T) # read the file
  res = dim(jpg)[2:1] # get the resolution, [x, y]
  if (!add) # initialize an empty plot area if add==FALSE
    plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
  rasterImage(jpg,1,1,res[1],res[2])
}

Resize each image and populate vector im with all images dim(im)=(17,60,125,3) where 17 is n the number of images and (60,125,3) are each image dimensions

im=array(rep(0,length(files)*height/scale*width/scale*3), dim=c(length(files), width/scale, height/scale, 3))

for (i in 1:n){
  im[i,,,]=resizeImage(readImage(paste0(files[i])),width=width/scale, height=height/scale)
  }

Transform and Plot first 9 Image.

par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))

for (i in 1:9){
  plot_jpeg(writeJPEG(im[i,,,]))
}