Homework 4

Build and visualize eigenimagery that accounts for 80 percent of the variability.

Use of Graphics

#############Prepare for Image Processing#######################
num=17
files=list.files("C:\\Users\\malia\\Downloads\\jpg (1)\\RC_2500x1200_2014_us_53446.jpg")[1:num] 
################################################################

Load the Data into an Array

###################Set Adj. Parameters##########################
height=1200; width=2500;scale=20
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])
}
################################################################
###################Load#########################
im=array(rep(0,length(files)*height/scale*width/scale*3), dim=c(length(files), height/scale,                                                          width/scale,3))

for (i in 1:17){
  temp=resize(readJPEG("C:\\Users\\malia\\Downloads\\jpg (1)\\RC_2500x1200_2014_us_53446.jpg",native = FALSE),height/scale, width/scale)
  im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))}

Vectorize

#################################################
flat=matrix(0, 17, prod(dim(im))) 
for (i in 1:17) {
  newim <- readJPEG("C:\\Users\\malia\\Downloads\\jpg (1)\\RC_2500x1200_2014_us_53446.jpg", files[i])
  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))
#################################################

Plots

####Old Shoes##################
par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))
for (i in 1:17){  #plot the first images only
plot_jpeg(writeJPEG(im[i,,,]))
}

.
## Eigencomponents from Correlation Structure

#################################################
scaled=scale(shoes, center = TRUE, scale = TRUE)
mean.shoe=attr(scaled, "scaled:center") #saving for classification
std.shoe=attr(scaled, "scaled:scale")  #saving for classification...later
#################################################

Calculated Covariance

Sigma_=cor(scaled)

Eigencomponents

myeigen=eigen(Sigma_)
myeigen
## eigen() decomposition
## $values
##  [1]  1.700000e+01  3.710349e-15  3.364946e-32  1.902197e-32  1.520188e-32
##  [6]  5.259356e-34  9.188347e-49  5.681283e-49  2.177954e-49 -1.690758e-50
## [11] -6.786955e-34 -2.326205e-33 -1.216794e-32 -9.998210e-31 -4.836901e-17
## [16] -6.666299e-17 -4.834274e-16
## 
## $vectors
##             [,1]         [,2]          [,3]          [,4]          [,5]
##  [1,] -0.2425356 -0.440031582  0.000000e+00  0.000000e+00  0.000000e+00
##  [2,] -0.2425356  0.864594662 -7.806256e-17 -5.204170e-17  4.336809e-17
##  [3,] -0.2425356  0.005390092  3.067315e-02  1.141513e-01  4.651438e-02
##  [4,] -0.2425356  0.005390092 -4.973110e-01  2.284952e-01  1.355072e-01
##  [5,] -0.2425356  0.005390092  4.666379e-01 -3.426464e-01 -1.820216e-01
##  [6,] -0.2425356 -0.120963522  3.827920e-01  6.898471e-01 -4.268423e-02
##  [7,] -0.2425356 -0.120963522 -3.250790e-01  1.875327e-02  2.548014e-01
##  [8,] -0.2425356 -0.120963522 -9.342961e-02 -4.533020e-01  1.470720e-01
##  [9,] -0.2425356 -0.120963522  3.571666e-02 -2.552984e-01 -3.591892e-01
## [10,] -0.2425356  0.005390092 -2.203711e-01  9.110322e-03 -3.169090e-01
## [11,] -0.2425356  0.005390092 -2.203711e-01  9.110322e-03 -3.169090e-01
## [12,] -0.2425356  0.005390092 -2.203711e-01  9.110322e-03 -3.169090e-01
## [13,] -0.2425356  0.005390092  2.828220e-02 -1.153032e-02  4.047358e-01
## [14,] -0.2425356  0.005390092  2.828220e-02 -1.153032e-02  4.047358e-01
## [15,] -0.2425356  0.005390092  2.225825e-01 -1.093828e-01  1.718399e-01
## [16,] -0.2425356  0.005390092  2.225825e-01 -1.093828e-01  1.718399e-01
## [17,] -0.2425356  0.005390092  1.593839e-01  2.144952e-01 -2.024243e-01
##                [,6]          [,7]          [,8]          [,9]         [,10]
##  [1,]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
##  [2,]  3.469447e-17 -1.882589e-17 -1.598781e-18  5.540742e-18  2.069104e-17
##  [3,]  2.255113e-02 -4.145467e-17 -2.038411e-17 -2.966851e-17  1.355134e-17
##  [4,]  4.637997e-02 -8.086689e-17 -3.305360e-17 -7.524694e-17  3.532507e-17
##  [5,] -6.893110e-02  8.499317e-17  5.167565e-17  6.817622e-17 -3.601200e-17
##  [6,]  2.999959e-01 -2.495360e-16 -5.385662e-17 -2.124085e-17  2.366357e-17
##  [7,] -3.786436e-01  6.244602e-16  1.089296e-16  2.201634e-16 -1.256965e-16
##  [8,]  1.037143e-01 -1.334666e-15 -2.364269e-16 -2.607764e-16  1.817738e-16
##  [9,] -2.506665e-02  9.319151e-16  2.018918e-16  5.825185e-17 -4.012570e-17
## [10,]  1.631968e-01  3.974693e-01 -7.051081e-01  3.907904e-02 -9.990125e-02
## [11,]  1.631968e-01 -5.954488e-01  1.558773e-01  5.311878e-01  7.516101e-02
## [12,]  1.631968e-01  1.979794e-01  5.492307e-01 -5.702669e-01  2.474024e-02
## [13,]  1.571161e-02  4.530723e-01  2.968906e-01  4.379588e-01 -1.215465e-01
## [14,]  1.571161e-02 -4.530723e-01 -2.968906e-01 -4.379588e-01  1.215465e-01
## [15,]  1.364247e-01 -1.373209e-01  1.706395e-02 -6.120548e-02 -6.907284e-01
## [16,]  1.364247e-01  1.373209e-01 -1.706395e-02  6.120548e-02  6.907284e-01
## [17,] -7.938631e-01 -4.662259e-16 -1.196921e-16 -2.552425e-16  1.989298e-16
##               [,11]         [,12]         [,13]         [,14]       [,15]
##  [1,]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.86460897
##  [2,]  3.469447e-17 -8.673617e-18  3.469447e-17  5.930586e-16  0.37198946
##  [3,] -3.950244e-03  1.935654e-02  2.194803e-02  8.056992e-01 -0.06529162
##  [4,]  2.718089e-02  9.854152e-02  3.674261e-01 -4.485241e-01 -0.06529162
##  [5,] -2.323064e-02 -1.178981e-01 -3.893741e-01 -3.571750e-01 -0.06529162
##  [6,] -1.055225e-01  5.925140e-02 -8.498919e-02 -1.178685e-01 -0.12959766
##  [7,]  2.375172e-02 -5.820157e-01 -3.091595e-01  2.812771e-02 -0.12959766
##  [8,] -5.775421e-01  4.033025e-01  7.376714e-02  4.185671e-02 -0.12959766
##  [9,]  6.593129e-01  1.194619e-01  3.203816e-01  4.788408e-02 -0.12959766
## [10,] -6.776764e-02 -3.746302e-02 -1.611262e-01  2.578374e-02 -0.06529162
## [11,] -6.776764e-02 -3.746302e-02 -1.611262e-01  2.578374e-02 -0.06529162
## [12,] -6.776764e-02 -3.746302e-02 -1.611262e-01  2.578374e-02 -0.06529162
## [13,]  2.770113e-01  2.829644e-01 -2.295687e-01 -2.243520e-02 -0.06529162
## [14,]  2.770113e-01  2.829644e-01 -2.295687e-01 -2.243520e-02 -0.06529162
## [15,] -7.505559e-02 -3.443653e-01  3.755652e-01 -9.041050e-03 -0.06529162
## [16,] -7.505559e-02 -3.443653e-01  3.755652e-01 -9.041050e-03 -0.06529162
## [17,] -2.006085e-01  2.351909e-01  1.913856e-01 -1.439871e-02 -0.06529162
##             [,16]      [,17]
##  [1,]  0.00000000  0.0000000
##  [2,] -0.04665951  0.2304328
##  [3,] -0.44948659 -0.2611122
##  [4,] -0.44948659 -0.2611122
##  [5,] -0.44948659 -0.2611122
##  [6,] -0.07932116  0.3917357
##  [7,] -0.07932116  0.3917357
##  [8,] -0.07932116  0.3917357
##  [9,] -0.07932116  0.3917357
## [10,]  0.21405049 -0.1267549
## [11,]  0.21405049 -0.1267549
## [12,]  0.21405049 -0.1267549
## [13,]  0.21405049 -0.1267549
## [14,]  0.21405049 -0.1267549
## [15,]  0.21405049 -0.1267549
## [16,]  0.21405049 -0.1267549
## [17,]  0.21405049 -0.1267549
cumsum(myeigen$values) / sum(myeigen$values)
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Eigenshoes

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

Generate Principal Components

Transform the images

height=1200
width=2500
scale=20
newdata=im
dim(newdata)=c(length(files),height*width*3/scale^2)
mypca=princomp(t(as.matrix(newdata)), scores=TRUE, cor=TRUE)

Eigenshoes (2)

Generate Eigenshoes.

###################Eigenshoes###################################
mypca2=t(mypca$scores)
dim(mypca2)=c(length(files),height/scale,width/scale,3)
par(mfrow=c(5,5))
par(mai=c(.001,.001,.001,.001))
for (i in 1:17){#plot the first 25 Eigenshoes only
plot_jpeg(writeJPEG(mypca2[i,,,], bg="white")) #complete without reduction
}
################################################################

Variance Capture

a=round(mypca$sdev[1:17]^2/ sum(mypca$sdev^2),3)
cumsum(a)
##  Comp.1  Comp.2  Comp.3  Comp.4  Comp.5  Comp.6  Comp.7  Comp.8  Comp.9 Comp.10 
##       1       1       1       1       1       1       1       1       1       1 
## Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17 
##       1       1       1       1       1       1       1

Eigenimagery that accounts for 80 percent of the variability

From the Variance Capture, we note that 80% of the variance lies between Comp. 2 and Comp. 3.

PVE <- myeigen$values / sum(myeigen$values)
round(PVE, 2)
##  [1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0