skimage2.measure.block_reduce#
- skimage2.measure.block_reduce(image, block_size=2, func=<function sum>, cval=0, func_kwargs=None)[source]#
Downsample image by applying function
functo local blocks.This function is useful for max and mean pooling, for example.
- Parameters:
- image(M[, …]) ndarray
N-dimensional input image.
- block_sizearray_like or int
Array containing down-sampling integer factor along each axis. Default block_size is 2.
- funccallable
Function object which is used to calculate the return value for each local block. This function must implement an
axisparameter. Primary functions arenumpy.sum,numpy.min,numpy.max,numpy.meanandnumpy.median. See alsofunc_kwargs.- cvalfloat
Constant padding value if image is not perfectly divisible by the block size.
- func_kwargsdict
Keyword arguments passed to
func. Notably useful for passing dtype argument tonp.mean. Takes dictionary of inputs, e.g.:func_kwargs={'dtype': np.float16}).
- Returns:
- imagendarray
Down-sampled image with same number of dimensions as input image.
Examples
>>> from _skimage2.measure import block_reduce >>> image = np.arange(3*3*4).reshape(3, 3, 4) >>> image array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]]]) >>> block_reduce(image, block_size=(3, 3, 1), func=np.mean) array([[[16., 17., 18., 19.]]]) >>> image_max1 = block_reduce(image, block_size=(1, 3, 4), func=np.max) >>> image_max1 array([[[11]], [[23]], [[35]]]) >>> image_max2 = block_reduce(image, block_size=(3, 1, 4), func=np.max) >>> image_max2 array([[[27], [31], [35]]])