kornia/kornia-rs

[Feature]: Add Canny Edge Detection Algorithm

Open

#836 opened on Mar 19, 2026

 (5 comments) (0 reactions) (0 assignees)Rust (188 forks)auto 404
enhancementhelp wantedtriage

Repository metrics

Stars
 (675 stars)
PR merge metrics
 (PR metrics pending)

Description

🚀 Feature Description

Add a Canny edge detection function to kornia-imgproc. Canny is an edge detection algorithm that produces clean 1-pixel-wide binary edge maps from grayscale images. kornia-rs already has the two main building blocks (Gaussian blur and Sobel) for this algorithm.

📂 Feature Category

Rust Core Library

💡 Motivation

Canny is one of the most widely used edge detection algorithms in computer vision. Having Canny would give new features to kornia-rs that depend on edge detection as input. Right now Kornia already has kornia.filters.Canny, but there's no Rust equivalent in kornia-rs yet. Also, it is a natural addition to kornia-rs's current image processing pipeline. It can be built directly based on the existing Gaussian blur and Sobel operators.

💭 Proposed Solution

Add canny.rs inside crates/kornia-imgproc/src. The signature is below:

pub fn canny<A1, A2, A3>(
    src: &Image<u8, 1, A1>,
    magnitude: &mut Image<f32, 1, A2>,
    edges: &mut Image<u8, 1, A3>,
    low_threshold: f32,
    high_threshold: f32,
    kernel_size: usize,
    hysteresis: bool,
) -> Result<(), ImageError>

The input is a single-channel u8 grayscale image. The function internally converts to f32 for processing. The edge output is a binary u8 image where edges are 255 and non-edges are 0. The magnitude output is f32 for raw gradient strength.

The implementation follows the standard 5-stage pipeline:

  1. Gaussian blur (existing gaussian_blur)
  2. Sobel gradients (existing separable_filter + sobel_kernel_1d)
  3. Non-maximum suppression along gradient direction
  4. Double thresholding
  5. BFS-based hysteresis edge tracking

📚 Library Reference

The implementation of this feature is basically based on:

🔄 Alternatives Considered

No response

🎯 Use Cases

It will be useful for contour extraction (as kornia-rs already has find_contour), object boundary/edge detection, lane detection, and general feature extraction pipelines.

kornia-rs currently have Sobel gradients, gaussian blur, and binary thresholding. A typical workaround would be to threshold Sobel magnitude. Here is a comparison between the typical workaround and the demo implementation of canny:

Input grayscale image:

Sobel threshold:

Canny edges:

For this image, canny edge detection algorithm works better than sobel threshold method.

📝 Additional Context

I currently have a working demo implementation. Happy to submit a PR and continue work on it if approved!

🤝 Contribution Intent

  • I plan to submit a PR to implement this feature
  • I'm requesting this feature but not planning to implement it

Contributor guide