DATA605_w4_eigenimagery

David Simbandumwe

With the attached data file, build and visualize eigenimagery that accounts for 80% of the variability. Provide full R code and discussion.

#Get the list of all images
file_info = list.files(path = "./Homework/data/w4/jpg", pattern = ".jpg", full.names = T)

#Get the dataframe with info
file_info = image_info(image_read(file_info))

#Attach the file names
file_info$fileName = list.files(path = "./Homework/data/w4/jpg", pattern = ".jpg")

# set dimensions
height <- 1200
width <- 2500
scale <- 10


# create img array
img_array <- array(rep(0, length(file_info) * height / scale * width / scale * 3)
                    , dim = c(nrow(file_info)
                    , height/scale, width/scale
                    ,3))


# read img array from files
for (i in 1:nrow(file_info)) {
    tmp <- readJPEG(paste0("./Homework/data/w4/jpg/", file_info$fileName[i]))
    tmp <- resizeImage(tmp,height/scale, width/scale, method = "nearest", normalize_pixels = FALSE)
    img_array[i,,,] <- array(as.cimg(tmp), dim=c(1, height/scale, width/scale,3))
}


# print resized images
for (i in 1:nrow(img_array)) {
    plot(as.cimg(img_array[i,,,]))
}

# create new data array and set dimesions
data <- img_array
dim(data) <- c(nrow(img_array), height*width*3/scale^2)

# calc princomp
pcomp <- princomp(t(as.matrix(data)), scores=TRUE, cor=TRUE)
pcomp_all <- t(pcomp$scores)
dim(pcomp_all) <- c(nrow(img_array), height/scale, width/scale, 3)

# summary stats
summary(pcomp)
## Importance of components:
##                          Comp.1     Comp.2     Comp.3     Comp.4    Comp.5
## Standard deviation     3.407305 1.29857083 0.94990346 0.68598900 0.5766057
## Proportion of Variance 0.682925 0.09919331 0.05307745 0.02768123 0.0195573
## Cumulative Proportion  0.682925 0.78211830 0.83519575 0.86287698 0.8824343
##                            Comp.6     Comp.7     Comp.8     Comp.9     Comp.10
## Standard deviation     0.54050770 0.50495138 0.46612119 0.41922322 0.406376203
## Proportion of Variance 0.01718521 0.01499858 0.01278053 0.01033812 0.009714213
## Cumulative Proportion  0.89961949 0.91461807 0.92739860 0.93773672 0.947450935
##                           Comp.11     Comp.12     Comp.13    Comp.14
## Standard deviation     0.39166617 0.379325445 0.375101155 0.34962721
## Proportion of Variance 0.00902367 0.008463988 0.008276522 0.00719054
## Cumulative Proportion  0.95647460 0.964938593 0.973215115 0.98040566
##                            Comp.15     Comp.16     Comp.17
## Standard deviation     0.345045419 0.338158184 0.315747626
## Proportion of Variance 0.007003314 0.006726527 0.005864504
## Cumulative Proportion  0.987408969 0.994135496 1.000000000
# print results
for (i in 1:nrow(pcomp_all)){
    plot(as.cimg(pcomp_all[i,,,]))  
}



Please show your work using an R-markdown document. Please name your assignment submission with your first initial and last name.