median_filter#

cbclib.bin.median_filter(inp: numpy.ndarray, size: Optional[Union[int, Tuple[int, Ellipsis]]] = None, footprint: Optional[numpy.ndarray] = None, mask: Optional[numpy.ndarray] = None, inp_mask: Optional[numpy.ndarray] = None, mode: str = 'reflect', cval: float = 0.0, num_threads: int = 1)[source]#

Calculate a multidimensional median filter.

Parameters
  • inp (numpy.ndarray) – Input array. Must be one of the following types: np.float64, np.float32, np.int32, np.uint32, np.uint64.

  • size (Optional[Union[int, Tuple[int, Ellipsis]]]) – See footprint, below. Ignored if footprint is given.

  • footprint (Optional[numpy.ndarray]) – Either size or footprint must be defined. size gives the shape that is taken from the input array, at every element position, to define the input to the filter function. footprint is a boolean array that specifies (implicitly) a shape, but also which of the elements within this shape will get passed to the filter function. Thus size=(n, m) is equivalent to footprint=np.ones((n, m)). We adjust size to the number of dimensions of the input array, so that, if the input array is shape (10, 10, 10), and size is 2, then the actual size used is (2, 2, 2). When footprint is given, size is ignored.

  • mask (Optional[numpy.ndarray]) – Output mask. Median is calculated only where mask is True, output array set to 0 otherwise. Median is calculated over the whole input array by default.

  • inp_mask (Optional[numpy.ndarray]) – Input mask. Median takes into account only the inp values, where inp_mask is True. inp_mask is equal to mask by default.

  • mode (str) –

    The mode parameter determines how the input array is extended when the filter overlaps a border. Default value is ‘reflect’. The valid values and their behavior is as follows:

    • constant, (k k k k | a b c d | k k k k) : The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

    • nearest, (a a a a | a b c d | d d d d) : The input is extended by replicating the last pixel.

    • mirror, (c d c b | a b c d | c b a b) : The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

    • reflect, (d c b a | a b c d | d c b a) : The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

    • wrap, (a b c d | a b c d | a b c d) : The input is extended by wrapping around to the opposite edge.

  • cval (float) – Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

  • num_threads (int) – Number of threads used in the calculations.

Raises
  • ValueError – When neither size nor footprint are provided.

  • TypeError – If data has incompatible type.

  • RuntimeError – If C backend exited with error.

Returns

Filtered array. Has the same shape as inp.

Return type

numpy.ndarray