Image.convolve#

Image.convolve(K: Kernel | ndarray | Any, mode: str = 'same', border: str = 'reflect', bordervalue: int | float = 0, dtype: Literal['same', 'int16', 'float32', 'float64'] = 'same') → Any[source]#

Image convolution

Parameters:
  • K (Kernel or ndarray, optional) – convolution kernel

  • mode (str, optional) – option for convolution, defaults to ‘same’

  • border (str, optional) – option for boundary handling, defaults to ‘reflect’

  • bordervalue (scalar, optional) – padding value, defaults to 0

  • dtype (str, optional) – datatype of the result, defaults to 'same', the datatype of the input image

Returns:

input image convolved with kernel

Return type:

Image

Computes the convolution of image with the kernel K.

There are two options that control what happens at the edge of the image where the convolution window lies outside the image border. mode controls the size of the resulting image, while border controls how pixel values are extrapolated outside the image border.

mode

description

'same'

output image is same size as input image (default)

'full'

output image is larger than the input image, add border to input image

'valid'

output image is smaller than the input image and contains only valid pixels

border

description

'replicate'

replicate border pixels outwards

'pad'

outside pixels are set to value

'wrap'

borders are joined, left to right, top to bottom

'reflect'

outside pixels reflect inside pixels

'reflect101'

outside pixels reflect inside pixels except for edge

'none'

do not look outside of border

Examples

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> img = Image.Read('monalisa.png')
>>> img.convolve(K=np.ones((11,11))).disp()
<matplotlib.image.AxesImage object at 0x741184850d40>

dtype

description

'same'

result has the datatype of the input image (default)

'int16'

signed 16-bit integer, for uint8 or int16 input

'float32'

single precision, for any input except float64

'float64'

double precision, for any input

Changed in version 2.5.0: Added the dtype argument.

Note

  • The kernel is typically square with an odd side length.

  • OpenCV sums the products of kernel and pixel values in a wider type than the image, so the sum itself does not overflow. The sum is then converted to the datatype of the result, and values outside its range are clipped (saturated), not wrapped. With the default dtype='same' a uint8 result is limited to 0 to 255, so for a kernel whose results can be negative (for example an edge detection kernel) every negative result becomes 0, and for a kernel that sums to more than one (for example np.ones((11, 11))) bright regions saturate at 255. Use dtype='float32' to keep the sign and the magnitude.

  • A uint8 image convolved to dtype='int16' keeps the sign, but also clips, at -32768 and 32767, which a large kernel can reach. dtype='float32' holds the result of any realistic kernel.

  • Only combinations OpenCV supports are allowed, a result cannot be less precise than a floating point input.

  • If the image is color (has multiple planes) the kernel is applied to each plane, resulting in an output image with the same number of planes.

Warning

treats NaNs in input image or kernels as standard IEEE-754 floating-point values, meaning any convolution operation involving a NaN results in NaN. Because convolution is a sum of products, a single NaN within the kernel’s window propagates to make the output pixel NaN. Use fixbad for handling NaN values.

References

Important

Uses OpenCV functions cv2.filter2D and cv2.copyMakeBorder which accept multiple-channel, CV_8U, CV_16U, CV_16S, CV_32F or CV_64F images.