Homework4 (EigenShoes)

EigenObjects(EigenShoes) is one of the simplest recognition methods. It is a good way to understand how recognition/dimensionality reduction works. EigenShoes has a parallel to one of the most fundamental ideas in mathematics and signal processing – The Fourier Series.

It is helpful to understand what EigenObjects/EigenShoes (or PCA) does. It is a representation of an object in the form of a linear combination of complex signals, also known as the Fourier Series.

The main idea is to find a set of object images that if can weigh and add together to give back the interested object. This is called EigenObjects, which is nothing but EigenVectors of the training data. The way the basis images are weighed, it could be used as features for recognizing the object image.

Preparing the data

Read limited data for time and memory consideration

library(jpeg)
library(OpenImageR)

setwd("C:\\MSDS_Course\\Spring_2022\\DATA_605\\Week4")
shoe <- readJPEG('RC_2500x1200_2014_us_53446.jpg')
imageShow(shoe)

ncol(shoe)
## [1] 2500
nrow(shoe)
## [1] 1200
num=5
files=list.files("C:\\MSDS_Course\\Spring_2022\\DATA_605\\Week4",pattern="\\.jpg")[1:num] 

data <- matrix(0, length(files), prod(dim(shoe)))

for (i in 1:length(files)) {
  jpg <- readJPEG(files[i])
  r <- as.vector(jpg[,,1])
  g <- as.vector((jpg[,,2]))
  b <- as.vector(jpg[,,3])
  
  data[i,] <- t(c(r, g, b))
}
class(data)
## [1] "matrix" "array"
shoes <- data.frame(t(data))

Compute eigenshoes

Classifications out of the training set

1.Center and scale the data

scaled <- scale(shoes, center = TRUE, scale = TRUE)
mean.shoe <- attr(scaled, "scaled:center")
std.shoe  <- attr(scaled, "scaled:scale")

2. Calculate the covariance matrix

Sigma_ <- cor(scaled)  

3. Calculate the non-null eigenvalues of the covariance matrix

eig          <- eigen(Sigma_)
eigenvalues  <- eig$values
eigenvectors <- eig$vectors

4. Choose the number of principal components and get the Eigencomponents

prop.var <- eigenvalues / sum(eigenvalues)
cum.var  <- cumsum(eigenvalues) / sum(eigenvalues)
thres    <- min(which(cum.var > .8))

5. Compute the eigenvectors

scaling    <- diag(eigenvalues[1:thres]^(-1/2)) / (sqrt(nrow(scaled)-1))
eigenshoes <- scaled%*%eigenvectors[,1:thres]%*%scaling

6. Display the eigenshoe

eigenshoe <- array(eigenshoes[,2], dim(shoe))
imageShow(eigenshoe)

Generate Principal Components

mypca=princomp(t(data), scores=TRUE, cor=TRUE)

Generate EigenShoes

mypca2=t(mypca$scores)
height=1200; width=2500;scale=20
#dim(mypca2)=c(5,height/scale,width/scale,3)
par(mfrow=c(5,5))
par(mai=c(.001,.001,.001,.001))

### 1
eigenshoe1 <- array(mypca2[1,], dim(shoe))
imageShow(eigenshoe1)

### 2
eigenshoe2 <- array(mypca2[2,], dim(shoe))
imageShow(eigenshoe2)

### 3
eigenshoe3 <- array(mypca2[3,], dim(shoe))
imageShow(eigenshoe3)

### 4
eigenshoe4 <- array(mypca2[4,], dim(shoe))
imageShow(eigenshoe4)

### 5
eigenshoe5 <- array(mypca2[5,], dim(shoe))
imageShow(eigenshoe5)