#!/usr/bin/perl use strict; # I don't know enough matlab to do things like iterate over directories # so this perl code does that for me and produces some static matlab code. my %FLAGS; $FLAGS{$_}=1 foreach (@ARGV); print "A=[",join(' ',map { sprintf("reshape(im2double(imread('%s')),[],1)",$_) } ),"];\n"; # Substract off mean face ... heh, like my advisor's face when he realizes # I spent the weekend doing eigenfaces and studying linear algebra. if ($FLAGS{'--subtract-mean'}) { print <<_end_; m=mean(A,2); for i=1:size(A,2) A(:,i) = m-A(:,i); end; _end_ } # Economy size svd print <<_end_; [U,S,V] = svd(A,0); _end_ # Now, normalize U's between 0 & 1 print <<_end_; for i=1:size(U,2) j = U(:,i) - min(U(:,i)); U(:,i) = j / max(j); end; _end_ # Generate eigenfaces mkdir("faces") unless -d "faces"; print <<_end_; sigmas = diag(S); for i=1:10 r = randn(1,4); ef = sigmas(1)*r(1)*U(:,1) + sigmas(2)*r(2)*U(:,2) + sigmas(3)*r(3)*U(:,3) + sigmas(4)*r(4)*U(:,4); ef = ef - min(ef); ef = ef / max(ef); imwrite(reshape(ef,300,250),sprintf('faces/face%d.png',i),'png'); end; _end_ # Export the columns of U as images, so I can look at them. if ($FLAGS{'--dump-U'}) { mkdir("U") unless -d "U"; print <<_end_; for i=1:size(U,2) imwrite(reshape(U(:,i),300,250),sprintf('U/U%d.png',i),'png'); end; _end_ }