In [1]:
%run ../common.ipynb


Populating the interactive namespace from numpy and matplotlib

In [2]:
sobel = np.array([[-1,0,1],
                  [-2,0,2],
                  [-1,0,1]])

In [3]:
image = imread('../MP.tiff')

In [4]:
image.shape


Out[4]:
(2111, 2198)

In [5]:
P = image.shape[0] + sobel.shape[0] - 1
Q = image.shape[1] + sobel.shape[1] - 1
P,Q


Out[5]:
(2113, 2200)

In [6]:
numpy.pad?

In [7]:
# pad with zeros - page 252
padding = ((0,P-sobel.shape[0]), (0,Q-sobel.shape[1]))
sobel_padded = numpy.pad(sobel, pad_width=padding, mode='constant')

In [8]:
sobel_padded.shape


Out[8]:
(2113, 2200)

In [9]:
padding = ((0,P-image.shape[0]), (0,Q-image.shape[1]))
image_padded = numpy.pad(image, pad_width=padding, mode='constant')

In [10]:
image_padded.shape


Out[10]:
(2113, 2200)

In [11]:
H = fft2(sobel_padded)
H = 1j*H.imag
specshow(H)


-c:3: RuntimeWarning: divide by zero encountered in log

In [12]:
F = fft2(image_padded)
specshow(F)



In [13]:
G = F*H
g = ifft2(G).real
gimshow(g)



In [14]:
g[100:110,100:110]


Out[14]:
array([[ -1.50000002e+00,   7.50000009e+00,   8.50000010e+00,
         -1.76811194e-15,   4.00000005e+00,   1.35000002e+01,
          1.30000001e+01,  -5.00000006e-01,  -1.00000001e+01,
         -8.50000010e+00],
       [ -1.00000001e+01,   2.00000002e+00,   9.00000010e+00,
         -1.88336320e-14,   1.50000002e+00,   1.25000001e+01,
          1.80000002e+01,   5.50000006e+00,  -6.50000007e+00,
         -1.05000001e+01],
       [ -8.50000010e+00,   5.00000006e-01,   5.00000006e+00,
         -2.00000002e+00,  -5.00000006e-01,   6.50000007e+00,
          1.75000002e+01,   1.30000001e+01,   2.00000002e+00,
         -9.00000010e+00],
       [ -1.83657315e-15,   1.50000002e+00,  -2.00000002e+00,
         -5.00000006e+00,  -6.33183343e-15,   1.50000002e+00,
          1.10000001e+01,   1.55000002e+01,   1.15000001e+01,
         -2.50000003e+00],
       [  6.00000007e+00,   2.00000002e+00,  -7.00000008e+00,
         -5.00000006e+00,   1.50000002e+00,  -5.00000006e-01,
          4.00000005e+00,   1.10000001e+01,   1.60000002e+01,
          5.00000006e+00],
       [  6.00000007e+00,   2.00000002e+00,  -5.50000006e+00,
         -1.00000001e+00,   2.00000002e+00,  -3.00000003e+00,
         -1.50000002e+00,   4.00000005e+00,   1.25000001e+01,
          9.00000010e+00],
       [  2.00000002e+00,   1.00000001e+00,  -5.00000006e-01,
          2.00000002e+00,   5.00000006e-01,  -3.50000004e+00,
         -3.00000003e+00,  -8.86312882e-15,   4.50000005e+00,
          7.00000008e+00],
       [ -4.00000005e+00,  -1.50000002e+00,   2.00000002e+00,
          3.00000003e+00,   2.00000002e+00,   1.00000001e+00,
         -5.00000006e-01,  -2.00000002e+00,  -4.00000005e+00,
          5.00000006e-01],
       [ -7.00000008e+00,  -6.50000007e+00,  -1.00000001e+00,
          5.00000006e-01,   5.00000006e+00,   6.50000007e+00,
          2.50000003e+00,  -2.50000003e+00,  -8.50000010e+00,
         -3.00000003e+00],
       [ -5.00000006e+00,  -1.10000001e+01,  -6.50000007e+00,
         -4.00000005e+00,   5.00000006e+00,   9.50000011e+00,
          4.00000005e+00,  -2.00000002e+00,  -8.00000009e+00,
         -2.50000003e+00]])

In [15]:
gimshow(scale(g))



In [16]:
imsave('sobel.tif', scale(g))

In [26]:
%time spatial = ndimage.filters.convolve(image.astype(np.int), sobel)


CPU times: user 153 ms, sys: 9.2 ms, total: 162 ms
Wall time: 162 ms

In [23]:
gimshow(spatial)



In [25]:
%time gimshow(scale(spatial))


CPU times: user 254 ms, sys: 17.4 ms, total: 271 ms
Wall time: 270 ms

In [ ]: