machinevisiontoolbox.Image.convolve

Image.convolve(K, mode='same', border='reflect', bordervalue=0)

Image convolution

Parameters:
  • K (ndarray(N,M)) – 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

Returns:

convolved image

Return type:

Image instance

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

Example:

>>> 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 0x7fe7ff6e22e0>
Note:
  • The kernel is typically square with an odd side length.

  • The result has the same datatype as the input image. For a kernel where the results could be negative (eg. edge detection kernel) this will cause issues such as value wraparound.

  • 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.

References:
  • Robotics, Vision & Control for Python, Section 11.5.1, P. Corke, Springer 2023.

Seealso:

Kernel smooth opencv.filter2D opencv.copyMakeBorder