Skip to content

Commit

Permalink
online PCA now uses .avi files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Berman committed Aug 12, 2014
1 parent 5904915 commit f212549
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 106 deletions.
3 changes: 2 additions & 1 deletion PCA/findImageSubsetStatistics.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [meanRadon,stdRadon] = findImageSubsetStatistics(alignedImageDirectory,numToTest,thetas,scale)
function [meanRadon,stdRadon,vidObjs] = findImageSubsetStatistics(alignedImageDirectory,numToTest,thetas,scale)
%findImageSubsetStatistics finds the Radon-transform space mean and
%standard deviations for all of the files in a directory
%
Expand All @@ -13,6 +13,7 @@
%
% meanRadon -> mean values of pixels in Radon-transform space
% stdRadon -> standard deviations of pixels in Radon-transform space
% vidObjs -> VideoReader objects for each of the aligned avi files
%
% (C) Gordon J. Berman, 2014
% Princeton University
Expand Down
231 changes: 163 additions & 68 deletions PCA/onlineImagePCA_radon.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
function [mu,vecs,vals,vecsS,valsS] = onlineImagePCA_radon(files,batchSize,scale,pixels,thetas)
function [mu,vecs,vals] = onlineImagePCA_radon(files,batchSize,scale,pixels,thetas,numPerFile)
%onlineImagePCA_radon finds postural eigenmodes based upon a set of
%aligned images (called by findPosturalEigenmodes.m).
%
% Input variables:
%
% files -> cell array of aligned .tiff files
% batchSize -> # of files to process at once
% files -> cell array of VideoReader objects
% batchSize -> # of images to process at once
% scale -> image scaling factor
% pixels -> radon-transform space pixels to use (Lx1 or 1xL array)
% thetas -> angles used in Radon transform
% numPerFile -> # of images to use per file
%
%
% Output variables:
Expand All @@ -17,105 +18,199 @@
% vecs -> postural eignmodes (LxL array). Each column (vecs(:,i)) is
% an eigenmode corresponding to the eigenvalue vals(i)
% vals -> eigenvalues of the covariance matrix
% vecsS -> postural eignmodes (LxL array) for the shuffled data.
% Each column (vecs(:,i)) is an eigenmode corresponding
% to the eigenvalue shuffledVals(i). (optional)
% valsS -> eigenvalues of the shuffled covariance matrix (optional).
%
%
% (C) Gordon J. Berman, 2014
% Princeton University


if nargin < 3
if nargin < 3 || isempty(scale);
scale = 10/7;
end

N = length(files);
if N < batchSize
batchSize = N;

if nargin < 6 || isempty(numPerFile)
numPerFile = -1;
end


Nf = length(files);
lengths = zeros(Nf,1);
for i=1:Nf
lengths(i) = files{i}.NumberOfFrames;
end
files = files(randperm(N));
num = ceil(N/batchSize);


L = length(pixels);

testImage = imread(files{1});
testImage = imread(files{1},1);
testImage = testImage(:,:,1);
s = size(testImage);
nX = round(s(1)/scale);
nY = round(s(2)/scale);
s = [nX nY];


fprintf(1,'Processing Initial Batch...\n');
X = zeros(batchSize,L);
parfor i=1:batchSize
a = double(imresize(imread(files{i}),s));
lowVal = min(a(a>0));
highVal = max(a(a>0));
a = (a - lowVal) / (highVal - lowVal);
firstBatch = true;
tempMu = zeros(1,L);
totalImages = 0;
for t=1:Nf

R = radon(a,thetas);
X(i,:) = R(pixels);
end
currentImage = batchSize;

mu = sum(X);
C = cov(X).*batchSize + (mu'*mu)./ batchSize;
if nargout > 3
shuffledC = cov(shuffledMatrix(X)).*batchSize + (mu'*mu)./ batchSize;
end

tempMu = zeros(size(mu));
for i=2:num
fprintf(1,'Processing Batch #%5i of %5i, Image #%6i of %6i\n',i,num,currentImage,N);
fprintf(1,'Processing File #%5i out of %5i\n',t,Nf);

if i == num
maxJ = N - currentImage;
M = lengths(t);
if numPerFile == -1
currentNumPerFile = M;
else
maxJ = batchSize;
currentNumPerFile = numPerFile;
end

tempMu(:) = 0;
parfor j=1:maxJ
a = double(imresize(imread(files{currentImage+j}),s));
lowVal = min(a(a>0));
highVal = max(a(a>0));
a = (a - lowVal) / (highVal - lowVal);

R = radon(a,thetas);
y = R(pixels);
X(j,:) = y';
tempMu = tempMu + y';

if M < currentNumPerFile
currentIdx = 1:M;
else
currentIdx = randperm(M,currentNumPerFile);
end
M = min([lengths(t) currentNumPerFile]);

mu = mu + tempMu;
C = C + cov(X(1:maxJ,:)).*maxJ + (tempMu'*tempMu)./maxJ;
if nargout > 3
shuffledC = shuffledC + cov(shuffledMatrix(X(1:maxJ,:))).*maxJ + (tempMu'*tempMu)./maxJ;

if M < batchSize
currentBatchSize = M;
else
currentBatchSize = batchSize;
end
currentImage = currentImage + maxJ;
num = ceil(M/currentBatchSize);

currentVideoReader = files{t};

currentImage = 0;
X = zeros(currentBatchSize,L);
for j=1:num

fprintf(1,'Batch #%5i out of %5i\n',j,num);

if firstBatch

firstBatch = false;

parfor i=1:currentBatchSize

a = read(currentVideoReader,currentIdx(i));
a = double(imresize(a(:,:,1),s));
lowVal = min(a(a>0));
highVal = max(a(a>0));
a = (a - lowVal) / (highVal - lowVal);

R = radon(a,thetas);
X(i,:) = R(pixels);

end
currentImage = currentBatchSize;

mu = sum(X);
C = cov(X).*currentBatchSize + (mu'*mu)./ currentBatchSize;

else

if j == num
maxJ = M - currentImage;
else
maxJ = currentBatchSize;
end

tempMu(:) = 0;
iterationIdx = currentIdx((1:maxJ) + currentImage);
parfor i=1:maxJ

a = read(currentVideoReader,iterationIdx(i));
a = double(imresize(a(:,:,1),s));

lowVal = min(a(a>0));
highVal = max(a(a>0));
a = (a - lowVal) / (highVal - lowVal);

R = radon(a,thetas);
y = R(pixels);
X(i,:) = y';
tempMu = tempMu + y';

end

mu = mu + tempMu;
C = C + cov(X(1:maxJ,:)).*maxJ + (tempMu'*tempMu)./maxJ;
currentImage = currentImage + maxJ;


end

end

totalImages = totalImages + currentImage;


end

mu = mu ./ N;
C = C ./ N - mu'*mu;
if nargout > 3
shuffledC = shuffledC ./ N - mu'*mu;
end


% fprintf(1,'Processing Initial Batch...\n');
% X = zeros(batchSize,L);
% parfor i=1:batchSize
% a = double(imresize(imread(files{i}),s));
% lowVal = min(a(a>0));
% highVal = max(a(a>0));
% a = (a - lowVal) / (highVal - lowVal);
%
% R = radon(a,thetas);
% X(i,:) = R(pixels);
% end
% currentImage = batchSize;
%
% mu = sum(X);
% C = cov(X).*batchSize + (mu'*mu)./ batchSize;
% if nargout > 3
% shuffledC = cov(shuffledMatrix(X)).*batchSize + (mu'*mu)./ batchSize;
% end
%
% tempMu = zeros(size(mu));
% for i=2:num
% fprintf(1,'Processing Batch #%5i of %5i, Image #%6i of %6i\n',i,num,currentImage,N);
%
% if i == num
% maxJ = N - currentImage;
% else
% maxJ = batchSize;
% end
%
% tempMu(:) = 0;
% parfor j=1:maxJ
% a = double(imresize(imread(files{currentImage+j}),s));
% lowVal = min(a(a>0));
% highVal = max(a(a>0));
% a = (a - lowVal) / (highVal - lowVal);
%
% R = radon(a,thetas);
% y = R(pixels);
% X(j,:) = y';
% tempMu = tempMu + y';
% end
%
% mu = mu + tempMu;
% C = C + cov(X(1:maxJ,:)).*maxJ + (tempMu'*tempMu)./maxJ;
% if nargout > 3
% shuffledC = shuffledC + cov(shuffledMatrix(X(1:maxJ,:))).*maxJ + (tempMu'*tempMu)./maxJ;
% end
% currentImage = currentImage + maxJ;
%
% end

mu = mu ./ totalImages;
C = C ./ totalImages - mu'*mu;

fprintf(1,'Finding Principal Components\n');
[vecs,vals] = eig(C);

vals = flipud(diag(vals));
vecs = fliplr(vecs);

if nargout > 3
fprintf(1,'Finding Shuffled Principal Components\n');
[vecsS,valsS] = eig(shuffledC);

valsS = flipud(diag(valsS));
vecsS = fliplr(vecsS);
end




Expand Down
51 changes: 20 additions & 31 deletions findPosturalEigenmodes.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
function [vecs,vals,meanValue,shuffledVecs,shuffledVals] = findPosturalEigenmodes(filePath,pixels,parameters,firstFrame,lastFrame)
function [vecs,vals,meanValue] = findPosturalEigenmodes(filePath,pixels,parameters)
%findPosturalEigenmodes finds postural eigenmodes based upon a set of
%aligned images within a directory.
%
% Input variables:
%
% filePath -> directory containing aligned .tiff files
% filePath -> cell array of VideoReader objects or a directory
% containing aligned .avi files
% pixels -> radon-transform space pixels to use (Lx1 or 1xL array)
% parameters -> struct containing non-default choices for parameters
% firstFrame -> first image in path to be analyzed
% lastFrame -> last image in path to be analyzed
%
%
% Output variables:
Expand All @@ -17,12 +16,6 @@
% an eigenmode corresponding to the eigenvalue vals(i)
% vals -> eigenvalues of the covariance matrix
% meanValue -> mean value for each of the pixels
% shuffledVecs -> postural eignmodes (LxL array) for the shuffled
% data. Each column (vecs(:,i)) is an eigenmode
% corresponding to the eigenvalue
% shuffledVals(i). (optional)
% shuffledVals -> eigenvalues of the shuffled covariance matrix
% (optional).
%
% (C) Gordon J. Berman, 2014
% Princeton University
Expand All @@ -45,35 +38,31 @@
end


files = findAllImagesInFolders(filePath,'tiff');
N = length(files);

if nargin < 4 || isempty(firstFrame)
firstFrame = 1;
end

if nargin < 5 || isempty(lastFrame)
lastFrame = N;
if iscell(filePath)

vidObjs = filePath;

else

files = findAllImagesInFolders(filePath,'avi');
N = length(files);
vidObjs = cell(N,1);
parfor i=1:N
vidObjs{i} = VideoReader(files{i});
end

end

files = files(firstFrame:lastFrame);



numThetas = parameters.num_Radon_Thetas;
spacing = 180/numThetas;
thetas = linspace(0,180-spacing,numThetas);
scale = parameters.rescaleSize;
batchSize = parameters.pca_batchSize;
numPerFile = parameters.pcaNumPerFile;



if nargout > 3
[meanValue,vecs,vals,shuffledVecs,shuffledVals] = ...
onlineImagePCA_radon(files,batchSize,scale,pixels,thetas);
else
[meanValue,vecs,vals] = ...
onlineImagePCA_radon(files,batchSize,scale,pixels,thetas);
end
[meanValue,vecs,vals] = ...
onlineImagePCA_radon(vidObjs,batchSize,scale,pixels,thetas,numPerFile);



Expand Down
Loading

0 comments on commit f212549

Please sign in to comment.