In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import diags
from scipy.sparse import vstack
from scipy.sparse.linalg import lsqr
from image_io import *
from get_mask import *
from align_images import *
In [7]:
# Load image
s_toy = image_open('./images/source/', 'lego.jpg', in_color=False)
imh, imw = s_toy.shape
print(imh, imw)
In [8]:
def construct_A2(s):
imh, imw = s.shape
n = imh*imw
A = diags(
diagonals=[1, -1, 1, -1, 1],
offsets=[0, -1, -n, -n-imw, -2*n],
shape=[2*n + 1, n],
format='csr',
dtype=float)
A[n, -1] = 0
A[-1, -imw] = 0
return A
A2 = construct_A2(s_toy)
In [24]:
A2.todense()[:10, :10]
Out[24]:
In [9]:
b2 = A2.dot(s_toy.ravel())
v2 = lsqr(A2, b2)[0]
out2 = v2.reshape((imh, imw))
In [16]:
gx = b2[:imh*imw].reshape((imh, imw))
gy = b2[imh*imw:-1].reshape((imh, imw))
plt.imsave('./extras/lego_x.jpg', gx, cmap='gray', vmin=-0.3, vmax=0.3)
plt.imsave('./extras/lego_y.jpg', gy, cmap='gray', vmin=-0.3, vmax=0.3)
In [ ]:
In [15]:
plt.imshow(out2, cmap='gray', vmin=0, vmax=1)
plt.show()
In [16]:
assert np.allclose(s_toy, out2, atol=1.e-5)
print(s_toy[0, 0])
print(out2[0, 0])
In [2]:
In [5]:
plt.close('all')
instructions = 'Use the mouse to draw a loose polygon around the object.\nDouble-click on the last point to exit.'
s_xs, s_ys = get_outline(s, instructions)
mask = get_mask(s, s_xs, s_ys)
In [29]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [32]:
In [34]:
In [21]:
In [19]:
In [36]:
plt.close('all')
In [ ]: