Read Pix Set Up Initialization

library(doParallel)
## Loading required package: foreach
## Loading required package: iterators
## Loading required package: parallel
library(foreach)
library(jpeg)
library(EBImage)
library(kableExtra)
library(OpenImageR)
## 
## Attaching package: 'OpenImageR'
## The following objects are masked from 'package:EBImage':
## 
##     readImage, writeImage

Use of Graphics Here, we add graphics to the data set.

#############Prepare for Image Processing#######################
num=17
files=list.files("C:/Users/Lisa/Documents/CUNY/605/Week4/jpg/",pattern="\\.jpg")[1:num] 
################################################################

View Shoes Function

as per Professor - Dr. Fulton

###################Set Adj. Parameters##########################
height=1200; width=2500;scale=20
plot_jpeg = function(path, add=FALSE)
{ jpg = readJPEG(path, native=T) # read the file
  res = dim(jpg)[2:1] # get the resolution, [x, y]
  if (!add) # initialize an empty plot area if add==FALSE
    plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
  rasterImage(jpg,1,1,res[1],res[2])
}
################################################################

Load the Data into an Array This is loading the data in the array by each file, an image structure into the array

Dimensions will be number of images x ht/scale x wt/scale, channnel.

There are 3 channels for red, green and blue.

Resize from the EBI package will scale the images to the desired dimension.

###################Load#########################
im=array(rep(0,length(files)*height/scale*width/scale*3), dim=c(length(files), height/scale,                                                          width/scale,3))

for (i in 1:17){
  temp=resize(readJPEG(paste0("C:/Users/Lisa/Documents/CUNY/605/Week4/jpg/", files[i])),height/scale, width/scale)
  im[i,,,]=array(temp,dim=c(1, height/scale, width/scale,3))}

dim(im)
## [1]  17  60 125   3
str(im)
##  num [1:17, 1:60, 1:125, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
#################################################

Vectorize

The initializing array built in the previous step now needs to be modified into a 2 dimensional matrix.

The dimensions will be the number of images x (ht/s * wt/s * 3)

Essentially, reshaping the array into a vector. Dimension (number of image x(numberHt/sWt/s*3))

#################################################
flat=matrix(0, 17, prod(dim(im))) 
for (i in 1:17) {
  newim <- readJPEG(paste0("C:/Users/Lisa/Documents/CUNY/605/Week4/jpg/", files[i]))
  r=as.vector(im[i,,,1]); g=as.vector(im[i,,,2]);b=as.vector(im[i,,,3])
  flat[i,] <- t(c(r, g, b))
}
shoes=as.data.frame(t(flat))
dim(flat)
## [1]     17 382500
dim(shoes)
## [1] 382500     17
str(shoes)
## 'data.frame':    382500 obs. of  17 variables:
##  $ V1 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V2 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V3 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V4 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V5 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V6 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V7 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V8 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V9 : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V10: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V11: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V12: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V13: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V14: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V15: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V16: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ V17: num  1 1 1 1 1 1 1 1 1 1 ...
#################################################

Actual Plots

Let’s take a look at the shoes, using the plot_jpeg function to see the images……..

These are the shoe basis

####Old Shoes##################
par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))
for (i in 1:17){  #plot the first images only
plot_jpeg(writeJPEG(im[i,,,]))
}

################################

Get Eigencomponents from Correlation Structure

We are scaling the pixels in the array.

Now we need to scale and center data using the scale function. We are normalizing the data to mean=0 and sd=1.

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
dim(scaled)
## [1] 382500     17
str(scaled)
##  num [1:382500, 1:17] 0.651 0.651 0.651 0.651 0.651 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:17] "V1" "V2" "V3" "V4" ...
##  - attr(*, "scaled:center")= Named num [1:17] 0.867 0.755 0.909 0.779 0.762 ...
##   ..- attr(*, "names")= chr [1:17] "V1" "V2" "V3" "V4" ...
##  - attr(*, "scaled:scale")= Named num [1:17] 0.205 0.343 0.194 0.327 0.328 ...
##   ..- attr(*, "names")= chr [1:17] "V1" "V2" "V3" "V4" ...
#################################################

Calculate Covariance (Correlation)

We are using the Correlation matrix. All the preceeding work was done to get to this matrix.

It is this correlation matrix that will be used to determine the eigencomponents.

#################################################
Sigma_=cor(scaled)
dim(Sigma_)
## [1] 17 17
str(Sigma_)
##  num [1:17, 1:17] 1 0.787 0.678 0.654 0.727 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:17] "V1" "V2" "V3" "V4" ...
##   ..$ : chr [1:17] "V1" "V2" "V3" "V4" ...
Sigma_
##            V1        V2        V3        V4        V5        V6        V7
## V1  1.0000000 0.7872852 0.6776806 0.6544083 0.7265979 0.6526121 0.6833202
## V2  0.7872852 1.0000000 0.5265192 0.8346666 0.8313697 0.4974713 0.5452390
## V3  0.6776806 0.5265192 1.0000000 0.3996754 0.4560180 0.6677417 0.6925272
## V4  0.6544083 0.8346666 0.3996754 1.0000000 0.7982592 0.4516536 0.5003524
## V5  0.7265979 0.8313697 0.4560180 0.7982592 1.0000000 0.5039991 0.5657747
## V6  0.6526121 0.4974713 0.6677417 0.4516536 0.5039991 1.0000000 0.8766550
## V7  0.6833202 0.5452390 0.6925272 0.5003524 0.5657747 0.8766550 1.0000000
## V8  0.7560167 0.8386777 0.5447951 0.8512430 0.8125317 0.6770704 0.6651370
## V9  0.5943099 0.8226958 0.3142268 0.8543069 0.7992001 0.3968194 0.4426930
## V10 0.7250561 0.8539275 0.4332609 0.7899076 0.8746777 0.4563027 0.5134772
## V11 0.6973759 0.8153650 0.4336216 0.7689457 0.8420656 0.4380275 0.4884839
## V12 0.7403653 0.7045207 0.6556382 0.6127174 0.6556111 0.6497151 0.6954732
## V13 0.6922410 0.5943306 0.6064572 0.5314402 0.5975513 0.6720664 0.6979220
## V14 0.6561373 0.6316040 0.5724038 0.5781606 0.6257459 0.5942177 0.6371815
## V15 0.7046240 0.7113570 0.5662061 0.6572436 0.6939204 0.5563216 0.5942730
## V16 0.7289015 0.6932984 0.5814029 0.6411786 0.7189399 0.6294994 0.6653556
## V17 0.7229931 0.6882444 0.5803742 0.6129113 0.6771056 0.5761203 0.6180553
##            V8        V9       V10       V11       V12       V13       V14
## V1  0.7560167 0.5943099 0.7250561 0.6973759 0.7403653 0.6922410 0.6561373
## V2  0.8386777 0.8226958 0.8539275 0.8153650 0.7045207 0.5943306 0.6316040
## V3  0.5447951 0.3142268 0.4332609 0.4336216 0.6556382 0.6064572 0.5724038
## V4  0.8512430 0.8543069 0.7899076 0.7689457 0.6127174 0.5314402 0.5781606
## V5  0.8125317 0.7992001 0.8746777 0.8420656 0.6556111 0.5975513 0.6257459
## V6  0.6770704 0.3968194 0.4563027 0.4380275 0.6497151 0.6720664 0.5942177
## V7  0.6651370 0.4426930 0.5134772 0.4884839 0.6954732 0.6979220 0.6371815
## V8  1.0000000 0.8189832 0.8034116 0.7634081 0.6949891 0.6342264 0.6425449
## V9  0.8189832 1.0000000 0.8050235 0.7639324 0.5417763 0.4402583 0.4948664
## V10 0.8034116 0.8050235 1.0000000 0.8797837 0.6871696 0.5871662 0.6344487
## V11 0.7634081 0.7639324 0.8797837 1.0000000 0.6973725 0.6136412 0.6736291
## V12 0.6949891 0.5417763 0.6871696 0.6973725 1.0000000 0.7515997 0.7983485
## V13 0.6342264 0.4402583 0.5871662 0.6136412 0.7515997 1.0000000 0.7738524
## V14 0.6425449 0.4948664 0.6344487 0.6736291 0.7983485 0.7738524 1.0000000
## V15 0.6920964 0.5965987 0.7056080 0.7348277 0.8138715 0.7377752 0.8451752
## V16 0.7053563 0.5709788 0.7078820 0.7184022 0.7792859 0.8487052 0.8218953
## V17 0.6784023 0.5544874 0.6970469 0.7274268 0.8390444 0.7541332 0.7958402
##           V15       V16       V17
## V1  0.7046240 0.7289015 0.7229931
## V2  0.7113570 0.6932984 0.6882444
## V3  0.5662061 0.5814029 0.5803742
## V4  0.6572436 0.6411786 0.6129113
## V5  0.6939204 0.7189399 0.6771056
## V6  0.5563216 0.6294994 0.5761203
## V7  0.5942730 0.6653556 0.6180553
## V8  0.6920964 0.7053563 0.6784023
## V9  0.5965987 0.5709788 0.5544874
## V10 0.7056080 0.7078820 0.6970469
## V11 0.7348277 0.7184022 0.7274268
## V12 0.8138715 0.7792859 0.8390444
## V13 0.7377752 0.8487052 0.7541332
## V14 0.8451752 0.8218953 0.7958402
## V15 1.0000000 0.8122590 0.8124941
## V16 0.8122590 1.0000000 0.8180335
## V17 0.8124941 0.8180335 1.0000000
#################################################

Get the Eigencomponents

We use the Correlation matrix to find the eigenvectors and eigenvalues.

The matrix will find the mulitpliers (eigenvalues) to the vectors (eigenvectors) for the new linear combination so that the most variability is on the 1st component and descends thereafter.

This is a restructing that is used to reduce the dimensionality.

The eigenvalues are the proportion ofvariability associated with each component.

The cumulative proportion of variability is sought after for variability

#################################################

myeigen=eigen(Sigma_)
myeigen
## eigen() decomposition
## $values
##  [1] 11.77794350  1.72081922  0.86806000  0.46371716  0.32298995  0.27624342
##  [7]  0.23790551  0.20503208  0.16487008  0.15399171  0.14378952  0.13577987
## [13]  0.12975772  0.11385581  0.10510011  0.10124271  0.07890165
## 
## $vectors
##             [,1]        [,2]        [,3]         [,4]         [,5]         [,6]
##  [1,] -0.2515577 -0.05962823 -0.14114588 -0.379563852  0.328385354  0.063548147
##  [2,] -0.2564669  0.22970931 -0.09482715 -0.230635855 -0.032640672  0.118423529
##  [3,] -0.1974907 -0.34526445 -0.24576561 -0.652153561 -0.171726978  0.244730194
##  [4,] -0.2391458  0.30516319 -0.13606203  0.123183424 -0.327265980  0.337265720
##  [5,] -0.2525203  0.23895408 -0.06096563  0.015140501  0.320236534 -0.134085706
##  [6,] -0.2096918 -0.34776362 -0.42324623  0.330938495  0.005186244 -0.204505723
##  [7,] -0.2220438 -0.32176946 -0.36923642  0.258760814  0.032948308 -0.251124796
##  [8,] -0.2597468  0.13861060 -0.27362520  0.147491705 -0.159303688  0.115445941
##  [9,] -0.2242754  0.39008166 -0.17677181  0.152415531 -0.239805790  0.083991828
## [10,] -0.2523894  0.26939879  0.02645108 -0.078843268  0.269266115 -0.257208299
## [11,] -0.2504276  0.23813194  0.14578328 -0.073262241  0.226873290 -0.269224927
## [12,] -0.2541524 -0.16064492  0.16973486 -0.126756529 -0.260585721 -0.363829750
## [13,] -0.2374627 -0.25443028  0.18739396  0.231917509  0.363005499  0.477340192
## [14,] -0.2431988 -0.17131144  0.34992144  0.146263195 -0.265656929  0.024086378
## [15,] -0.2531909 -0.06188326  0.32463836 -0.002860499 -0.331364145 -0.006034388
## [16,] -0.2571186 -0.11980857  0.24383197  0.183679177  0.245239261  0.313599435
## [17,] -0.2513643 -0.10507093  0.30609696 -0.063192352 -0.061516870 -0.259275443
##              [,7]         [,8]        [,9]        [,10]        [,11]
##  [1,]  0.52472151  0.462780425  0.08090052 -0.028686437 -0.082670446
##  [2,]  0.18685685  0.102636186 -0.22284303  0.146778255 -0.003007651
##  [3,] -0.44212122 -0.227921853  0.08469880 -0.056985524  0.032359887
##  [4,]  0.09329363 -0.108658492 -0.02018693  0.043000887 -0.385967012
##  [5,] -0.30873641  0.075058750  0.30887082  0.313271194 -0.077454999
##  [6,]  0.03288846  0.082751705  0.03318951 -0.363929097  0.185379664
##  [7,] -0.10293111  0.025168182 -0.01883316  0.449241184 -0.162176676
##  [8,]  0.11833818 -0.024122940  0.03766341 -0.430322312 -0.007346868
##  [9,] -0.01801285 -0.145350940  0.08507873  0.182867722  0.335094970
## [10,] -0.16721360 -0.008894314 -0.16125030 -0.017921721  0.043924937
## [11,] -0.28837615 -0.096205432 -0.23846881 -0.437088321 -0.015359077
## [12,]  0.25268735 -0.248709314 -0.45258345  0.273338221  0.012571836
## [13,]  0.06307754 -0.247377522 -0.38600966 -0.004186005  0.162213020
## [14,] -0.25497078  0.410430155 -0.07746900 -0.114623024 -0.513072384
## [15,] -0.07654358  0.415272003  0.11933253  0.048656688  0.597911695
## [16,] -0.05781332 -0.076685196  0.29966154  0.139308358  0.021132312
## [17,]  0.33330420 -0.444466219  0.53287649 -0.143201315 -0.119605230
##               [,12]        [,13]        [,14]       [,15]        [,16]
##  [1,] -0.0596509962  0.173867983  0.021256074 -0.33990643  0.018395636
##  [2,]  0.3583438497 -0.043787518  0.005075752  0.68936557 -0.279022018
##  [3,]  0.0232829238 -0.006365664 -0.007579662 -0.05684866  0.046598395
##  [4,] -0.5189672373  0.090700864  0.185159803  0.07881625  0.109633099
##  [5,] -0.2572809776 -0.330742658 -0.439080456 -0.02771865 -0.285787046
##  [6,]  0.0305021439 -0.169122407  0.080639839  0.13350961 -0.192869700
##  [7,] -0.0371957437  0.404440089  0.076926737  0.10798180  0.172641328
##  [8,]  0.0008210003 -0.344221644 -0.157965785 -0.10736758  0.137981374
##  [9,]  0.4421801179  0.290091562 -0.080294979 -0.42914069 -0.091690276
## [10,]  0.1600328218 -0.200827705  0.151176703  0.06475096  0.725134274
## [11,] -0.1827459961  0.420306818  0.203199029 -0.04230540 -0.339738851
## [12,] -0.1165190416 -0.355620806  0.077245611 -0.28273932 -0.181694045
## [13,] -0.0596669046  0.120677217 -0.399244393  0.01585424  0.091552581
## [14,]  0.3522754738  0.005863715 -0.179998868 -0.12378113 -0.007729217
## [15,] -0.3278142100  0.085454624 -0.010929080  0.16580116  0.127718981
## [16,]  0.1412592086 -0.238082715  0.654494270 -0.07441115 -0.149991294
## [17,]  0.0804565747  0.182592123 -0.195394955  0.19831312  0.099937306
##              [,17]
##  [1,]  0.059004080
##  [2,] -0.063957363
##  [3,]  0.042079240
##  [4,]  0.305515615
##  [5,]  0.057864330
##  [6,]  0.487526960
##  [7,] -0.357322012
##  [8,] -0.634621151
##  [9,]  0.159327194
## [10,]  0.207477246
## [11,] -0.144512187
## [12,] -0.008414234
## [13,]  0.060719984
## [14,]  0.093570291
## [15,] -0.075669136
## [16,] -0.126288629
## [17,]  0.048581820
cumsum(myeigen$values) / sum(myeigen$values)
##  [1] 0.6928202 0.7940449 0.8451072 0.8723847 0.8913841 0.9076337 0.9216282
##  [8] 0.9336889 0.9433871 0.9524454 0.9609037 0.9688907 0.9765235 0.9832209
## [15] 0.9894033 0.9953587 1.0000000

Eigenshoes

The new shoes are a reduced number that are built by the new linear combinations. The new shoes are eigenvectors.

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

80% is between 2 and 3 components….. Here is 2

#################################################
scaling=diag(myeigen$values[1:2]^(-1/2)) / (sqrt(nrow(scaled)-1))

scaling
##              [,1]        [,2]
## [1,] 0.0004711401 0.000000000
## [2,] 0.0000000000 0.001232586
eigenshoes=scaled%*%myeigen$vectors[,1:2]%*%scaling
dim(eigenshoes)
## [1] 382500      2
str(eigenshoes)
##  num [1:382500, 1:2] -0.0012 -0.0012 -0.0012 -0.0012 -0.0012 ...
imageShow(array(eigenshoes[,1], c(60,125,3)))

The second

imageShow(array(eigenshoes[,2], c(60,125,3)))

Here is 3

scaling3=diag(myeigen$values[1:3]^(-1/2)) / (sqrt(nrow(scaled)-1))
scaling3
##              [,1]        [,2]        [,3]
## [1,] 0.0004711401 0.000000000 0.000000000
## [2,] 0.0000000000 0.001232586 0.000000000
## [3,] 0.0000000000 0.000000000 0.001735441
eigenshoes3=scaled%*%myeigen$vectors[,1:3]%*%scaling3
dim(eigenshoes3)
## [1] 382500      3
#imageShow(array(eigenshoes3[,1], c(60,125,3)))
#imageShow(array(eigenshoes3[,2], c(60,125,3)))
imageShow(array(eigenshoes3[,3], c(60,125,3)))

#################################################

Generate Principal Components This is for a future assignment, same matrix so include.

Transform the images.

###################Generate Variables###########################
height=1200
width=2500
scale=20
newdata=im
dim(newdata)=c(length(files),height*width*3/scale^2)
mypca=princomp(t(as.matrix(newdata)), scores=TRUE, cor=TRUE)
################################################################

Eigenshoes Generate Eigenshoes. These are all 17 eigen shoes.

###################Eigenshoes###################################
mypca2=t(mypca$scores)
dim(mypca2)=c(length(files),height/scale,width/scale,3)
par(mfrow=c(5,5))
par(mai=c(.001,.001,.001,.001))
for (i in 1:17){#plot the first 17 Eigenshoes only
plot_jpeg(writeJPEG(mypca2[i,,,], bg="white"))  #complete without reduction
}
################################################################

# Variance Capture

#################################################
a=round(mypca$sdev[1:17]^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

New Data Set

#################################################
x = t(t(eigenshoes)%*%scaled)
#################################################

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.