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.
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))
Classifications out of the training set
scaled <- scale(shoes, center = TRUE, scale = TRUE)
mean.shoe <- attr(scaled, "scaled:center")
std.shoe <- attr(scaled, "scaled:scale")
Sigma_ <- cor(scaled)
eig <- eigen(Sigma_)
eigenvalues <- eig$values
eigenvectors <- eig$vectors
prop.var <- eigenvalues / sum(eigenvalues)
cum.var <- cumsum(eigenvalues) / sum(eigenvalues)
thres <- min(which(cum.var > .8))
scaling <- diag(eigenvalues[1:thres]^(-1/2)) / (sqrt(nrow(scaled)-1))
eigenshoes <- scaled%*%eigenvectors[,1:thres]%*%scaling
eigenshoe <- array(eigenshoes[,2], dim(shoe))
imageShow(eigenshoe)
mypca=princomp(t(data), scores=TRUE, cor=TRUE)
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)