Parallel Recursive Filtering of Infinite Input Extensions

This notebook tests alg5epe

Algorithm 5 Even-Periodic Extension


In [1]:
import math
import cmath
import numpy as np
from scipy import ndimage, linalg
from skimage.color import rgb2gray
from skimage.measure import structural_similarity as ssim
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
%matplotlib inline
plt.gray() # to plot gray images using gray scale


<matplotlib.figure.Figure at 0x16a9bd30>

In [2]:
%run 'all_functions.ipynb'


All functions loaded successfully!

First: load the test image and run Gaussian filter on it


In [3]:
%%time
X1 = plt.imread('input.png')
X1 = rgb2gray(X1)
s = 16. # sigma for testing filtering
X2 = np.copy(X1).astype(np.float64)
# Gaussian filter runs with periodic extension
X2 = ndimage.filters.gaussian_filter(X1, sigma=s, mode='reflect')


Wall time: 79 ms

Second: setup basic parameters from the input image


In [4]:
%%time
b = 32 # squared block size (b,b)
w = [ weights1(s), weights2(s) ] # weights of the recursive filter
width, height = X1.shape[1], X1.shape[0]
m_size, n_size = get_mn(X1, b)
blocks = break_blocks(X1, b, m_size, n_size)
# Pre-computation of matrices and pre-allocation of carries
alg5m1 = build_alg5_matrices(b, 1, w[0], width, height)
alg5m2 = build_alg5_matrices(b, 2, w[1], width, height)
alg5c1 = build_alg5_carries(m_size, n_size, b, 1)
alg5c2 = build_alg5_carries(m_size, n_size, b, 2)
alg5epem1 = build_epe_matrices(1, w[0], alg5m1)
alg5epem2 = build_epe_matrices(2, w[1], alg5m2)


Wall time: 53 ms

Third: run alg5epe with filter order 1 then 2


In [5]:
%%time
# Running alg5epe with filter order r = 1
alg5_stage1(m_size, n_size, 1, w[0], alg5m1, alg5c1, blocks)
alg5_epe_stage23(m_size, n_size, alg5m1, alg5epem1, alg5c1)
alg5_epe_stage45(m_size, n_size, 1, alg5m1, alg5epem1, alg5c1)
alg5_stage6(m_size, n_size, w[0], alg5c1, blocks)
# Running alg5epe with filter order r = 2
alg5_stage1(m_size, n_size, 2, w[1], alg5m2, alg5c2, blocks)
alg5_epe_stage23(m_size, n_size, alg5m2, alg5epem2, alg5c2)
alg5_epe_stage45(m_size, n_size, 2, alg5m2, alg5epem2, alg5c2)
alg5_stage6(m_size, n_size, w[1], alg5c2, blocks)
# Join blocks back together
X3 = join_blocks(blocks, b, m_size, n_size, X1.shape)


Wall time: 4.34 s

Fourth: show both results and error measurements


In [6]:
fig, (ax2, ax3) = plt.subplots(1, 2)
fig.set_figheight(9)
fig.set_figwidth(14)
ax2.imshow(X2)
ax3.imshow(X3)
print '[ Mean Squared Error:', mean_squared_error(X2, X3), ' ]',
print '[ Structural similarity:', ssim(X2, X3), ' ]'


[ Mean Squared Error: 1.03991074552e-06  ] [ Structural similarity: 0.999989014058  ]

Conclusion: direct convolution (left) and recursive filtering (right) present the same result when considering even-periodic extension