<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Parallel Recursive Filtering of Infinite Input Extensions
This notebook tests alg5cpe
Algorithm 5 Constant-Padding Extension
Step1:  First
Step2:  Second
Step3:  Third
Step4:  Fourth
<ASSISTANT_TASK:>
Python Code:
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
%run 'all_functions.ipynb'
Explanation: Parallel Recursive Filtering of Infinite Input Extensions
This notebook tests alg5cpe
Algorithm 5 Constant-Padding Extension
End of explanation
%%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 constant padding
X2 = ndimage.filters.gaussian_filter(X1, sigma=s, mode='nearest')
Explanation: First: load the test image and run Gaussian filter on it
End of explanation
%%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)
alg5cpem1 = build_cpe_matrices(1, w[0], alg5m1)
alg5cpem2 = build_cpe_matrices(2, w[1], alg5m2)
Explanation: Second: setup basic parameters from the input image
End of explanation
%%time
# Running alg5cpe with filter order r = 1
alg5_cpe_stage1(m_size, n_size, 1, w[0], alg5m1, alg5c1, blocks)
alg5_cpe_stage23(m_size, n_size, alg5m1, alg5cpem1, alg5c1)
alg5_cpe_stage45(m_size, n_size, 1, alg5m1, alg5cpem1, alg5c1)
alg5_stage6(m_size, n_size, w[0], alg5c1, blocks)
# Running alg5cpe with filter order r = 2
alg5_cpe_stage1(m_size, n_size, 2, w[1], alg5m2, alg5c2, blocks)
alg5_cpe_stage23(m_size, n_size, alg5m2, alg5cpem2, alg5c2)
alg5_cpe_stage45(m_size, n_size, 2, alg5m2, alg5cpem2, 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)
Explanation: Third: run alg5cpe with filter order 1 then 2
End of explanation
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), ' ]'
Explanation: Fourth: show both results and error measurements
End of explanation
<END_TASK>