Implementation:Kornia Kornia Sobel Operator
| Knowledge Sources | |
|---|---|
| Domains | Vision Filters |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Concrete tool for computing Sobel gradient edge magnitude provided by Kornia's filters module.
Description
The Sobel class computes the gradient magnitude per channel using Sobel operators. It applies horizontal and vertical 3x3 Sobel kernels, computes the magnitude sqrt(gx^2 + gy^2), and optionally normalizes the kernel. The output preserves the per-channel structure of the input.
The related Laplacian class computes second-order derivative edges using a configurable kernel size.
Usage
Import for gradient-based edge detection or as a building block for custom edge detection pipelines.
Code Reference
Source Location
- Repository: kornia
- File: kornia/filters/sobel.py (Sobel L251-280), kornia/filters/laplacian.py (Laplacian L66-109)
Signature
Sobel:
class Sobel(nn.Module):
def __init__(self, normalized: bool = True, eps: float = 1e-6) -> None
def forward(self, input: torch.Tensor) -> torch.Tensor
Laplacian:
class Laplacian(nn.Module):
def __init__(
self,
kernel_size: tuple[int, int] | int,
border_type: str = "reflect",
normalized: bool = True,
) -> None
def forward(self, input: torch.Tensor) -> torch.Tensor
Import
from kornia.filters import Sobel, Laplacian
I/O Contract
Inputs
Sobel constructor parameters:
| Name | Type | Required | Description |
|---|---|---|---|
| normalized | bool | No | Normalize the Sobel kernel (default True) |
| eps | float | No | Regularization constant (default 1e-6) |
Sobel forward parameters:
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor | Yes | Input image tensor of shape (B, C, H, W) |
Laplacian constructor parameters:
| Name | Type | Required | Description |
|---|---|---|---|
| kernel_size | int | Yes | Size of the Laplacian kernel |
| border_type | str | No | Padding mode (default "reflect") |
| normalized | bool | No | Normalize the kernel (default True) |
Laplacian forward parameters:
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor | Yes | Input image tensor of shape (B, C, H, W) |
Outputs
Sobel:
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Gradient magnitude of shape (B, C, H, W) |
Laplacian:
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Laplacian response of shape (B, C, H, W) |
Usage Examples
Sobel Edge Detection
import torch
from kornia.filters import Sobel
# Create Sobel operator
sobel = Sobel(normalized=True)
# Compute gradient magnitude
img = torch.rand(1, 3, 256, 256)
edges = sobel(img) # (1, 3, 256, 256)
Laplacian Edge Detection
import torch
from kornia.filters import Laplacian
# Create Laplacian operator
laplacian = Laplacian(kernel_size=3)
# Compute Laplacian edges
img = torch.rand(1, 1, 256, 256)
edges = laplacian(img) # (1, 1, 256, 256)