Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Sobel Operator

Sobel operator is a 3×3 image filter that measures brightness gradients for edge detection, feature extraction, and image segmentation.

The Sobel operator is a fixed image filter that estimates how quickly brightness changes across an image and in which direction. It produces image gradients: large responses mark sharp transitions, while small responses indicate relatively uniform regions. Also called the Sobel filter or Sobel–Feldman operator, it is a fundamental tool for edge detection and feature extraction in computer vision.

Irwin Sobel and Gary Feldman introduced it at the Stanford Artificial Intelligence Project in 1968 in a presentation titled A 3x3 Isotropic Gradient Operator for Image Processing, described in Sobel's historical account and derivation.

How the Sobel Kernels Work#

Sobel filtering applies two small grids of weights, called kernels, to neighboring pixels. This local filtering is commonly described as convolution; OpenCV applies the weights without flipping them, using correlation. The standard 3 x 3 kernels have these rows:

RowX-direction kernelY-direction kernel
Top-1, 0, 1-1, -2, -1
Middle-2, 0, 20, 0, 0
Bottom-1, 0, 11, 2, 1

The x-direction response, Gx, measures left-to-right intensity changes and emphasizes vertical edges. Gy measures top-to-bottom changes and emphasizes horizontal edges. Each kernel combines a difference filter, with weights -1, 0, 1, along one axis and weighted smoothing, with weights 1, 2, 1, along the other. Its receptive field covers nine pixels.

Combining Gx and Gy gives gradient magnitude—the square root of Gx squared plus Gy squared—and orientation, calculated with atan2(Gy, Gx). The gradient points across an edge, perpendicular to its local boundary. These directional outputs are feature maps: spatial grids describing the response at each location.

Applying Sobel in Python#

OpenCV calculates signed derivatives with cv2.Sobel, as used in its edge-detection implementation guide. Floating-point output preserves negative responses, which occur when brightness decreases along the selected direction; unsigned 8-bit output clips them to zero and loses those edge transitions.

Install the dependencies with pip install opencv-python numpy. This example downloads an image, calculates both derivatives, and saves their combined magnitude:

from urllib.request import urlopen

import cv2
import numpy as np

with urlopen("https://ultralytics.com/images/bus.jpg") as response:
    encoded = np.frombuffer(response.read(), dtype=np.uint8)
image = cv2.imdecode(encoded, cv2.IMREAD_GRAYSCALE)

# Preserve signed derivatives before combining their strengths.
gx = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
gy = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
magnitude = cv2.magnitude(gx, gy)
edge_image = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX)
cv2.imwrite("sobel_edges.png", edge_image.astype(np.uint8))

The saved image displays strong brightness transitions as bright contours. Its normalization rescales values for display; a separate thresholding step converts gradient strength into a binary edge map.

Where Sobel Gradients Are Used#

In image segmentation, Sobel magnitude can provide boundary information for a watershed algorithm, which grows regions from starting markers. In scikit-image's coin-segmentation example, strong gradients form barriers between coins and their background. The watershed uses those barriers to separate foreground from background before connected components label individual coins.

Sobel gradients also guide object detection in YOLO26-RD, a custom model based on Ultralytics YOLO26. Its EdgeSPD module uses a fixed Sobel edge signal to gate downsampling, emphasizing boundary information as spatial resolution decreases.

Sobel vs. Other Edge Filters#

Prewitt uses uniform smoothing weights; Sobel gives the central row or column greater weight. Scharr improves directional accuracy for 3 x 3 derivative estimates. The Laplacian measures second-order intensity changes. Canny combines gradient estimation with edge thinning and two-threshold edge linking, producing a more selective contour map than raw Sobel magnitude.

In medical image analysis, noise-induced gradients can fragment watershed regions: MR image segmentation with Sobel gradients demonstrates oversegmentation and uses denoising to reduce it. At image borders, missing neighbors require a defined extension rule: SciPy's Sobel implementation defaults to reflecting pixels across the edge of the last pixel.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning