@@ -0,0 +1,2367 @@
+import numpy as np%0Aimport scipy.ndimage.filters as spf%0A%0A%0Adef GradientDiffusion(dX, dY, Mu=5, Lambda=5, Mask, Iterations=10,%0A                      dT=0.05):%0A    %22%22%22%0A    Diffusion of gradient field using Navier-Stokes equation. Used for%0A    smoothing/denoising a gradient field.%0A%0A    Takes as input a gradient field image (dX, dY), and a mask of the%0A    foreground region, and then iteratively solves the Navier-Stokes equation%0A    to diffuse the vector field and align noisy gradient vectors with their%0A    surrounding signals.%0A%0A    Parameters%0A    ----------%0A    dX : array_like%0A        Horizontal component of gradient image.%0A    dY : array_like%0A        Vertical component of gradient image.%0A    Mu : float%0A        Weight parmeter from Navier-Stokes equation - weights divergence and%0A        Laplacian terms. Default value = 5.%0A    Lambda : float%0A        Weight parameter from Navier-Stokes equation - used to weight%0A        divergence. Default value = 5.%0A    Mask : array_like%0A        Binary mask where foreground objects have value 1, and background%0A        objects have value 0. Used to restrict influence of background vectors%0A        on diffusion process.%0A    Iterations : float%0A        Number of time-steps to use in solving Navier-Stokes. Default value =%0A        10.%0A    dT : float%0A        Timestep to be used in solving Navier-Stokes. Default value = 0.05.%0A%0A    Returns%0A    -------%0A    vX : array_like%0A        Horizontal component of diffused gradient.%0A    vY : array_like%0A        Vertical component of diffused gradient.%0A%0A    See Also%0A    --------%0A    GradientFlow%0A%0A    References%0A    ----------%0A    .. %5B1%5D G. Li et al %223D cell nuclei segmentation based on gradient flow%0A    tracking%22 in BMC Cell Biology,vol.40,no.8, 2007.%0A    %22%22%22%0A%0A    # initialize solution%0A    vX = dX.copy()%0A    vY = dY.copy()%0A%0A    # iterate for prescribed number of iterations%0A    for it in range(Iterations):%0A%0A        # calculate divergence of current solution%0A        vXY, vXX = np.gradient(vX)%0A        vYY, vYX = np.gradient(vY)%0A        Div = vXX + vYY%0A        DivY, DivX = np.gradient(Div)%0A%0A        # calculate laplacians of current solution%0A        vX += dT*(Mu * spf.laplace(vX) + (Lambda + Mu) * DivX + Mask *%0A                  (dX - vX))%0A        vY += dT*(Mu * spf.laplace(vY) + (Lambda + Mu) * DivY + Mask *%0A                  (dY - vY))%0A%0A    # return solution%0A    return vX, vY%0A

Create GradientDiffusion.py