VPI - Vision Programming Interface

2.2 Release

Canny edge detector


The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images, see [1]. The following table shows an example input image, a set of input parameters for the canny edge detector algorithm (explained below), and the corresponding edge image.

Input Parameters Output

\begin{align*} \mathit{normType} &= VPI\_NORM\_L2 \\ \mathit{gradMethod} &= GEN\_GRADIENT\_SOBEL \\ \mathit{gradientSize} &= 3 \\ \mathit{edgeValue} &= 255 \\ \mathit{nonEdgeValue} &= 0 \\ \mathit{thresholdStrong} &= 300 \\ \mathit{thresholdWeak} &= 100 \end{align*}


Canny edge detector is a multi-stage algorithm:

  1. Noise reduction
    Since edge detection is easily affected by noise, a Gaussian filter is applied to the image first.
  2. Find the intensity and angle of each pixel in the image
    The smoothed image from stage 1 is then filtered with edge detection filters in both horizontal and vertical direction, resulting in \(G_x\) and \(G_y\). The intensity and angle of each pixel is calculated as follows:

    \begin{align*} Intensity &= \sqrt{G_x^2 + G_y^2} \\ Angle &= arctan(\frac{G_y}{G_x}) \end{align*}

    The gradient direction is always perpendicular to edges. It is rounded to one of four angles representing vertical, horizontal and two diagonal directions.
  3. Thin the edge by non-maximum suppression
    For each pixel in the image, check if the intensity is a local maximum in the gradient direction. If it is, keep it as edge pixel, otherwise remove it as non-edge pixel.
  4. Double thresholding
    Two thresholding values are provided and they are called strong threshold and weak threshold. For each pixel, if the intensity is greater than the strong threshold, then it is marked as strong edge. If the intensity is less than the weak threshold, then it is marked as non-edge. Any intensity value in between of the strong threshold and weak threshold is marked as weak edge.
  5. Track edge by hysteresis
    If the weak edge is connected to a strong edge, then this weak edge is changed into a strong edge. Repeat this process until all the weak edges that are connected to strong edges are found. Mark the rest of weak edges as non-edge.

C API functions

For list of limitations, constraints and backends that implements the algorithm, consult reference documentation of the following functions:

Function Description
vpiCreateCannyEdgeDetector Creates payload for vpiSubmitCannyEdgeDetector.
vpiInitCannyEdgeDetectorParams Initialize vpiInitCannyEdgeDetectorParams with default values.
vpiSubmitCannyEdgeDetector Runs the canny edge detector algorithm over an image.


  1. Import VPI module
    import vpi
  2. Run canny edge detector on input image using the CUDA backend and the given kernel. Input and output are VPI images.
    with vpi.Backend.CUDA:
    output = input.canny(thresh_strong=300, thresh_weak=100, edge_value=255, nonedge_value=0, norm=vpi.Norm.L2)
  1. Initialization phase
    1. Include the header that defines the canny edge detector algorithm functions and parameter structure.
      Declares functions that implement the canny edge detector algorithm.
    2. Define the input image object.
      VPIImage input = /*...*/;
      struct VPIImageImpl * VPIImage
      A handle to an image.
      Definition: Types.h:256
    3. Create the output image object.
      int32_t w, h;
      vpiImageGetSize(input, &w, &h);
      VPIImage output;
      vpiImageCreate(w, h, VPI_IMAGE_FORMAT_U8, 0, &output);
      #define VPI_IMAGE_FORMAT_U8
      Single plane with one 8-bit unsigned integer channel.
      Definition: ImageFormat.h:100
      VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
      Create an empty image instance with the specified flags.
      VPIStatus vpiImageGetSize(VPIImage img, int32_t *width, int32_t *height)
      Get the image dimensions in pixels.
    4. Create the stream where the algorithm will be submitted for execution.
      VPIStream stream;
      vpiStreamCreate(0, &stream);
      struct VPIStreamImpl * VPIStream
      A handle to a stream.
      Definition: Types.h:250
      VPIStatus vpiStreamCreate(uint64_t flags, VPIStream *stream)
      Create a stream instance.
    5. Create a payload for the canny edge detector algorithm that will perform the correction.
      int width, height;
      vpiImageGetSize(input, &width, &height);
      VPIPayload payload;
      vpiCreateCannyEdgeDetector(VPI_BACKEND_CUDA, width, height, &payload);
      VPIStatus vpiCreateCannyEdgeDetector(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIPayload *payload)
      Creates payload for vpiSubmitCannyEdgeDetector.
      struct VPIPayloadImpl * VPIPayload
      A handle to an algorithm payload.
      Definition: Types.h:268
      CUDA backend.
      Definition: Types.h:93
  2. Processing phase
    1. Initialize the parameter structure with user-provided parameters. In this example, the parameters are set to reflect the output above,
      VPIStatus vpiInitCannyEdgeDetectorParams(VPICannyEdgeDetectorParams *params)
      Initialize vpiInitCannyEdgeDetectorParams with default values.
      Structure that defines the parameters for vpiSubmitCannyEdgeDetector.
      Definition: CannyEdges.h:108
    2. Submit the algorithm and its parameters to the stream.
      float thresholdStrong = 300;
      float thresholdWeak = 100;
      float edgeValue = 255;
      float nonEdgeValue = 0;
      VPI_CHECK_STATUS(vpiSubmitCannyEdgeDetector(stream, VPI_BACKEND_CUDA, payload, input, output, thresholdStrong,
      thresholdWeak, edgeValue, nonEdgeValue, &params));
      VPIStatus vpiSubmitCannyEdgeDetector(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage input, VPIImage output, float thresholdStrong, float thresholdWeak, float edgeValue, float nonEdgeValue, const VPICannyEdgeDetectorParams *params)
      Runs the canny edge detector algorithm over an image.
    3. Optionally, wait until the processing is done.
      VPIStatus vpiStreamSync(VPIStream stream)
      Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
  3. Cleanup phase
    1. Free resources held by the stream, the input image and the output image.
      void vpiImageDestroy(VPIImage img)
      Destroy an image instance.
      void vpiStreamDestroy(VPIStream stream)
      Destroy a stream instance and deallocate all HW resources.

For more information, see Canny Edge Detector in the "C API Reference" section of VPI - Vision Programming Interface.


Performance benchmarks will be added at a later time.


  1. https://en.wikipedia.org/wiki/Canny_edge_detector