medianfilter: OpenCL median filter

A module for performing the 1d, 2d and 3d median filter …

The target is to mimic the signature of scipy.signal.medfilt and scipy.medfilt2

The first implementation targets 2D implementation where this operation is costly (~10s/2kx2k image)

class MedianFilter2D(shape, kernel_size=(3, 3), ctx=None, devicetype='all', platformid=None, deviceid=None, block_size=None, profile=False)[source]

A class for doing median filtering using OpenCL

set_kernel_arguments()[source]

Parametrize all kernel arguments

send_buffer(data, dest)[source]

Send a numpy array to the device, including the cast on the device if possible

Parameters
  • data – numpy array with data

  • dest – name of the buffer as registered in the class

calc_wg(kernel_size)[source]

calculate and return the optimal workgroup size for the first dimension, taking into account the 8-height band

Parameters

kernel_size – 2-tuple of int, shape of the median window

Returns

optimal workgroup size

medfilt2d(image, kernel_size=None)[source]

Actually apply the median filtering on the image

Parameters
  • image – numpy array with the image

  • kernel_size – 2-tuple if

Returns

median-filtered 2D image

Nota: for window size 1x1 -> 7x7 up to 49 / 64 elements in 8 threads, 8elt/th

9x9 -> 15x15 up to 225 / 256 elements in 32 threads, 8elt/th 17x17 -> 21x21 up to 441 / 512 elements in 64 threads, 8elt/th

TODO: change window size on the fly,

static calc_kernel_size(kernel_size)[source]

format the kernel size to be a 2-length numpy array of int32

medfilt2d(ary, kernel_size=3)

Median filter a 2-dimensional array.

Apply a median filter to the input array using a local window-size given by kernel_size (must be odd).

Parameters
  • ary – A 2-dimensional input array.

  • kernel_size – A scalar or a list of length 2, giving the size of the median filter window in each dimension. Elements of kernel_size should be odd. If kernel_size is a scalar, then this scalar is used as the size in each dimension. Default is a kernel of size (3, 3).

Returns

An array the same size as input containing the median filtered result. always work on float32 values

About the padding:

  • The filling mode in scipy.signal.medfilt2d is zero-padding

  • This implementation is equivalent to:

    scipy.ndimage.filters.median_filter(ary, kernel_size, mode=”nearest”)