Implementation:Kornia Kornia Edge Map Processing
| Knowledge Sources | |
|---|---|
| Domains | Vision Image_Processing |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Pattern for post-processing edge detection outputs using PyTorch operations and Kornia's EdgeDetector visualization.
Description
This documents the common patterns for edge map post-processing. Thresholding uses torch.threshold or comparison operators on gradient magnitudes. Sigmoid normalization uses torch.sigmoid on raw logits from deep models. The EdgeDetector.visualize method converts edge maps to RGB images or PIL Images for display. These are user-applied patterns rather than a single API.
Usage
Apply after edge detection to prepare outputs for visualization or downstream processing.
Code Reference
Source Location
- Repository: kornia
- File: kornia/filters/canny.py (hysteresis L132-159), kornia/contrib/edge_detection.py (visualize L118-141)
Signature
Common interface patterns:
# Binary thresholding
binary_edges = (magnitude > threshold).float()
# Sigmoid normalization
normalized = torch.sigmoid(raw_logits)
# EdgeDetector visualization
rgb_edges = EdgeDetector.visualize(images, edge_maps, output_type="torch")
Import
from kornia.contrib import EdgeDetector # for visualize method
import torch # for torch.threshold, torch.sigmoid
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| raw_edge_map | torch.Tensor | Yes | Edge detection output of shape (B, 1, H, W) or (B, C, H, W) |
| threshold | float | No | Threshold value for binary edge conversion |
| images | torch.Tensor | No | Original images for visualization overlay |
Outputs
| Name | Type | Description |
|---|---|---|
| binary_edges | torch.Tensor | Binary edge map of shape (B, 1, H, W) with values 0 or 1 |
| normalized_map | torch.Tensor | Sigmoid-normalized edge map in [0, 1] range |
| rgb_visualization | PIL.Image | RGB visualization of edges for display |
Usage Examples
Thresholding Sobel Output
import torch
from kornia.filters import Sobel, Canny
# Thresholding Sobel output
sobel = Sobel()
img = torch.rand(1, 1, 256, 256)
magnitude = sobel(img)
binary_edges = (magnitude > 0.1).float()
# Canny already includes thresholding
canny = Canny(low_threshold=0.1, high_threshold=0.3)
mag, edges = canny(img)
# edges is already binary from hysteresis
Sigmoid Normalization of DexiNed Output
import torch
from kornia.filters import DexiNed
# Get raw logits from DexiNed
model = DexiNed(pretrained=True).eval()
img = torch.rand(1, 3, 352, 352)
with torch.no_grad():
logits = model(img)
# Normalize to [0, 1] range
edge_map = torch.sigmoid(logits)
# Convert to binary with threshold
binary = (edge_map > 0.5).float()
EdgeDetector Visualization
from kornia.contrib import EdgeDetectorBuilder
detector = EdgeDetectorBuilder.build(pretrained=True, image_size=352)
img = torch.rand(1, 3, 480, 640)
edges = detector(img)
# Get PIL images for display
pil_images = detector.visualize(img, edges, output_type="pil")
# Get torch tensors for further processing
torch_vis = detector.visualize(img, edges, output_type="torch")