Set up
Packages
library(EBImage)
library(jpeg)
library(OpenImageR)
##
## Attaching package: 'OpenImageR'
## The following objects are masked from 'package:EBImage':
##
## readImage, writeImage
Loading Images
num=17
path="jpg/"
files=list.files(path,pattern="\\.jpg")[1:num]
View Images Function
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])
}
Loading into Array
im=array(rep(0,length(files)*height/scale*width/scale*3), dim=c(length(files), height/scale, width/scale,3))
for (i in 1:num){
temp=resize(readJPEG(paste0(path, files[i])),height/scale, width/scale)
im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))}
Vectorizing Images
flat=matrix(0, num, prod(dim(im)))
for (i in 1:num) {
newim <- readJPEG(paste0(path, 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
par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))
for (i in 1:num){ #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")
Correlation
Sigma_=cor(scaled)
Getting Eigencomponents
myeigen=eigen(Sigma_)
cumsum(myeigen$values) / sum(myeigen$values)
## [1] 0.6928202 0.7940449 0.8451072 0.8723847 0.8913841 0.9076337 0.9216282
## [8] 0.9336889 0.9433871 0.9524454 0.9609037 0.9688907 0.9765235 0.9832209
## [15] 0.9894033 0.9953587 1.0000000
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
Generate New Images
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:num){
plot_jpeg(writeJPEG(mypca2[i,,,], bg="white"))
}

Eigencomponents
a=round(mypca$sdev[1:num]^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
## 0.693 0.794 0.845 0.872 0.891 0.907 0.921 0.933 0.943 0.952
## Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17
## 0.960 0.968 0.976 0.983 0.989 0.995 1.000
New Data Set
x = t(t(eigenshoes)%*%scaled)
x
## [,1] [,2] [,3] [,4] [,5]
## V1 -533.9340 -48.37653 -81.33139 -159.855131 115.423287
## V2 -544.3537 186.36373 -54.64152 -97.133393 -11.472782
## V3 -419.1762 -280.11388 -141.61561 -274.657590 -60.359855
## V4 -507.5895 247.57964 -78.40197 51.879288 -115.029841
## V5 -535.9771 193.86403 -35.12975 6.376494 112.559080
## V6 -445.0730 -282.14147 -243.88390 139.376329 1.822899
## V7 -471.2904 -261.05234 -212.76224 108.978354 11.580912
## V8 -551.3154 112.45511 -157.66893 62.116837 -55.993226
## V9 -476.0269 316.47419 -101.85985 64.190529 -84.288694
## V10 -535.6992 218.56389 15.24170 -33.205219 94.643624
## V11 -531.5352 193.19702 84.00357 -30.854743 79.743084
## V12 -539.4412 -130.33161 97.80501 -53.384118 -91.592576
## V13 -504.0171 -206.41990 107.98057 97.673168 127.591829
## V14 -516.1920 -138.98539 201.63252 61.599444 -93.375041
## V15 -537.4005 -50.20604 187.06385 -1.204713 -116.470294
## V16 -545.7370 -97.20098 140.50141 77.357363 86.198490
## V17 -533.5235 -85.24430 176.37988 -26.613761 -21.622400