With the attached data file, build and visualize eigenimagery that accounts for 80% of the variability. Provide full R code and discussion.

library(doParallel)
## Loading required package: foreach
## Loading required package: iterators
## Loading required package: parallel
library(foreach)
library(iterators)
library(parallel)
library(jpeg)
library(OpenImageR)
library(Matrix)

I have 17 files in the zip so we will process the files by setting the num=17

num=17
files=list.files("C:/Users/Ivan/OneDrive/Desktop/jpg/",pattern="\\.jpg")[1:num]

View shoes 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 the data into the array with required dimensions.Resize function scales the images to the specified dimensions.

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=resizeImage(readJPEG(paste0("C:/Users/Ivan/OneDrive/Desktop/jpg/",files[i])),height/scale, width/scale)
  im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))}
dim(im)
## [1]  17  60 125   3

Here we vecorize the array with dimensions as -> number of files(ht/sclwt/scl*3)

flat=matrix(0,17,prod(dim(im)))
for (i in 1:17){
  newim <-readJPEG(paste0("C:/Users/Ivan/OneDrive/Desktop/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))

Visualize the actual plots

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  

Calculate Covariance

Sigma_=cor(scaled)

Get the Eigencomponents

myeigen=eigen(Sigma_)
cumsum(myeigen$vslues) / sum(myeigen$values)
## numeric(0)

From the above we see the 80% variability can be found at position 2, accordingly we find the eigenshoes at [,2]

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