Skip to content

[Feature]: Add Canny Edge Detection AlgorithmΒ #836

@ana-jiangR

Description

@ana-jiangR

πŸš€ 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:
Image

Sobel threshold:
Image

Canny edges:
Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is neededtriagewait for a maintainer to approve and assign this ticket

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions