Eigenshoes
Load Libraries
library(jpeg)
library(imager)
library(knitr)
library(dplyr)
files = list.files(path = '/Users/kristinlussi/Documents/MSDS/DATA 605/Week 4/jpg', pattern="\\.jpg")
View Images
# plot layout
par(mfrow = c(4, 5), mar = c(0.5, 0.5, 0.5, 0.5))
for (file in files) {
filepath <- file.path("/Users/kristinlussi/Documents/MSDS/DATA 605/Week 4/jpg", file)
img <- readJPEG(filepath)
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", xlim = c(0, 1), ylim = c(0, 1), xaxt = "n", yaxt = "n", bty = "n")
rasterImage(img, 0, 0, 1, 1)
}

Load Images into an Array
# Define image dimensions and scale
height <- 1200
width <- 2500
scale <- 20
# Initialize an array to store resized images
newdata <- array(0, dim = c(length(files), height/scale * width/scale * 3))
# Reshape image data to match expected format for PCA
for (i in 1:length(files)) {
filepath <- file.path("/Users/kristinlussi/Documents/MSDS/DATA 605/Week 4/jpg", files[i])
img <- resize(load.image(filepath), height/scale, width/scale)
newdata[i,] <- as.vector(img)
}
Generate Principal Components
# Perform PCA
mypca <- princomp(t(newdata), scores = TRUE, cor = TRUE)
# Verify that sum of variance is 1
sum(mypca$sdev^2 / sum(mypca$sdev^2))
## [1] 1
# Extract squared eigenvalues
mycomponents <- (mypca$sdev^2) / sum(mypca$sdev^2)
# Sum of squared eigenvalues for the first 4 principal components
sum(mycomponents[1:4]) # the first four components account for 86.5% of the variability
## [1] 0.8652577
# sum of squared eigenvalues for the first 15 principal components
sum(mycomponents[1:15]) # first 15 components account for 98.8% of the variability
## [1] 0.9878529
Show Eigenshoes
# Transpose the scores matrix
mypca2 <- t(mypca$scores)
# Reshape the scores matrix to match the image dimensions
dim(mypca2) <- c(length(files), height/scale, width/scale, 3)
# plot layout
par(mfrow = c(4, 5), mar = c(0.5, 0.5, 0.5, 0.5))
# Loop over the principal components and plot them
for (i in seq_along(files)) {
new_img <- as.cimg(mypca2[i,,,])
plot(0:1, 0:1, type = "n", xlab = "", ylab = "", xlim = c(0, 1), ylim = c(0, 1), xaxt = "n", yaxt = "n", bty = "n")
rasterImage(new_img, 0, 0, 1, 1)
}
