!pip install scikit-image
# start by importing some necessary packages
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
im = imread("hidden_message.png")
im = im.astype(np.float)/255.
print("Image dimensions:",im.shape)
plt.imshow(im)
plt.axis('off')
plt.show()
T = 0.004
m0 = np.zeros((240,180))
m1 = np.zeros((240,180))
m2 = np.zeros((240,180))
for i in range(len(m0)):
for j in range(len(m0[0,:])):
s0 = np.array(im[4*i:4*(i+1), 4*j:4*(j+1),0])
_, s0, _ = np.linalg.svd(s0)
s0 = np.sort(s0)[::-1]
s1 = np.array(im[4*i:4*(i+1), 4*j:4*(j+1),1])
_, s1, _ = np.linalg.svd(s1)
s1 = np.sort(s1)[::-1]
s2 = np.array(im[4*i:4*(i+1), 4*j:4*(j+1),2])
_, s2, _ = np.linalg.svd(s2)
s2 = np.sort(s2)[::-1]
if s0[1] - s0[2] > T:
m0[i][j] = 1
if s1[1] - s1[2] > T:
m1[i][j] = 1
if s2[1] - s2[2] > T:
m2[i][j] = 1
M = (m0 + m1 + m2)
plt.imshow(M, cmap = 'gray')
plt.axis('off')
plt.show()
A = np.loadtxt('recon.txt')
U, s, Vt = np.linalg.svd(A, full_matrices=False)
print(s[0:5])
print('Recon.txt has an effective rank of 3 with values of approximately 0 following the third term')
u_hat = U[:,0:3]
Vt_hat = Vt[0:3, :]
s_hat = np.diag(s[0:3])
U_tot = []
S = []
for i in range(10):
u1 = u_hat[i*2][0] * u_hat[i*2][0]
u2 = u_hat[i*2][0] * u_hat[i*2][1] + u_hat[i*2][1] * u_hat[i*2][0]
u3 = u_hat[i*2][0] * u_hat[i*2][2] + u_hat[i*2][2] * u_hat[i*2][0]
u4 = u_hat[i*2][1] * u_hat[i*2][1]
u5 = u_hat[i*2][1] * u_hat[i*2][2] + u_hat[i*2][2] * u_hat[i*2][1]
u6 = u_hat[i*2][2] * u_hat[i*2][2]
U_tot.append([u1, u2, u3, u4, u5, u6])
S.append(1)
u1 = u_hat[i*2+1][0] * u_hat[i*2+1][0]
u2 = u_hat[i*2+1][0] * u_hat[i*2+1][1] + u_hat[i*2+1][1] * u_hat[i*2+1][0]
u3 = u_hat[i*2+1][0] * u_hat[i*2+1][2] + u_hat[i*2+1][2] * u_hat[i*2+1][0]
u4 = u_hat[i*2+1][1] * u_hat[i*2+1][1]
u5 = u_hat[i*2+1][1] * u_hat[i*2+1][2] + u_hat[i*2+1][2] * u_hat[i*2+1][1]
u6 = u_hat[i*2+1][2] * u_hat[i*2+1][2]
U_tot.append([u1, u2, u3, u4, u5, u6])
S.append(1)
u1 = u_hat[i*2][0] * u_hat[i*2+1][0]
u2 = u_hat[i*2][0] * u_hat[i*2+1][1] + u_hat[i*2][1] * u_hat[i*2+1][0]
u3 = u_hat[i*2][0] * u_hat[i*2+1][2] + u_hat[i*2][2] * u_hat[i*2+1][0]
u4 = u_hat[i*2][1] * u_hat[i*2+1][1]
u5 = u_hat[i*2][1] * u_hat[i*2+1][2] + u_hat[i*2][2] * u_hat[i*2+1][1]
u6 = u_hat[i*2][2] * u_hat[i*2+1][2]
U_tot.append([u1, u2, u3, u4, u5, u6])
S.append(0)
U_tot = np.array(U_tot)
S = np.array(S)
X, resid, rank, singular = np.linalg.lstsq(U_tot, S)
S = np.array([
[X[0], X[1], X[2]],
[X[1], X[3], X[4]],
[X[2], X[4], X[5]]
])
print("S:\n", S)
eigs, Q = np.linalg.eigh(S)
D = np.diag(eigs)
B = np.dot(Q, np.sqrt(D))
R = np.array([
[0.7175, 0.1907, 0.6699],
[0.6799, 0.0173, -0.7331],
[0.1513, -0.9815, 0.1172]
])
Br = np.dot(np.dot(Q, np.sqrt(D)), R)
M = np.dot(u_hat, Br)
O = np.dot(np.linalg.inv(Br), np.dot(s_hat, Vt_hat))
print("Q:\n", Q, "\n")
print("Delta:\n", D, "\n")
print("B:\n", Q, "\n")
print("Rotated B:\n", Br, "\n")
print("M:\n", M, "\n")
print("O:\n", O)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(O[0,:],O[1,:],O[2,:], alpha=0.1, s=0.5) # plot the point (2,3,4) on the figure
plt.show()
print("It's a rabbit!")