Use scipy.signal.convolve2d to apply a two-dimensional filter kernel to an image array. For an output with the same height and width as the input, set mode="same"; choose a boundary rule based on what should happen beyond the image edges. The examples below cover those choices, plus gradient and edge filters.
What convolve2d does
scipy.signal.convolve2d computes the two-dimensional discrete linear convolution of two 2-D arrays: typically an image and a filter kernel. It is part of scipy.signal, and the SciPy v1.18.0 API reference documents the signature as convolve2d(in1, in2, mode='full', boundary='fill', fillvalue=0). SciPy API reference
As an Amazon Associate I earn from qualifying purchases.
Convolution is not the same as cross-correlation: convolution reverses the kernel according to the operation’s mathematics. With directional kernels, that can affect orientation or sign when comparing results with libraries or code that use correlation-style filtering. SciPy documents the related operation separately as correlate2d.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsHow to choose output size and edge behavior
The mode option determines which region of the convolution result is returned. The boundary option determines how samples outside the image are treated when a kernel overlaps an edge.
#1 Best Overall
| Setting | Behavior | Useful when |
|---|---|---|
mode="full" |
Returns the full discrete linear convolution, including the expanded result around the input. | You need the complete convolution rather than an image-sized result. |
mode="same" |
Returns the portion centered relative to the full result, with the same shape as in1. |
You want a filtered array aligned to the input image dimensions. |
mode="valid" |
Returns only values that do not rely on zero padding. One input must be at least as large as the other in every dimension. | You want results only where the kernel fits without extending beyond the input. |
Boundary handling is separate from output size. The API’s default is boundary="fill" with fillvalue=0. boundary="wrap" treats the array as circular, so values from the opposite edge can contribute. boundary="symm" uses symmetrical boundaries, extending the image symmetrically at its edges. These choices produce different edge pixels; there is no single rule that is right for every image.
A common starting point for an image-sized result is:
Rank #2
from scipy import signal
filtered = signal.convolve2d(image, kernel, mode="same", boundary="symm")
Here image and kernel must be two-dimensional arrays. Symmetric boundaries are useful when extending the image by reflection-like symmetry is appropriate; use fill or wrap instead when those assumptions better fit the data.
Use convolve2d to calculate image gradients
The SciPy v1.18.0 API example applies a complex Scharr operator to compute an image gradient. Its real and imaginary parts encode horizontal and vertical responses. After convolution, the absolute value gives gradient magnitude and the angle gives gradient orientation. The example uses mode="same" and boundary="symm". SciPy’s Scharr example
The manual describes this example as: “Compute the gradient of an image by 2D convolution with a complex Scharr operator.” The symmetric boundary choice is intended to avoid creating edges at the image boundaries.
Use a Laplacian kernel to emphasize edges
SciPy’s signal tutorial demonstrates a four-neighbor Laplacian kernel that emphasizes changes in intensity:
Rank #4
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")
This returns a filtered response, not an automatic binary edge map. The kernel and the chosen boundary rule determine the response values, which you can then interpret or process further for your application. SciPy signal-processing tutorial
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →When to use another SciPy filtering method
convolve2d is a straightforward choice for a 2-D image and a 2-D kernel, but the best method depends on the array dimensions, kernel structure, boundary requirements, and workload. SciPy’s signal tutorial also covers general N-D convolution, FFT-based convolution, and separable filtering with sepfir2d. A Gaussian filter, for example, can be factored into row and column components. The tutorial does not establish one universally fastest method; compare approaches for the arrays and kernels you actually use. SciPy signal-processing tutorial
Best Value
Array API backend support in SciPy v1.18.0
The v1.18.0 API reference labels Array API Standard support for convolve2d experimental. It lists NumPy, CuPy, PyTorch, JAX, and Dask in particular CPU and GPU combinations, and notes that JAX supports only boundary="fill" with fillvalue=0. Treat this as version-specific support rather than a permanent compatibility guarantee; check the manual for the SciPy version and backend you use. SciPy v1.18.0 API reference
Quick Recap
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.




