Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use scipy.signal.convolve2d to apply a 2-D filter kernel to a 2-D image array. For an output with the image’s shape, set mode="same"; choose a boundary rule to control what happens beyond the image edges. For many image filters, boundary="symm" is a useful starting point, but the right choice depends on the image and the operation.

Apply a 2-D filter to an image

convolve2d accepts two 2-D arrays: the image and a kernel. The kernel determines the transformation; mode and boundary determine the output region and treatment of image edges.

from scipy import signal

filtered = signal.convolve2d(image, kernel, mode="same", boundary="symm")

This is a starting pattern for a same-sized result, not a universal setting. Select a kernel for the effect you want, then choose edge handling that matches your image and intended interpretation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the output mode

The SciPy v1.18.0 convolve2d API reference defines three modes:

#1 Best Overall
Mode Returned region When it can be useful
full The full discrete linear convolution, including results extending beyond the original image extent. When you need the entire convolution result.
same An output the size of the first input, centered relative to the full result. When downstream image data should retain the input image’s height and width.
valid Only values that do not rely on zero padding. When you want results only where the kernel is fully supported by the input.

For valid, one input must be at least as large as the other in every dimension. Note that same specifies the output size; it does not itself specify how neighborhoods beyond the image boundary are handled.

Choose how the filter treats image edges

The API defaults to boundary="fill" with fillvalue=0. Near an edge, this treats pixels outside the image as the specified fill value. Other documented choices are:

Rank #2
Sale
Introductory Digital Image Processing 4Th Edition
  • Introductory Digital Image Processing 4Th Edition
  • Product Type: ABIS_BOOK
  • fill: extend the image with the selected fill value, zero by default. This can create edge responses where the image meets the padded values.
  • wrap: treat the image as circular, so a neighborhood beyond one edge continues from the opposite edge. Use it only when that wraparound matches the data.
  • symm: use symmetrical boundaries. SciPy’s Scharr example selects this option and notes that it avoids creating edges at image boundaries.

These choices model different assumptions about the image beyond its field of view. Symmetry can be a sensible starting point for ordinary images, but it is not automatically right for every data set or filter.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a complex Scharr operator to calculate gradients

The SciPy v1.18.0 API example shows how to “Compute the gradient of an image by 2D convolution with a complex Scharr operator.” With this operator, the real and imaginary parts of the convolution response represent horizontal and vertical components. Its magnitude gives gradient strength, while its angle gives gradient orientation.

The same example uses mode="same" and boundary="symm". The pattern is useful when you need directional gradient information rather than a single edge-emphasis response. See the official example for the operator definition and complete code.

Use a Laplacian kernel to emphasize edges

The SciPy signal tutorial demonstrates a Laplacian kernel that responds to changes in neighboring pixel values:

import numpy as np
from scipy import signal

laplacian = np.array([[0, 1, 0],
                      [1, -4, 1],
                      [0, 1, 0]])
edges = signal.convolve2d(image, laplacian, mode="same", boundary="symm")

The result emphasizes local intensity changes. It is a filter response, not automatically a finished display image; how you scale, clip, or combine it depends on the image data and the next processing step.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Convolution is not cross-correlation

Convolution reverses the kernel as part of the operation. That matters with directional kernels: compared with a correlation-style filter, the response’s orientation or sign can differ. If the intended operation is template matching or another correlation task, use SciPy’s separately documented correlate2d rather than assuming the two operations are interchangeable.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use another SciPy filtering approach

convolve2d is specifically for two-dimensional inputs. The SciPy signal tutorial describes alternatives for different dimensions or kernel structures; there is no universal speed ranking that applies to every image and filter.

  • General N-D convolution: consider scipy.signal.convolve when the data or kernel is not limited to two dimensions.
  • FFT convolution: consider scipy.signal.fftconvolve when frequency-domain convolution suits the input and kernel sizes. Check boundary and output requirements for the operation you choose.
  • Separable filtering: if a 2-D filter factors into row and column components, successive 1-D filtering may fit the problem. The tutorial discusses sepfir2d and notes that a Gaussian can be factored into row and column components.

Compare options against the actual array dimensions, kernel structure, boundary behavior, and workload rather than assuming one method is always faster. The SciPy signal tutorial covers these approaches.

Array API backend support is experimental

The SciPy v1.18.0 API reference marks Array API Standard support for convolve2d as experimental and lists support for NumPy, CuPy, PyTorch, JAX, and Dask in particular CPU/GPU combinations. This is version-sensitive compatibility information, not a permanent guarantee. The same reference says JAX supports only boundary="fill" and fillvalue=0. Check the current API table for the backend and device you plan to use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
SaleBestseller No. 2
Introductory Digital Image Processing 4Th Edition
Introductory Digital Image Processing 4Th Edition
Introductory Digital Image Processing 4Th Edition; Product Type: ABIS_BOOK
$29.47
SaleBestseller No. 3

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.