This assignment is done to build and visualize eigenimagery that accounts for 80% of the variability of the given image data set. To do so, following steps are being followed:
library(jpeg)
library(OpenImageR)
files<-list.files("C:\\Users\\Acer\\Documents\\jpg",pattern="\\.jpg",full.names = TRUE)
for (file in files) {
img <- readImage(file)
}
flat <- matrix(0, length(files), prod(dim(img)))
for (i in 1:length(files)) {
im <- readJPEG(files[i])
r <- as.vector(im[,,1])
g <- as.vector(im[,,2])
b <- as.vector(im[,,3])
flat[i,] <- t(c(r, g, b))
}
shoes=as.data.frame(t(flat))
scaled=scale(shoes, center = TRUE, scale = TRUE)
mean.shoe=attr(scaled, "scaled:center") #saving for classification
std.shoe=attr(scaled, "scaled:scale") #saving for classification...later
Finding correlation matrix showing the correlations between pairs of variables. This correlation matrix will be used to get the eigencomponents.
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 > .80))
thres
## [1] 3
scaling<-diag(eigenvalues[1:thres]^(-1/2)) / (sqrt(nrow(scaled)-1))
eigenshoes<-scaled%*%eigenvectors[,1:thres]%*%scaling
imageShow(array(eigenshoes[,1], dim(img)))
imageShow(array(eigenshoes[,2], dim(img)))
mypca <- prcomp(shoes, center = TRUE, scale= TRUE)
pca.var <- mypca$sdev^2
pca.var.per <- round(pca.var/sum(pca.var)*100,1)
barplot(pca.var.per, main = "Scree Plot", xlab = "Principal Component", ylab = "Percent Variation")
data_new<-as.data.frame(t(t(eigenshoes)%*%scaled))
It is found that there are 3 principal components among all the components are accounted for 80% of the variability.
References: (1) https://rpubs.com/dherrero12/543854 (2) https://rpubs.com/R-Minator/eigenshoes