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)


(432, 540)

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]:
matrix([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [-1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0., -1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0., -1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0., -1.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0., -1.,  1.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0., -1.,  1.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0., -1.,  1.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0., -1.,  1.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., -1.,  1.]])

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)


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-16-bf9cef22cfe6> in <module>()
      6 plt.imshow(gy, cmap='gray', vmin=-0.3, vmax=0.3)
      7 plt.show()
----> 8 plt.imsave('./extras/lego_x.jpg', gx, cmap='gray', vmin=-0.3, vmax=0.3)

/Users/rachel/.pyenv/lib/python2.7/site-packages/matplotlib/pyplot.pyc in imsave(*args, **kwargs)
   2213 @docstring.copy_dedent(_imsave)
   2214 def imsave(*args, **kwargs):
-> 2215     return _imsave(*args, **kwargs)
   2216 
   2217 

/Users/rachel/.pyenv/lib/python2.7/site-packages/matplotlib/image.pyc in imsave(fname, arr, vmin, vmax, cmap, format, origin, dpi)
   1308     canvas = FigureCanvas(fig)
   1309     im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
-> 1310     fig.savefig(fname, dpi=dpi, format=format, transparent=True)
   1311 
   1312 

/Users/rachel/.pyenv/lib/python2.7/site-packages/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
   1468             self.set_frameon(frameon)
   1469 
-> 1470         self.canvas.print_figure(*args, **kwargs)
   1471 
   1472         if frameon:

/Users/rachel/.pyenv/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2190                 orientation=orientation,
   2191                 bbox_inches_restore=_bbox_inches_restore,
-> 2192                 **kwargs)
   2193         finally:
   2194             if bbox_inches and restore_bbox:

/Users/rachel/.pyenv/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_jpg(self, filename_or_obj, *args, **kwargs)
    569                 options['quality'] = rcParams['savefig.jpeg_quality']
    570 
--> 571             return image.save(filename_or_obj, format='jpeg', **options)
    572         print_jpeg = print_jpg
    573 

/Users/rachel/.pyenv/lib/python2.7/site-packages/PIL/Image.pyc in save(self, fp, format, **params)
   1655 
   1656         if isPath(fp):
-> 1657             fp = builtins.open(fp, "wb")
   1658             close = 1
   1659         else:

IOError: [Errno 2] No such file or directory: './extras/lego_x.jpg'

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])


0.345098039216
0.345097941259

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)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-52653c51fbc6> in <module>()
      2 instructions = 'Use the mouse to draw a loose polygon around the object.\nDouble-click on the last point to exit.'
      3 s_xs, s_ys = get_outline(s, instructions)
----> 4 mask = get_mask(s, s_xs, s_ys)

/Users/rachel/Dropbox/programming/CS294-26_Computational_Photography/project_4/get_mask.py in get_mask(im, ys, xs)
     32         print('too far out, try again!')
     33     mask = np.zeros(im.shape)
---> 34     mask[rr, cc] = 1.0
     35     return mask

IndexError: index 355 is out of bounds for axis 0 with size 355

In [29]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [32]:


In [34]:


In [21]:


In [19]:


In [36]:
plt.close('all')

In [ ]: