!pip install scikit-image
Collecting scikit-image
Downloading scikit_image-0.19.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (13.3 MB)
|████████████████████████████████| 13.3 MB 29.6 MB/s
Requirement already satisfied: scipy>=1.4.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-image) (1.7.3)
Collecting PyWavelets>=1.1.1
Downloading PyWavelets-1.2.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (6.1 MB)
|████████████████████████████████| 6.1 MB 34.9 MB/s
Collecting networkx>=2.2
Downloading networkx-2.6.3-py3-none-any.whl (1.9 MB)
|████████████████████████████████| 1.9 MB 38.6 MB/s
Requirement already satisfied: pillow!=7.1.0,!=7.1.1,!=8.3.0,>=6.1.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-image) (8.4.0)
Collecting imageio>=2.4.1
Downloading imageio-2.13.3-py3-none-any.whl (3.3 MB)
|████████████████████████████████| 3.3 MB 44.2 MB/s
Requirement already satisfied: numpy>=1.17.0 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from scikit-image) (1.19.5)
Requirement already satisfied: packaging>=20.0 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from scikit-image) (21.3)
Collecting tifffile>=2019.7.26
Downloading tifffile-2021.11.2-py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 49.8 MB/s
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from packaging>=20.0->scikit-image) (3.0.6)
Installing collected packages: PyWavelets, networkx, imageio, tifffile, scikit-image
Successfully installed PyWavelets-1.2.0 imageio-2.13.3 networkx-2.6.3 scikit-image-0.19.1 tifffile-2021.11.2
WARNING: You are using pip version 20.1.1; however, version 21.3.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
# 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()
Image dimensions: (960, 720, 3)
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')
[2.50216331e+02 1.57375848e+02 9.60468182e+01 5.51232757e-07
5.45593533e-07]
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)
S:
[[ 7.84360259 0.41949416 -0.07174078]
[ 0.41949416 9.81784705 -0.51023946]
[-0.07174078 -0.51023946 2.33855035]]
/shared-libs/python3.7/py-core/lib/python3.7/site-packages/ipykernel_launcher.py:42: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
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)
Q:
[[ 0.00782302 -0.98013857 -0.19815949]
[ 0.06730981 0.19823225 -0.97784118]
[ 0.99770145 -0.00568841 0.06752372]]
Delta:
[[2.30356459 0. 0. ]
[0. 7.75834387 0. ]
[0. 0. 9.93809154]]
B:
[[ 0.00782302 -0.98013857 -0.19815949]
[ 0.06730981 0.19823225 -0.97784118]
[ 0.99770145 -0.00568841 0.06752372]]
Rotated B:
[[-1.94216378 0.56817004 1.93614607]
[-0.01769251 3.05462436 -0.69762909]
[ 1.10791657 0.07956679 1.0509671 ]]
M:
[[ 9.99980974e-01 4.31953252e-05 -3.36663601e-05]
[ 4.53751894e-05 1.00000401e+00 4.14497152e-05]
[ 9.84790996e-01 -1.50909285e-02 -1.73015766e-01]
[ 4.65460970e-05 9.96199215e-01 -8.71117253e-02]
[ 9.39677240e-01 -5.93488190e-02 -3.36847654e-01]
[ 4.73627835e-05 9.84812739e-01 -1.73601927e-01]
[ 8.66010500e-01 -1.29369761e-01 -4.82982330e-01]
[ 4.78189555e-05 9.65931243e-01 -2.58770914e-01]
[ 7.66029204e-01 -2.19810510e-01 -6.04038787e-01]
[ 4.79113723e-05 9.39698427e-01 -3.41970498e-01]
[ 6.42771393e-01 -3.23713775e-01 -6.94285392e-01]
[ 4.76390443e-05 9.06313939e-01 -4.22567480e-01]
[ 4.99982401e-01 -4.32988381e-01 -7.50011328e-01]
[ 4.70042148e-05 8.66031854e-01 -4.99948468e-01]
[ 3.42001046e-01 -5.38968356e-01 -7.69760915e-01]
[ 4.60116306e-05 8.19158744e-01 -5.73524546e-01]
[ 1.73627780e-01 -6.33012774e-01 -7.54415000e-01]
[ 4.46687836e-05 7.66051342e-01 -6.42735756e-01]
[-2.11841256e-05 -7.07105413e-01 -7.07113978e-01]
[ 4.29860846e-05 7.07113826e-01 -7.07055359e-01]]
O:
[[-0.66104452 -0.65517193 -0.65651351 ... -0.1997787 -0.20576411
-0.21212354]
[-0.08238522 -0.08231305 -0.09229696 ... -0.3023673 -0.31770801
-0.33364766]
[ 0.36089959 0.37446016 0.37462012 ... -0.60358972 -0.60267387
-0.60255805]]
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!")
It's a rabbit!