Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Apply 2D Kernels to Images with SciPy

Apply 2-D kernels to images with SciPy’s convolve2d. Learn how output modes and boundary settings affect results, with Scharr gradient and Laplacian examples.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

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

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:

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.

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

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:

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 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

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.