Libraries
library(imager)
## Warning: package 'imager' was built under R version 4.3.2
## Loading required package: magrittr
## Warning: package 'magrittr' was built under R version 4.3.2
##
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
##
## add
## The following objects are masked from 'package:stats':
##
## convolve, spectrum
## The following object is masked from 'package:graphics':
##
## frame
## The following object is masked from 'package:base':
##
## save.image
library(jpeg)
library(OpenImageR)
## Warning: package 'OpenImageR' was built under R version 4.3.2
library(EBImage)
##
## Attaching package: 'EBImage'
## The following objects are masked from 'package:OpenImageR':
##
## readImage, writeImage
## The following objects are masked from 'package:imager':
##
## channel, dilate, display, erode, resize, watershed
# This library takes a long time to load, so it's been commented out once I installed it
# https://bioconductor.org/packages/release/bioc/html/EBImage.html
#if (!require("BiocManager", quietly = TRUE))
#install.packages("BiocManager")
#BiocManager::install("EBImage")
File location
#############Prepare for Image Processing#######################
num=20
filepath <- "C:/Users/Ron/OneDrive/Desktop/Personal - Important/Grad School Stuff/CUNY SPS/DATA 605 Spring 2024/jpg"
files <- list.files(filepath, pattern = "\\.jpg")
print(files)
## [1] "RC_2500x1200_2014_us_53446.jpg" "RC_2500x1200_2014_us_53455.jpg"
## [3] "RC_2500x1200_2014_us_53469.jpg" "RC_2500x1200_2014_us_53626.jpg"
## [5] "RC_2500x1200_2014_us_53632.jpg" "RC_2500x1200_2014_us_53649.jpg"
## [7] "RC_2500x1200_2014_us_53655.jpg" "RC_2500x1200_2014_us_53663.jpg"
## [9] "RC_2500x1200_2014_us_53697.jpg" "RC_2500x1200_2014_us_54018.jpg"
## [11] "RC_2500x1200_2014_us_54067.jpg" "RC_2500x1200_2014_us_54106.jpg"
## [13] "RC_2500x1200_2014_us_54130.jpg" "RC_2500x1200_2014_us_54148.jpg"
## [15] "RC_2500x1200_2014_us_54157.jpg" "RC_2500x1200_2014_us_54165.jpg"
## [17] "RC_2500x1200_2014_us_54172.jpg"
################################################################
View Shoes
###################Set Adj. Parameters##########################
height=1200 # dimensions of images
width=2500
scale=20
plot_jpeg = function(path, add=FALSE) #initialize function
{
require('jpeg')
jpg = readJPEG(path, native=T) # read the file
res = dim(jpg)[2:1] # get the resolution, [x is 2, y is 1] (dimensions of jpeg)
if (!add) # initialize an empty plot area if add==FALSE
plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]), #set the X Limits by size
asp=1, #aspect ratio
type='n', #don't plot
xaxs='i',yaxs='i',#prevents expanding axis windows +6% as normal (fixed size)
xaxt='n',yaxt='n',xlab='',ylab='', # no axes or labels
bty='n') # no box around graph
rasterImage(jpg,1,1,res[1],res[2]) #image, xleft,ybottom,xright,ytop. Converts jpeg to Raster image and sets the size. Start at 1,1. res[1] and res[2] are the x and y dimension
}
################################################################
Load the Data into an Array
###################Load#########################
#initialize array with zeros.
im=array(rep(0,length(files)*height/scale*width/scale*3), # 3 for RGB
#set dimension to N, x, y, 3 colors, 4D array)
dim=c(length(files), height/scale, width/scale,3))
for (i in 1:length(files)){
#define file to be read
tmp=paste0("C:/Users/Ron/OneDrive/Desktop/Personal - Important/Grad School StufF/CUNY SPS/DATA 605 Spring 2024/jpg/", files[i])
#read the file
temp=EBImage::resize(readJPEG(tmp),height/scale, width/scale) #temporary image of dimensions (1200/20) x (2500/20)
#assign to the array, with dimensions below
im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))
}
#################################################
Actual Plots
par(mfrow = c(4, 5), mar = c(0.5, 0.5, 0.5, 0.5))
par(mai=c(.3,.3,.3,.3)) #set margins
for (i in 1:length(files)){
plot_jpeg(writeJPEG(im[i,,,]))
}

Generate Principal Components
###################Generate Variables###########################
height=1200
width=2500
scale=20
newdata=im
dim(newdata)=c(length(files),height*width*3/scale^2) #17*(60*125*3)/ (20^2) = 22,500 # Converts images into 2d array
mypca=princomp(t(as.matrix(newdata)), scores=TRUE, cor=TRUE) #Get principal components, as a matrix (17 x 22500)
sum(mypca$sdev^2/sum(mypca$sdev^2)) #verify that sum of variance=1 #correlation matrix
## [1] 1
mycomponents=mypca$sdev^2/sum(mypca$sdev^2)
sum(mycomponents[1:2]) #first 2 components account for 80% of variability (0.7940449)
## [1] 0.7940449
sum(mycomponents[1:6]) #first 6 components account for 90% of variability (0.9076338)
## [1] 0.9076338
# The cum-sum shows how many components account for the variability
a=round(mypca$sdev[1:length(files)]^2/ sum(mypca$sdev^2),3)
cumsum(a)
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9 Comp.10
## 0.693 0.794 0.845 0.872 0.891 0.907 0.921 0.933 0.943 0.952
## Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17
## 0.960 0.968 0.976 0.983 0.989 0.995 1.000
Eigenshoes
###################Eigenshoes###################################
mypca2=t(mypca$scores)
dim(mypca2)=c(length(files),height/scale,width/scale,3)
par(mfrow=c(4,5))
par(mai=c(.001,.001,.001,.001))
for (i in 1:length(files)){ #plot the Eigenshoes
plot_jpeg(writeJPEG(mypca2[i,,,], quality=1,bg="white")) #complete without reduction
}
