Created
September 8, 2013 15:29
-
-
Save lizecillie/6485634 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import division | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from skimage import io, color | |
| from skimage.io import ImageCollection | |
| #Training recognition system | |
| ic = ImageCollection('*.pgm') | |
| for image in enumerate(ic): | |
| image = color.rgb2gray(image) | |
| iterable = (i for i in enumerate(ic)) | |
| ic = np.fromiter(iterable, np.float) | |
| p = ic[0].shape[0] | |
| q = ic[0].shape[1] | |
| tmp = np.array(ic).reshape(-1, len(ic)) | |
| average_face = np.mean(tmp, axis=1).T # Average face | |
| display_average_face = average_face.reshape(p, q) | |
| X = tmp - average_face # X | |
| U, S, V = np.linalg.svd(X) | |
| rank = np.sum(S > 1e-12) # threshold | |
| alpha = rank | |
| U_alpha = U[:, 0:alpha+1] # U_alpha | |
| #for f in enumerate(ic): | |
| # y = np.dot(U_alpha.T, (f - average_face)) | |
| # | |
| f, (ax0) = plt.subplots(1,1) | |
| ax0.plot(S) | |
| ax0.set_title('Singular values of X') | |
| f1, (ax0) = plt.subplots(1,1) | |
| ax0.imshow(display_average_face, cmap=plt.cm.gray) | |
| ax0.set_title('Average face') | |
| f2, (ax0, ax1, ax2, ax3) = plt.subplots(1,4) | |
| f2.tight_layout() | |
| ax0.imshow(U_alpha[:, 0].reshape(p, q), cmap=plt.cm.gray) | |
| ax0.set_title('First eigenface') | |
| ax1.imshow(U_alpha[:, 1].reshape(p, q), cmap=plt.cm.gray) | |
| ax1.set_title('Second eigenface') | |
| ax2.imshow(U_alpha[:, 2].reshape(p, q), cmap=plt.cm.gray) | |
| ax2.set_title('Third eigenface') | |
| ax3.imshow(U_alpha[:, 3].reshape(p, q), cmap=plt.cm.gray) | |
| ax3.set_title('Fourth eigenface') | |
| plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment