Library

library(here)
## here() starts at C:/Users/Santi/OneDrive/Documents/GitHub/DATA605/Assignments/HW4
library(jpeg)
library(EBImage)
library(OpenImageR)
## 
## Attaching package: 'OpenImageR'
## The following objects are masked from 'package:EBImage':
## 
##     readImage, writeImage

Load images

here command returns the current directory and appends “jpg” for the location of the images. We then pass that directory to list.files looking for jpgs in order to get a list of all the images.

direc = here("jpg")

images = list.files(direc, pattern="jpg$")

images
##  [1] "RC_2500x1200_2014_us_53446.jpg" "RC_2500x1200_2014_us_53455.jpg"
##  [3] "RC_2500x1200_2014_us_53469.jpg" "RC_2500x1200_2014_us_53626.jpg"
##  [5] "RC_2500x1200_2014_us_53632.jpg" "RC_2500x1200_2014_us_53649.jpg"
##  [7] "RC_2500x1200_2014_us_53655.jpg" "RC_2500x1200_2014_us_53663.jpg"
##  [9] "RC_2500x1200_2014_us_53697.jpg" "RC_2500x1200_2014_us_54018.jpg"
## [11] "RC_2500x1200_2014_us_54067.jpg" "RC_2500x1200_2014_us_54106.jpg"
## [13] "RC_2500x1200_2014_us_54130.jpg" "RC_2500x1200_2014_us_54148.jpg"
## [15] "RC_2500x1200_2014_us_54157.jpg" "RC_2500x1200_2014_us_54165.jpg"
## [17] "RC_2500x1200_2014_us_54172.jpg"

Create a JPG plotting function

plot_jpeg = function(path, add=FALSE)
{
    jpg = readJPEG(path, native=TRUE) # reads in jpg from the provided path
    res = dim(jpg)[2:1] #returns the resolution of the jpg
    if (!add) {
        plot(1,1, xlim=c(1,res[1]), ylim=c(1,res[2]), asp=1, type='n', xaxs='i', yaxs = 'i', yaxt='n', xlab='',ylab='', bty='n')
    }
    rasterImage(jpg,1,1,res[1],res[2])
}

Load Image Data

Transform the image data into an array

height=1200; width=2500;scale=20

im = array(rep(0,length(images)*height/scale*width/scale*3), dim=c(length(images), height/scale, width/scale,3))  # creates an array the length of the images and scales it so we can start processing the image values

i=1
for (image in images) #read and resize each image and append into the array
{
  temp= resize(readJPEG(here("jpg",image)),height/scale, width/scale)
  im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))
  i = i + 1
}

Vectorize all the images in order to create a matrix of all the images and then a dataframe with all that information.

flat=matrix(0, 17, prod(dim(im))) # flatten the array into a matrix and multiply by the dimensions of the IM array 
i=1
for (image in images) {
  newim <- readJPEG(here("jpg",image))
  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)) #read in the values of the image into our new matrix
  i = i + 1
}
shoes=as.data.frame(t(flat))  # convert the matrix into a dataframe for each shoe with the corresponding vector values
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,,,]))
}

Get the required attributes of the matrices in order to find the Eigenvalues of all the shoes combined

scaled=scale(shoes, center = TRUE, scale = TRUE)
mean.shoe=attr(scaled, "scaled:center") # mean shoe attributes
std.shoe=attr(scaled, "scaled:scale")  # standard deviation of shoe attributes
Sigma_=cor(scaled) #take the correlation of the scaled attributes
myeigen=eigen(Sigma_) #take the eigenvalues of the correlation of the scaled attributes
cumsum(myeigen$values) / sum(myeigen$values) #sum up all the eigenvalues
##  [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

using a diag matrix we can find a way to scale all the images using our eigenvalues for transformation

scaling=diag(myeigen$values[1:2]^(-1/2)) / (sqrt(nrow(scaled)-1))  

matrix multiplication of the scaling factor and the eigenvalue vectors creates an “Eigenshoe” of an “average of all the shoes”

eigenshoes=scaled%*%myeigen$vectors[,1:2]%*%scaling  
imageShow(array(eigenshoes[,2], c(60,125,3)))