Discretization

This notebook investigates the error that is caused by replacing a differentiated image warp by an image gradient applied to the warped image.

[1]:
import numpy as np
import imwip
from matplotlib import pyplot as plt
import tomopy
[2]:
errors = []
im_sizes = [int(1/s) for s in np.linspace(0.1, 0.001, 20)]
for im_size in im_sizes:
    im = tomopy.cameraman(im_size)[0]
    u = im_size/50 * np.repeat(np.sin(np.linspace(0,4*np.pi, im_size, dtype=np.float32)), im_size).reshape((im_size, im_size))
    v = im_size/70 * np.repeat(np.cos(np.linspace(0,4*np.pi, im_size, dtype=np.float32)), im_size).reshape((im_size, im_size))
    diff_x, diff_y = imwip.diff_warp(im, u, v)
    approx_y = np.gradient(imwip.warp(im, u, v), axis=1)
    errors.append(np.linalg.norm(diff_y - approx_y)/im.size)
[3]:
plt.imshow(im)
[3]:
<matplotlib.image.AxesImage at 0x7fd70c562ef0>
../_images/notebooks_discretization_3_1.png
[4]:
plt.imshow(imwip.warp(im, u, v))
[4]:
<matplotlib.image.AxesImage at 0x7fd704a1e890>
../_images/notebooks_discretization_4_1.png
[5]:
plt.plot([1/n for n in im_sizes], errors, "-o")
plt.title("Discretization error when using approximate derivative of image warp")
plt.xlabel("Pixel size")
plt.ylabel("Root mean squared error")
plt.show()
../_images/notebooks_discretization_5_0.png

The error is proportional to the pixel size, which is to be expected as a larger pixel size implies a larger step in the image gradient.