Guest User

Untitled

a guest
Apr 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. % copyright J K blah blah blah
  2. % display original picture and PCA approximated pictures using an arbitrary basis
  3. %
  4. % assume featureNormalize() function has been modified to accept optional mu,sigma arguments
  5. % see ex4->displaydata.m->example_width variable how to do this.
  6. %
  7. % start with a fresh octave. run ex7_pca, press <ctrl> - c after done training but before it goes back to k-means stuff
  8. % you want the variables to be set up properly
  9. %
  10. % function accepts identical picture dimension as U was created by. 32x32
  11. % typical calls to the function:
  12. % jmkPCA_Picture(reshape(double(imread('name.png'))/255,1,1024),mu,sigma,U,1:30:300)
  13. % jmkPCA_Picture(X(<pick any row for a face>,:),mu,sigma,U,1:30:300)
  14.  
  15.  
  16. % K is a vector for the # of features you want for that picture, for example K = 1:20:200
  17. %
  18. % here is a function which can give you nice results warping to one side or other
  19. % paste the following two lines at the prompt
  20. % maxfeatures = 1024;
  21. % abc = @(x,a) uint32(1+(maxfeatures-1).*(1-nthroot(1-((0:(x-1))/(x-1)).^a,a)))
  22. %
  23. % K = abc(8,1) % this is 8 reconstructions linear from 1:maxfeatures
  24. % K = abc(8,2) % this is a perfect circle
  25. % K = abc(8,3.5) % you can use real #s for warp factor
  26. % K = abc(8,0.5) % as well as values < 1 (not useful for this application)
  27. % typical call with abc function, (typical range for a is is in 1 to 3.5 or so)
  28. % jmkPCA_Picture(X(<pick any row for a face>,:),mu,sigma,U,abc(8,3.5))
  29.  
  30. %
  31. % X_norm has N original features(pixels of original image)
  32. % Z is a K feature vector(weights of face like feature pictures) 1 <= K <= N
  33. %
  34.  
  35. function [w h] = jmkPCA_Picture(A,mu,sigma,U,K)
  36.  
  37. [w h] = size(A);
  38.  
  39. % if a one color channel image, of proper dimensions of U vectors.
  40. if (w == 1 && (w*h) == size(U,1))
  41. X_norm = featureNormalize(A,mu,sigma);
  42.  
  43. subplot(1, size(K,2)+1, 1);
  44. imagesc(reshape(A,32,32,1));
  45. title('Original');
  46.  
  47. count = 1;
  48.  
  49. for k = K
  50. Z = projectData(X_norm,U,k);
  51. X_rec = recoverData(Z,U,k);
  52.  
  53. % unnormalize the recovered features
  54. X_rec = featureNormalize(X_rec,-mu./sigma,1./sigma);
  55.  
  56. subplot(1, size(K,2)+1, count+1);
  57. imagesc(reshape(X_rec,32,32,1));
  58. title(sprintf('%d',k));
  59.  
  60. count = count + 1;
  61. end
  62.  
  63.  
  64. fprintf("Taaadaaa!\n");
  65. end
  66.  
  67. end
Add Comment
Please, Sign In to add comment