# -*- coding: utf-8 -*- import numpy as np import math import matplotlib.pyplot as plt import matplotlib.image as mpimg img = mpimg.imread("images/butter.png") img_gray = np.mean(img, -1) #img = mpimg.imread("images/baboon.png") #img_gray = img #np.mean(img, -1) plt.clf() plt.imshow(img_gray, cmap = 'gray') plt.axis('off') plt.show() m, n = img_gray.shape T = 0 if m < n: img_gray = img_gray.T m, n = img_gray.shape T = 1 M = img_gray.mean(axis = 0)*np.ones((1,n)) h = np.ones((m,1)) X = img_gray - h.dot(M) Sigma = X.T.dot(X)/(n-1) s, V = np.linalg.eig(Sigma) variances = np.diag(s*s) position = range(0,10) width = 0.35 plt.figure() plt.bar(position, s[0:10]*s[0:10], width) plt.show() l = 20 D = V[:,0:l] C = D.T.dot(X.T) ratio = m/(2.0*l+1) XX = D.dot(C).T XX = XX + h.dot(M) if T == 1: XX = XX.T plt.figure() plt.imshow(XX, cmap='gray') plt.axis('off') plt.show()