Skip to content

Instantly share code, notes, and snippets.

@rajatkhanduja
Created October 21, 2012 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajatkhanduja/3926106 to your computer and use it in GitHub Desktop.
Save rajatkhanduja/3926106 to your computer and use it in GitHub Desktop.
Reading directory of files into a matrix of row vectors of pixels. Here, the code needs to be executed when orl_faces is a sub-directory of "current working directory".
library (pixmap)
# List the train files
trainFiles = dir("orl_faces", pattern="*[1-9].pgm", full.names=T, recursive=T)
# List the test files
testFiles = dir("orl_faces", pattern="*10.pgm", full.names=T, recursive=T)
# Separate the training set and test set corresponding to each folder
trainSet = numeric(9)
testSet = 1
x <- 1:40
for (i in seq(along = x))
{
searchStr = paste('/s', i, '/', sep='')
trainSet = rbind(trainSet, trainFiles[grep(searchStr, trainFiles)])
testSet = rbind(testSet, testFiles[grep(searchStr, testFiles)])
}
trainSet = trainSet[2:nrow(trainSet),]
testSet = testSet[2:nrow(testSet),]
# Create the training and test matrix of row vectors of pixels
trainMatrix = numeric(92 * 112)
testMatrix = numeric(92 * 112)
y <- 1:9
for (i in seq(along = x))
{
for (j in seq(along = y))
{
t <- read.pnm(trainSet[i,j])
m <- getChannels(t)
trainMatrix = rbind(trainMatrix, as.vector(m))
}
t <- read.pnm(testSet[i])
m <- getChannels(t)
testMatrix = rbind(testMatrix, as.vector(m))
}
trainMatrix = trainMatrix[2:nrow(trainMatrix),]
testMatrix = testMatrix[2:nrow(testMatrix),]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment