Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

solve q 4 for me and the code is: import numpy as np from matplotlib.image import imread import matplotlib.pyplot as plt #load image def load

solve q4 for me and the code is: import numpy as np
from matplotlib.image import imread
import matplotlib.pyplot as plt
#load image
def load_image(filepath='./Background_CAU.jpg'):
image_raw = imread(filepath)
# Displaying an image in grayscale.
image_sum = image_raw.sum(axis=2)
X = image_sum/image_sum.max() #treat X as a D x N matrix (treat each column as a sample and each row as a feature)
X_mean = np.mean(X, axis=1, keepdims=True)
plt.figure(figsize=[12,8])
plt.imshow(X, cmap=plt.cm.gray)
plt.title('Original Image', fontsize=15)
plt.show()
plt.close()
return X, X_mean
# compute covariance matrix
def compute_cov_mat(X, X_mean):
### Fill in ################################
cov_mat =
############################################
return cov_mat
if __name__=="__main__":
X, X_mean = load_image('./Background_CAU.jpg')
cov_mat = compute_cov_mat(X, X_mean)
eigen_values , eigen_vectors = np.linalg.eigh(cov_mat)
### sort from highest to lowest eigenvalues
eigen_vectors = eigen_vectors[:, np.argsort(-eigen_values)]
eigen_values = eigen_values[np.argsort(-eigen_values)]
### Compute the projectoin matrix B_10, B_50, B_100, B_500, B_1000
### Fill in ################################
B_10=
B_50=
B_100=
B_500=
B_1000=
############################################
### Reconstructe image: make X_tilde using B_10, B_50, B_100, B_500, B_1000
### Don't forget PCA is performed on the mean-shifted image. So you have to add the mean after reconstruction!
### Fill in ################################
X_tilde_10=
X_tilde_50=
X_tilde_100=
X_tilde_500=
X_tilde_1000=
############################################
for K in [10,50,100,500,1000]:
plt.figure(figsize=[12,8])
if K ==10:
plt.imshow(X_tilde_10, cmap=plt.cm.gray)
elif K ==50:
plt.imshow(X_tilde_50, cmap=plt.cm.gray)
elif K ==100:
plt.imshow(X_tilde_100, cmap=plt.cm.gray)
elif K ==500:
plt.imshow(X_tilde_500, cmap=plt.cm.gray)
elif K ==1000:
plt.imshow(X_tilde_1000, cmap=plt.cm.gray)
plt.title('Reconstructed Image (K={})'.format(K), fontsize=15)
plt.show()
plt.close()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions