Sobel Filter#

Filters an image using a Sobel filter.

Description

This function applies a Sobel filter to the source image with the specified kernel size and normalization type. Algorithm parameters are set through the sobel_spec class. The values of border pixels may be determined by the border type. This function can operate with an ROI (see Image Regions of Interest). The formula below describes the algorithm for 3x3 and 5x5 Sobel operators.

3x3 Sobel operator:

\[\begin{split}G_{x} = \begin{bmatrix} 1 & 0 & -1 \\ 2 & 0 & -2 \\ 1 & 0 & -1 \\ \end{bmatrix} * A \quad \text{and} \quad G_{y} = \begin{bmatrix} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \\ \end{bmatrix} * A\end{split}\]

5x5 Sobel operator:

\[\begin{split}G_{x} = \begin{bmatrix} 1 & 2 & 0 & -2 & -1 \\ 4 & 8 & 0 & -8 & -4 \\ 6 & 12 & 0 & -12 & -6 \\ 4 & 8 & 0 & -8 & -4 \\ 1 & 2 & 0 & -2 & -1 \\ \end{bmatrix} * A \quad \text{and} \quad G_{y} = \begin{bmatrix} 1 & 4 & 6 & 4 & 1 \\ 2 & 8 & 12 & 8 & 2 \\ 0 & 0 & 0 & 0 & 0 \\ -2 & -8 & -12 & -8 & -2 \\ -1 & -4 & -6 & -4 & -1 \\ \end{bmatrix} * A\end{split}\]

where

  • A is the source image

  • * is the 2D convolution operator

  • Gx and Gy are horizontal and vertical magnitudes of the source image, respectively

The Sobel filter output, G, is the overall gradient magnitude generated through L1 and L2 normalization of Gx and Gy.

L1 normalization:

\[G = \left\lvert G_{x} \right\rvert + \left\lvert G_{y} \right\rvert\]

L2 normalization:

\[G = \sqrt{ G_{x}^{2} + G_{y}^{2} }\]

Headers

oneapi/ipl/filter/sobel.hpp

Syntax

template<typename ComputeT = float, typename SrcImageT, typename DstImageT>
sycl::event oneapi::ipl::sobel(sycl::queue &queue, SrcImageT &src, DstImageT &dst, const sobel_spec &spec = {}, const typename SrcImageT::pixel_t &border_val = {}, const std::vector<sycl::event> &dependencies = {})#

Sobel filtering function.

Template Parameters:
  • ComputeT – The type in which all calculations will be performed

  • SrcImageT – Source image type

  • DstImageT – Destination image type

Parameters:
  • queue – SYCL queue object

  • src – Source image object

  • dst – Destination image object

  • spec – algorithmic specification

  • border_val – border value

  • dependencies – SYCL event dependencies

Returns:

SYCL event

Supported values for ComputeT:

float

double

half

Supported combinations for data types and layouts:

Data types/Layouts

plane

channel3

channel4

plane3

std::uint8_t

std::int8_t

std::uint16_t

std::int16_t

std::uint32_t

std::int32_t

float

double

Supported values for subsampled layouts (only for DataT = std::uint8_t):

sub420

sub420i

Parameters

border type support

Type of border. Possible values are:

const_val - Values of all border pixels are set to constant.

repl - Border is replicated from the edge pixels.

class sobel_spec#

Sobel filter parameters.

Public Functions

sobel_spec() = default#

Construct default spec.

sobel_spec(border_types border_type)#

Construct spec from border type.

Parameters:

border_type – border type

sobel_spec(crop_types crop)#

Construct spec from crop flag.

Parameters:

crop – crop flag

sobel_spec(border_types border_type, crop_types crop)#

Construct spec from border type and crop flag.

Parameters:
  • border_type – border type

  • crop – crop flag

crop_types get_crop() const noexcept#

Returns the crop type.

Returns:

crop type

border_types get_border() const noexcept#

Returns the border type.

Returns:

border type

sobel_spec(std::size_t radius = 1U, norm_types norm = norm_types::l2, border_types border = border_types::repl, crop_types crop = crop_types::on)#

Construct Sobel spec with given radius, border type, crop flag, normalization type.

Parameters:
  • radius – kernel radius

  • norm – norm to compute distance

  • border – border type

  • crop – crop flag

sobel_spec(const sobel_spec &other)#

Copy constructor for Sobel specification.

Parameters:

other – the other Sobel spec

std::size_t get_radius() const noexcept#

Returns the radius of Sobel filter.

Returns:

the radius of Sobel filter

norm_types get_norm() const noexcept#

Returns the norm of Sobel filter.

Returns:

the norm of Sobel filter

Errors

compile-time layout check

Indicates an error when image layout is not supported (only plane for grayscale format is supported).

compile-time data type check

Indicates an error when the image data type is not supported.

compile-time compute data type check

Indicates an error when the compute data type is not supported.

invalid_argument exception

Indicates an error when the ROI sizes of the source image and the destination image are not equal.

invalid_argument exception

Indicates an error when one of the pitch values is not divisible by the size of the component data type in bytes.

invalid_argument exception

Indicates an error when kernel radius is equal to zero.

unimplemented exception

Indicates an error when border type is not supported.

unimplemented exception

Indicates an error when kernel size is not supported.

The code example below demonstrates how oneapi::ipl::sobel is called:

using namespace oneapi;

sycl::queue                 queue;
ipl::shared_usm_allocator_t allocator{ queue };
const sycl::range<2>        image_size{ height, width };

// Image representing source grayscale image
ipl::image<ipl::layouts::plane, std::uint8_t> src_image{ queue, p_image_data, image_size, allocator };
// Destination image with result of Sobel filtering
ipl::image<ipl::layouts::plane, std::uint8_t> dst_image{ image_size, allocator };

// Apply Sobel filter with 3x3 kernel
auto event = ipl::sobel(queue, src_image, dst_image);