VPI - Vision Programming Interface

3.0 Release

Dilate

Overview

The dilate algorithm performs a two dimensional (2D) filter operation on the input image with the provided 2D boolean kernel. The boolean kernel defines the pixel neighborhood for the filtering operation. The filter is a morphological operation called dilate that is applied to each pixel of the input image masked by the boolean filter. The dilate filter is equivalent to the maximum operation. The dilate operation in a binary image increases or grows the foreground regions (in this case white in the figure below) and identifies and fills holes in objects.

Input Kernel Output

\[ \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \]

\( \circ \; max \)

C API functions

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

Function Description
vpiSubmitDilate Runs a 2D dilate over an image.

Implementation

Discrete 2D morphological filter is implemented using the following discrete function:

\[ I'[x,y] = \underset{m \in [0 .. k_h-1] \\ n \in [0 .. k_w-1]}{max} \Big\{ K[m,n] \times I[x-(n-\lfloor k_w/2 \rfloor), y-(m-\lfloor k_h/2 \rfloor)] \Big\} \]

Where:

  • \(I\) is the input image.
  • \(I'\) is the result image.
  • \(K\) is the neighborhood kernel, where \(K \in \{0 \vee 1\}\).
  • \(k_w, k_h\) are the kernel's width and height, respectively.
  • \(max\) is the morphological filter operation.

A morphological operation is a set of image processing operations that process digital images based on their shapes. The dilate filter is equivalent to the maximum operation. Dilation extends bright structures in an image. The input may be binary, for the most-common binary morphology, or gray-scale, for the gray morphology. The other morphological filter is the Erode, that is equivalent to the minimum operation. The erode filter can be used after the dilate filter to compose a morphological closing of an image.

Usage

Language:
  1. Import VPI module
    import vpi
  2. Define a 3x3 morphological kernel to perform full neighborhood search.
    kernel = [[1, 1, 1],
    [1, 1, 1],
    [1, 1, 1]]
  3. Run morphological filter on input image using the CPU backend and the given kernel. Input and output are VPI images.
    with vpi.Backend.CUDA:
    output = input.dilate(kernel, border=vpi.Border.ZERO)
  1. Initialization phase
    1. Include the header that defines the needed functions and structures.
      Declares functions to perform image morphological filtering with binary kernels.
    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. It gets its dimensions and format from the input image.
      int32_t w, h;
      vpiImageGetSize(input, &w, &h);
      vpiImageGetFormat(input, &format);
      VPIImage output;
      vpiImageCreate(w, h, format, 0, &output);
      uint64_t VPIImageFormat
      Pre-defined image formats.
      Definition: ImageFormat.h:94
      VPIStatus vpiImageGetFormat(VPIImage img, VPIImageFormat *format)
      Get the image format.
      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.
  2. Processing phase
    1. Define the kernel to be used. In this case, a simple 3x3 full neighborhood.
      int8_t kernel[3 * 3] = { 1, 1, 1,
      1, 1, 1,
      1, 1, 1 };
    2. Submit the algorithm for the chosen morphological filter to the stream, passing the kernel and other parameters. In this case, the dilate filter. It'll be executed by the CPU backend.
      vpiSubmitDilate(stream, VPI_BACKEND_CPU, input, output, kernel, 3, 3, VPI_BORDER_ZERO);
      VPIStatus vpiSubmitDilate(VPIStream stream, uint64_t backend, VPIImage input, VPIImage output, const int8_t *kernelData, int32_t kernelWidth, int32_t kernelHeight, VPIBorderExtension border)
      Runs a 2D dilate over an image.
      @ VPI_BACKEND_CPU
      CPU backend.
      Definition: Types.h:92
      @ VPI_BORDER_ZERO
      All pixels outside the image are considered to be zero.
      Definition: Types.h:278
    3. Optionally, wait until the processing is done.
      vpiStreamSync(stream);
      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 and the input and output images.
      vpiImageDestroy(output);
      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 Dilate in the "C API Reference" section of VPI - Vision Programming Interface.

Performance

For information on how to use the performance table below, see Algorithm Performance Tables.
Before comparing measurements, consult Comparing Algorithm Elapsed Times.
For further information on how performance was benchmarked, see Performance Benchmark.

 -