ImageDev

GradientOperator2d

Provides different algorithms to perform an edge detection based on the first derivative of a two-dimensional image.

Access to parameter description

This command is deprecated, it will be removed in ImageDev 2025.1.
You can use GradientVector2d or GradientMagnitude2d instead.

For an introduction: GradientOperator2d computes a first order derivative in each image axis direction. To minimize the effect of noise, the algorithm can be combined with a smoothing filter for computing the gradient. A smoothing scale parameter determines the smoothing intensity. If its value is large, noise is reduced but edges are less sharp and only the most significant edges are revealed (this behavior is reversed for the Shen and Castan algorithm). It is important to select the right coefficient to lower the noise just enough without defocusing the edges. Finally the algorithm optionally computes the magnitude of the gradient vectors either by selecting its maximum component or its Euclidean norm.

The following algorithms are proposed to extract the edges of an image: Notices: References
[1] R.Deriche. "Using Canny's criteria to derive a recursively implemented optimal edge detector". International Journal of Computer Vision, vol.1, no 2, pp. 167-187, Jun. 1987.
[2] J.Shen, S.Castan. "An optimal linear operator for step edge detection". CVGIP : Graphical Models and Image Processing, vol.54, no 2, pp. 112-133, Mar. 1992.
[3] J.F.Canny. "A computational approach to edge detection." IEEE Transactions on Pattern Analysis and Machine Intelligence, vol.8, No 6, pp. 679-698, Nov. 1986.

See also

Function Syntax

This function returns a GradientOperator2dOutput structure containing outputImageX, outputImageY, outputAmplitudeImage and outputOrientationImage.
// Output structure of the gradientOperator2d function.
struct GradientOperator2dOutput
{
    /// The X-gradient output image.
    std::shared_ptr< iolink::ImageView > outputImageX;
    /// The Y-gradient output image.
    std::shared_ptr< iolink::ImageView > outputImageY;
    /// The gradient amplitude output image.
    std::shared_ptr< iolink::ImageView > outputAmplitudeImage;
    /// The gradient orientation output image.
    std::shared_ptr< iolink::ImageView > outputOrientationImage;
};

// Function prototype
GradientOperator2dOutput gradientOperator2d( std::shared_ptr< iolink::ImageView > inputImage, GradientOperator2d::GradientOperator gradientOperator, GradientOperator2d::GradientMode gradientMode, double smoothingFactor, std::shared_ptr< iolink::ImageView > outputImageX = nullptr, std::shared_ptr< iolink::ImageView > outputImageY = nullptr, std::shared_ptr< iolink::ImageView > outputAmplitudeImage = nullptr, std::shared_ptr< iolink::ImageView > outputOrientationImage = nullptr );
This function returns a tuple containing output_image_x, output_image_y, output_amplitude_image and output_orientation_image.
// Function prototype.
gradient_operator_2d(input_image: idt.ImageType,
                     gradient_operator: GradientOperator2d.GradientOperator = GradientOperator2d.GradientOperator.CANNY_DERICHE,
                     gradient_mode: GradientOperator2d.GradientMode = GradientOperator2d.GradientMode.AMPLITUDE_MAXIMUM,
                     smoothing_factor: float = 60,
                     output_image_x: idt.ImageType = None,
                     output_image_y: idt.ImageType = None,
                     output_amplitude_image: idt.ImageType = None,
                     output_orientation_image: idt.ImageType = None) -> Tuple[idt.ImageType, idt.ImageType, idt.ImageType, idt.ImageType]
This function returns a GradientOperator2dOutput structure containing outputImageX, outputImageY, outputAmplitudeImage and outputOrientationImage.
/// Output structure of the GradientOperator2d function.
public struct GradientOperator2dOutput
{
    /// The X-gradient output image.
    public IOLink.ImageView outputImageX;
    /// The Y-gradient output image.
    public IOLink.ImageView outputImageY;
    /// The gradient amplitude output image.
    public IOLink.ImageView outputAmplitudeImage;
    /// The gradient orientation output image.
    public IOLink.ImageView outputOrientationImage;
};

// Function prototype.
public static GradientOperator2dOutput
GradientOperator2d( IOLink.ImageView inputImage,
                    GradientOperator2d.GradientOperator gradientOperator = ImageDev.GradientOperator2d.GradientOperator.CANNY_DERICHE,
                    GradientOperator2d.GradientMode gradientMode = ImageDev.GradientOperator2d.GradientMode.AMPLITUDE_MAXIMUM,
                    double smoothingFactor = 60,
                    IOLink.ImageView outputImageX = null,
                    IOLink.ImageView outputImageY = null,
                    IOLink.ImageView outputAmplitudeImage = null,
                    IOLink.ImageView outputOrientationImage = null );

Class Syntax

Parameters

Parameter Name Description Type Supported Values Default Value
input
inputImage
The input image. Image Binary, Label, Grayscale or Multispectral nullptr
input
gradientOperator
The gradient operator to apply.
CANNY_DERICHE The gradient is computed using the Canny-Deriche algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
SHEN_CASTAN The gradient is computed using the Shen and Castan algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM.
KERNEL3X3 The gradient is computed using the Sobel 3x3 kernel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
CANNY The gradient is computed using the Canny algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM and AMPLITUDE_EUCLIDEAN.
GAUSSIAN The gradient is computed using the Gaussian algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
SOBEL The gradient is computed using the Sobel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
PREWITT The gradient is computed using the Prewitt algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
Enumeration CANNY_DERICHE
input
gradientMode
The output image to compute.
AMPLITUDE_MAXIMUM This option computes the gradient maximal amplitude. Only the outputAmplitudeImage output is set using this mode.
AMPLITUDE_EUCLIDEAN This option computes the gradient Euclidean amplitude. Only the outputAmplitudeImage output is set using this mode.
AMPLITUDE_AND_ORIENTATION This option computes Euclidean amplitude and orientation in degrees (between -128 and +128 degrees). Both outputAmplitudeImage and outputOrientationImage outputs are set using this mode.
X_AND_Y_GRADIENTS This option computes gradient in X and Y directions. Both outputImageX and outputImageY outputs are set using this mode.
Enumeration AMPLITUDE_MAXIMUM
input
smoothingFactor
The smoothing factor defines the gradient sharpness. It is only used with Canny Deriche, Shen Castan, and Gaussian operators. It has a totally different meaning depending on the selected gradient operator. Its value must be between 0 and 100.
  • Canny-Deriche: it is inversely proportional to Deriche alpha smoothing factor, in pixels. Low values provide sharp gradient. $SmoothingFactor=\frac{(5.3-\alpha)}{5}\times 100$
  • Shen And Castan: it is proportional to Shen Castan p smoothing factor, in pixels. High values provide sharp gradient.
  • Gaussian: the Gaussian kernel standard deviation, in pixels. Low values provide sharp gradient.
  • Prewitt, Sobel, Canny: it is ignored.
  • Float64 ]0, 100] 60
    output
    outputImageX
    The X-gradient output image. Image nullptr
    output
    outputImageY
    The Y-gradient output image. Image nullptr
    output
    outputAmplitudeImage
    The gradient amplitude output image. Image nullptr
    output
    outputOrientationImage
    The gradient orientation output image. Image nullptr
    Parameter Name Description Type Supported Values Default Value
    input
    input_image
    The input image. image Binary, Label, Grayscale or Multispectral None
    input
    gradient_operator
    The gradient operator to apply.
    CANNY_DERICHE The gradient is computed using the Canny-Deriche algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    SHEN_CASTAN The gradient is computed using the Shen and Castan algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM.
    KERNEL3X3 The gradient is computed using the Sobel 3x3 kernel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    CANNY The gradient is computed using the Canny algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM and AMPLITUDE_EUCLIDEAN.
    GAUSSIAN The gradient is computed using the Gaussian algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    SOBEL The gradient is computed using the Sobel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    PREWITT The gradient is computed using the Prewitt algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    enumeration CANNY_DERICHE
    input
    gradient_mode
    The output image to compute.
    AMPLITUDE_MAXIMUM This option computes the gradient maximal amplitude. Only the outputAmplitudeImage output is set using this mode.
    AMPLITUDE_EUCLIDEAN This option computes the gradient Euclidean amplitude. Only the outputAmplitudeImage output is set using this mode.
    AMPLITUDE_AND_ORIENTATION This option computes Euclidean amplitude and orientation in degrees (between -128 and +128 degrees). Both outputAmplitudeImage and outputOrientationImage outputs are set using this mode.
    X_AND_Y_GRADIENTS This option computes gradient in X and Y directions. Both outputImageX and outputImageY outputs are set using this mode.
    enumeration AMPLITUDE_MAXIMUM
    input
    smoothing_factor
    The smoothing factor defines the gradient sharpness. It is only used with Canny Deriche, Shen Castan, and Gaussian operators. It has a totally different meaning depending on the selected gradient operator. Its value must be between 0 and 100.
  • Canny-Deriche: it is inversely proportional to Deriche alpha smoothing factor, in pixels. Low values provide sharp gradient. $SmoothingFactor=\frac{(5.3-\alpha)}{5}\times 100$
  • Shen And Castan: it is proportional to Shen Castan p smoothing factor, in pixels. High values provide sharp gradient.
  • Gaussian: the Gaussian kernel standard deviation, in pixels. Low values provide sharp gradient.
  • Prewitt, Sobel, Canny: it is ignored.
  • float64 ]0, 100] 60
    output
    output_image_x
    The X-gradient output image. image None
    output
    output_image_y
    The Y-gradient output image. image None
    output
    output_amplitude_image
    The gradient amplitude output image. image None
    output
    output_orientation_image
    The gradient orientation output image. image None
    Parameter Name Description Type Supported Values Default Value
    input
    inputImage
    The input image. Image Binary, Label, Grayscale or Multispectral null
    input
    gradientOperator
    The gradient operator to apply.
    CANNY_DERICHE The gradient is computed using the Canny-Deriche algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    SHEN_CASTAN The gradient is computed using the Shen and Castan algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM.
    KERNEL3X3 The gradient is computed using the Sobel 3x3 kernel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    CANNY The gradient is computed using the Canny algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM and AMPLITUDE_EUCLIDEAN.
    GAUSSIAN The gradient is computed using the Gaussian algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    SOBEL The gradient is computed using the Sobel algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    PREWITT The gradient is computed using the Prewitt algorithm. Supported gradient output modes : AMPLITUDE_MAXIMUM, AMPLITUDE_EUCLIDEAN, AMPLITUDE_AND_ORIENTATION and X_AND_Y_GRADIENTS.
    Enumeration CANNY_DERICHE
    input
    gradientMode
    The output image to compute.
    AMPLITUDE_MAXIMUM This option computes the gradient maximal amplitude. Only the outputAmplitudeImage output is set using this mode.
    AMPLITUDE_EUCLIDEAN This option computes the gradient Euclidean amplitude. Only the outputAmplitudeImage output is set using this mode.
    AMPLITUDE_AND_ORIENTATION This option computes Euclidean amplitude and orientation in degrees (between -128 and +128 degrees). Both outputAmplitudeImage and outputOrientationImage outputs are set using this mode.
    X_AND_Y_GRADIENTS This option computes gradient in X and Y directions. Both outputImageX and outputImageY outputs are set using this mode.
    Enumeration AMPLITUDE_MAXIMUM
    input
    smoothingFactor
    The smoothing factor defines the gradient sharpness. It is only used with Canny Deriche, Shen Castan, and Gaussian operators. It has a totally different meaning depending on the selected gradient operator. Its value must be between 0 and 100.
  • Canny-Deriche: it is inversely proportional to Deriche alpha smoothing factor, in pixels. Low values provide sharp gradient. $SmoothingFactor=\frac{(5.3-\alpha)}{5}\times 100$
  • Shen And Castan: it is proportional to Shen Castan p smoothing factor, in pixels. High values provide sharp gradient.
  • Gaussian: the Gaussian kernel standard deviation, in pixels. Low values provide sharp gradient.
  • Prewitt, Sobel, Canny: it is ignored.
  • Float64 ]0, 100] 60
    output
    outputImageX
    The X-gradient output image. Image null
    output
    outputImageY
    The Y-gradient output image. Image null
    output
    outputAmplitudeImage
    The gradient amplitude output image. Image null
    output
    outputOrientationImage
    The gradient orientation output image. Image null

    Object Examples

    auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );
    
    GradientOperator2d gradientOperator2dAlgo;
    gradientOperator2dAlgo.setInputImage( polystyrene );
    gradientOperator2dAlgo.setGradientOperator( GradientOperator2d::GradientOperator::CANNY_DERICHE );
    gradientOperator2dAlgo.setGradientMode( GradientOperator2d::GradientMode::AMPLITUDE_MAXIMUM );
    gradientOperator2dAlgo.setSmoothingFactor( 60.0 );
    gradientOperator2dAlgo.execute();
    
    std::cout << "outputImageX:" << gradientOperator2dAlgo.outputImageX()->toString();
    std::cout << "outputImageY:" << gradientOperator2dAlgo.outputImageY()->toString();
    std::cout << "outputAmplitudeImage:" << gradientOperator2dAlgo.outputAmplitudeImage()->toString();
    std::cout << "outputOrientationImage:" << gradientOperator2dAlgo.outputOrientationImage()->toString();
    
    polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))
    
    gradient_operator_2d_algo = imagedev.GradientOperator2d()
    gradient_operator_2d_algo.input_image = polystyrene
    gradient_operator_2d_algo.gradient_operator = imagedev.GradientOperator2d.CANNY_DERICHE
    gradient_operator_2d_algo.gradient_mode = imagedev.GradientOperator2d.AMPLITUDE_MAXIMUM
    gradient_operator_2d_algo.smoothing_factor = 60.0
    gradient_operator_2d_algo.execute()
    
    print("output_image_x:", str(gradient_operator_2d_algo.output_image_x))
    print("output_image_y:", str(gradient_operator_2d_algo.output_image_y))
    print("output_amplitude_image:", str(gradient_operator_2d_algo.output_amplitude_image))
    print("output_orientation_image:", str(gradient_operator_2d_algo.output_orientation_image))
    
    ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );
    
    GradientOperator2d gradientOperator2dAlgo = new GradientOperator2d
    {
        inputImage = polystyrene,
        gradientOperator = GradientOperator2d.GradientOperator.CANNY_DERICHE,
        gradientMode = GradientOperator2d.GradientMode.AMPLITUDE_MAXIMUM,
        smoothingFactor = 60.0
    };
    gradientOperator2dAlgo.Execute();
    
    Console.WriteLine( "outputImageX:" + gradientOperator2dAlgo.outputImageX.ToString() );
    Console.WriteLine( "outputImageY:" + gradientOperator2dAlgo.outputImageY.ToString() );
    Console.WriteLine( "outputAmplitudeImage:" + gradientOperator2dAlgo.outputAmplitudeImage.ToString() );
    Console.WriteLine( "outputOrientationImage:" + gradientOperator2dAlgo.outputOrientationImage.ToString() );
    

    Function Examples

    auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" );
    
    auto result = gradientOperator2d( polystyrene, GradientOperator2d::GradientOperator::CANNY_DERICHE, GradientOperator2d::GradientMode::AMPLITUDE_MAXIMUM, 60.0 );
    
    std::cout << "outputImageX:" << result.outputImageX->toString();
    std::cout << "outputImageY:" << result.outputImageY->toString();
    std::cout << "outputAmplitudeImage:" << result.outputAmplitudeImage->toString();
    std::cout << "outputOrientationImage:" << result.outputOrientationImage->toString();
    
    polystyrene = ioformat.read_image(imagedev_data.get_image_path("polystyrene.tif"))
    
    result_output_image_x, result_output_image_y, result_output_amplitude_image, result_output_orientation_image = imagedev.gradient_operator_2d(polystyrene, imagedev.GradientOperator2d.CANNY_DERICHE, imagedev.GradientOperator2d.AMPLITUDE_MAXIMUM, 60.0)
    
    print("output_image_x:", str(result_output_image_x))
    print("output_image_y:", str(result_output_image_y))
    print("output_amplitude_image:", str(result_output_amplitude_image))
    print("output_orientation_image:", str(result_output_orientation_image))
    
    ImageView polystyrene = ViewIO.ReadImage( @"Data/images/polystyrene.tif" );
    
    Processing.GradientOperator2dOutput result = Processing.GradientOperator2d( polystyrene, GradientOperator2d.GradientOperator.CANNY_DERICHE, GradientOperator2d.GradientMode.AMPLITUDE_MAXIMUM, 60.0 );
    
    Console.WriteLine( "outputImageX:" + result.outputImageX.ToString() );
    Console.WriteLine( "outputImageY:" + result.outputImageY.ToString() );
    Console.WriteLine( "outputAmplitudeImage:" + result.outputAmplitudeImage.ToString() );
    Console.WriteLine( "outputOrientationImage:" + result.outputOrientationImage.ToString() );