The exercise was to “With the attached data file, build and visualize eigenimagery that accounts for 80% of the variability. Provide full R code and discussion.”

To start with, I will begin by loading the images, converting them into a suitable numerical format for analysis, performing Principal Component Analysis (PCA) to identify the eigenimages, and then visualizing those eigenimages that together account for at least 80% of the variability in the dataset.

## [1] "C:/jpg/renamed"
##  [1] "shoe1.jpg"  "shoe10.jpg" "shoe11.jpg" "shoe12.jpg" "shoe13.jpg"
##  [6] "shoe14.jpg" "shoe15.jpg" "shoe16.jpg" "shoe17.jpg" "shoe2.jpg" 
## [11] "shoe3.jpg"  "shoe4.jpg"  "shoe5.jpg"  "shoe6.jpg"  "shoe7.jpg" 
## [16] "shoe8.jpg"  "shoe9.jpg"

I will use the jpeg R package to load and handle JPEG files.

# loading and prepping the images
library(jpeg)
library(ggplot2)

# after changing the files names to shoe1.jpg, shoe2.jpg, ..., shoe17.jpg
image_filenames <- paste0("shoe", 1:17, ".jpg")
image_paths <- file.path(main_directory, image_filenames)
image_list <- lapply(image_paths, readJPEG)

# flattening them into vectors
image_matrix <- sapply(image_list, function(image) {
  # color (3 layers for RGB)
  if (length(dim(image)) == 3) {
    # converting to vectors while keeping color information
    c(as.vector(image[,,1]), as.vector(image[,,2]), as.vector(image[,,3]))
  } else {
    rep(as.vector(image), 3)
  }
})

# transposing to have images as rows and pixels as columns
image_matrix <- t(image_matrix)

Moving on now to performing PCA, after a step that is meant to avoid an error (I had to insert this step after many attempts at debugging):

# performing PCA

# first, and to avoid the error that kept appearing ("Error in prcomp.default(image_matrix, center = TRUE, scale. = TRUE) : cannot rescale a constant/zero column to unit variance"), I will identify and remove any constant columns
constant_columns <- apply(image_matrix, 2, var) == 0

image_matrix_filtered <- image_matrix[, !constant_columns]

if (sum(constant_columns) > 0) {
  cat("Removed", sum(constant_columns), "constant columns.\n")
} else {
  cat("No constant columns found. Check data preparation steps.\n")
}
## Removed 5064170 constant columns.
### continuing with the PCA:
pca_result <- prcomp(image_matrix_filtered, center = TRUE, scale. = TRUE)

# the number of principal components to account for 80% of variability
cum_var <- cumsum(pca_result$sdev^2) / sum(pca_result$sdev^2)
num_components <- which(cum_var >= 0.8)[1]

# how many components are needed
cat("Number of principal components accounting for 80% of variability:", num_components, "\n")
## Number of principal components accounting for 80% of variability: 8

PCA is now performed on the image matrix. PCA identifies the eigenimages, which are essentially the principal components, and calculates how much variance each eigenimage accounts for. And from the code above (towards the very end of it), I found the minimum number of principal components required to account for 80% of the total variability to be 8.

 # visualizing the Eigenimages

visualize_eigenimage_color <- function(eigenimage, width, height) {
  # splitting the vector back into three colors
  channel_length <- length(eigenimage) / 3
  red_channel <- matrix(eigenimage[1:channel_length], nrow = height, ncol = width)
  green_channel <- matrix(eigenimage[(channel_length + 1):(2*channel_length)], nrow = height, ncol = width)
  blue_channel <- matrix(eigenimage[(2*channel_length + 1):(3*channel_length)], nrow = height, ncol = width)
  
  # combining into a 3D array
  color_image <- array(0, dim = c(height, width, 3))
  color_image[,,1] <- red_channel
  color_image[,,2] <- green_channel
  color_image[,,3] <- blue_channel
  
  # normalizing the eigenimage for display
  color_image <- color_image - min(color_image)
  color_image <- color_image / max(color_image)
  
  # display
  plot(1, type="n", xlab="", ylab="", xlim=c(1,width), ylim=c(1,height), axes=FALSE)
  rasterImage(color_image, 1, 1, width, height)
}

# Since images have same dimensions, find width and height of the first image
image_dimensions <- dim(image_list[[1]])[1:2]

# plotting first few eigenimages (up to what accounts for 80% of the variability)
par(mfrow = c(2, ceiling(num_components / 2)))
for (i in 1:num_components) {
  visualize_eigenimage_color(pca_result$rotation[,i], image_dimensions[2], image_dimensions[1])
  title(paste("Eigenimage", i))
}

The eigenimages corresponding to the principal components accounting for 80% of the variability are visualized. These images reveal the most significant patterns within the shoe images in terms of variability.Each eigenimage represents a principal component, which is a pattern that the PCA has identified as significant in explaining the variance within the dataset.