Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % copyright J K blah blah blah
- % display original picture and PCA approximated pictures using an arbitrary basis
- %
- % assume featureNormalize() function has been modified to accept optional mu,sigma arguments
- % see ex4->displaydata.m->example_width variable how to do this.
- %
- % start with a fresh octave. run ex7_pca, press <ctrl> - c after done training but before it goes back to k-means stuff
- % you want the variables to be set up properly
- %
- % function accepts identical picture dimension as U was created by. 32x32
- % typical calls to the function:
- % jmkPCA_Picture(reshape(double(imread('name.png'))/255,1,1024),mu,sigma,U,1:30:300)
- % jmkPCA_Picture(X(<pick any row for a face>,:),mu,sigma,U,1:30:300)
- % K is a vector for the # of features you want for that picture, for example K = 1:20:200
- %
- % here is a function which can give you nice results warping to one side or other
- % paste the following two lines at the prompt
- % maxfeatures = 1024;
- % abc = @(x,a) uint32(1+(maxfeatures-1).*(1-nthroot(1-((0:(x-1))/(x-1)).^a,a)))
- %
- % K = abc(8,1) % this is 8 reconstructions linear from 1:maxfeatures
- % K = abc(8,2) % this is a perfect circle
- % K = abc(8,3.5) % you can use real #s for warp factor
- % K = abc(8,0.5) % as well as values < 1 (not useful for this application)
- % typical call with abc function, (typical range for a is is in 1 to 3.5 or so)
- % jmkPCA_Picture(X(<pick any row for a face>,:),mu,sigma,U,abc(8,3.5))
- %
- % X_norm has N original features(pixels of original image)
- % Z is a K feature vector(weights of face like feature pictures) 1 <= K <= N
- %
- function [w h] = jmkPCA_Picture(A,mu,sigma,U,K)
- [w h] = size(A);
- % if a one color channel image, of proper dimensions of U vectors.
- if (w == 1 && (w*h) == size(U,1))
- X_norm = featureNormalize(A,mu,sigma);
- subplot(1, size(K,2)+1, 1);
- imagesc(reshape(A,32,32,1));
- title('Original');
- count = 1;
- for k = K
- Z = projectData(X_norm,U,k);
- X_rec = recoverData(Z,U,k);
- % unnormalize the recovered features
- X_rec = featureNormalize(X_rec,-mu./sigma,1./sigma);
- subplot(1, size(K,2)+1, count+1);
- imagesc(reshape(X_rec,32,32,1));
- title(sprintf('%d',k));
- count = count + 1;
- end
- fprintf("Taaadaaa!\n");
- end
- end
Add Comment
Please, Sign In to add comment