def gaussian_filter(pixels, sigma): r""" Calculates the convolution of the input image with a multidimensional Gaussian filter. Parameters ---------- pixels : :map:`Image` or subclass or ``(C, X, Y, ..., Z)`` `ndarray` Either the image object itself or an array with the pixels. The first dimension is interpreted as channels. This means an N-dimensional image is represented by an N+1 dimensional array. sigma : `float` or `list` of `float` The standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a `list`, or as a single `float`, in which case it is equal for all axes. Returns ------- output_image : :map:`Image` or subclass or ``(X, Y, ..., Z, C)`` `ndarray` The filtered image has the same type and size as the input ``pixels``. """ global scipy_gaussian_filter if scipy_gaussian_filter is None: from scipy.ndimage import gaussian_filter as scipy_gaussian_filter output = np.empty(pixels.shape, dtype=pixels.dtype) for dim in range(pixels.shape[0]): scipy_gaussian_filter(pixels[dim], sigma, output=output[dim]) return output
def gaussian_filter(pixels, sigma): r""" Calculates the convolution of the input image with a multidimensional Gaussian filter. Parameters ---------- pixels : :map:`Image` or subclass or ``(C, X, Y, ..., Z)`` `ndarray` Either the image object itself or an array with the pixels. The first dimension is interpreted as channels. This means an N-dimensional image is represented by an N+1 dimensional array. sigma : `float` or `list` of `float` The standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a `list`, or as a single `float`, in which case it is equal for all axes. Returns ------- output_image : :map:`Image` or subclass or ``(X, Y, ..., Z, C)`` `ndarray` The filtered image has the same type and size as the input ``pixels``. """ global scipy_gaussian_filter if scipy_gaussian_filter is None: from scipy.ndimage import gaussian_filter as scipy_gaussian_filter output = np.empty(pixels.shape, dtype=pixels.dtype) for dim in range(pixels.shape[0]): scipy_gaussian_filter(pixels[dim], sigma, output=output[dim]) return output
def gaussian_filter(pixels, sigma): global scipy_gaussian_filter if scipy_gaussian_filter is None: from scipy.ndimage import gaussian_filter as scipy_gaussian_filter output = np.empty(pixels.shape) for dim in range(pixels.shape[2]): scipy_gaussian_filter(pixels[..., dim], sigma, output=output[..., dim]) return output
def test_gaussian_filter_compare_to_scipy(x_1D, x_3D): for x, R in zip((x_1D, x_3D), (R1, R3)): yref = scipy_gaussian_filter(x, R, mode="wrap") ynew = gaussian_filter(x, R) # Compare result to the one using scipy's gaussian filter np.testing.assert_allclose(yref, ynew, atol=1e-4, rtol=0)
# Separable convolution # ----------------------- wg = None shape = (Nc, Nr) k1 = ocl.call(program.horizontal_convolution, shape, wg, d_input, d_tmp, d_gaussian, ksize, Nc, Nr) k1.wait() print("Horizontal convolution took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start))) k2 = ocl.call(program.vertical_convolution, shape, wg, d_tmp, d_output, d_gaussian, ksize, Nc, Nr) k2.wait() print("Vertical convolution took %.3fms" % (1e-6 * (k2.profile.end - k2.profile.start))) # Retrieve result res = ocl.fetch(d_output) if __has_ndimage: print("Max err : %e" % np.max(np.abs(res - scipy_gaussian_filter(img, sigma)))) # Non-separable convolution # ------------------------------ gaussian2 = np.outer(gaussian, gaussian) d_gaussian = ocl.to_device(gaussian2, flags="r") wg = None shape = (Nc, Nr) k3 = ocl.call(program.nonseparable_convolution, shape, wg, d_input, d_output, d_gaussian, ksize, Nc, Nr) k3.wait() print("Nonseparable convolution took %.3fms" % (1e-6 * (k3.profile.end - k3.profile.start))) # Retrieve result res = ocl.fetch(d_output)
d_gaussian, ksize, Nc, Nr) k1.wait() print("Horizontal convolution took %.3fms" % (1e-6 * (k1.profile.end - k1.profile.start))) k2 = ocl.call(program.vertical_convolution, shape, wg, d_tmp, d_output, d_gaussian, ksize, Nc, Nr) k2.wait() print("Vertical convolution took %.3fms" % (1e-6 * (k2.profile.end - k2.profile.start))) # Retrieve result res = ocl.fetch(d_output) if __has_ndimage: print("Max err : %e" % np.max(np.abs(res - scipy_gaussian_filter(img, sigma)))) # Non-separable convolution # ------------------------------ gaussian2 = np.outer(gaussian, gaussian) d_gaussian = ocl.to_device(gaussian2, flags="r") wg = None shape = (Nc, Nr) k3 = ocl.call(program.nonseparable_convolution, shape, wg, d_input, d_output, d_gaussian, ksize, Nc, Nr) k3.wait() print("Nonseparable convolution took %.3fms" % (1e-6 * (k3.profile.end - k3.profile.start))) # Retrieve result
def test_gaussian_filter_3d(x_3D): yref = scipy_gaussian_filter(x_3D, R3, mode="wrap") ynew = gaussian_filter(x_3D, R3) # Compare result to the one using scipy's gaussian filter np.testing.assert_allclose(yref, ynew, atol=1e-4, rtol=0)