library(doParallel)
library(jpeg)
library(foreach)
library(png)
library(OpenImageR)
library(EBImage)
# Using name of folder as path to images
files=list.files(path="jpg", pattern = "*.jpg")
height=1200
width=2500
scale=20
plot_jpeg <- function(path, add=FALSE)
{
jpg=readJPEG(path, native=T)
res=dim(jpg)[2:1]
if(!add)
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])
}
im=array(rep(0,length(files)*height/scale*width/scale*3),
dim=c(length(files),height/scale, width/scale,3))
# Error Message below
for(i in 1:length(files)){
tmp=paste0('jpg/', files[i])
temp=EBImage::resize(readJPEG(tmp), height/scale, width/scale)
im[i,,,]=array(temp, dim=c(1,height/scale, width/scale, 3))
}
par(mfrow=c(3,3))
par(mai=c(.3,.3,.3,.3))
for (i in 1:17){
plot_jpeg(writeJPEG(im[i,,,]))
}


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)
sum(mypca$sdev^2/sum(mypca$sdev^2))
## [1] 1
mycomponents=mypca$sdev^2/sum(mypca$sdev^2)
sum(mycomponents[1:17])
## [1] 1
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_jpeg(writeJPEG(mypca2[i,,,], quality=1,bg="white"))
}
