@@ -0,0 +1,1816 @@
+import numpy as np%0A%0A%0Adef image_tv(x, fn=0, nn=3, a=0):%0A    %22%22%22 Computes total variation (TV) of matrix x along axis a in two%0A    directions.%0A%0A    Parameters%0A    ----------%0A    x : 2D ndarray%0A        matrix x%0A    fn : int%0A        Distance of first neighbor to be included in TV calculation. If fn=0%0A        the own point is also included in the TV calculation.%0A    nn : int%0A        Number of points to be included in TV calculation.%0A    a : int (0 or 1)%0A        Axis along which TV will be calculated. Default a is set to 0.%0A%0A    Returns%0A    -------%0A    PTV : 2D ndarray%0A        Total variation calculated from the right neighbors of each point%0A    NTV : 2D ndarray%0A        Total variation calculated from the left neighbors of each point%0A    Note%0A    ----%0A    This function was created to deal with gibbs artefacts of MR images.%0A    Assuming that MR images are reconstructed from estimates of their Fourier%0A    expansion coefficients, during TV calculation matrix x can taken as and%0A    periodic signal. In this way NTV values on the image left boundary is%0A    computed using the time series values on the right boundary and vice versa.%0A    %22%22%22%0A    if a:%0A        xs = x.copy()%0A    else:%0A        xs = x.T.copy()%0A%0A    xs = np.concatenate((xs%5B:, (-nn-fn):%5D, xs, xs%5B:, 0:(nn+fn)%5D), axis=1)%0A%0A    PTV = np.absolute(xs%5B:, (nn+fn):(-nn-fn)%5D - xs%5B:, (nn+fn+1):(-nn-fn+1)%5D)%0A    NTV = np.absolute(xs%5B:, (nn+fn):(-nn-fn)%5D - xs%5B:, (nn+fn-1):(-nn-fn-1)%5D)%0A    for n in np.linspace(fn+1, nn-2, num=nn-2):%0A        PTV = PTV + np.absolute(xs%5B:, (nn+fn+n):(-nn-fn+n)%5D -%0A                                xs%5B:, (nn+fn+n+1):(-nn-fn+n+1)%5D)%0A        NTV = NTV + np.absolute(xs%5B:, (nn+fn-n):(-nn-fn-n)%5D -%0A                                xs%5B:, (nn+fn-n-1):(-nn-fn-n-1)%5D)%0A%0A    if a:%0A        return PTV, NTV%0A    else:%0A        return PTV.T, NTV.T%0A

Add total variation function need to access sub-voxel shifts