Edge Detectors
TD COMPs / TOPs / 2023 – Present
Edge detection is a cornerstone of image processing, playing a crucial role in identifying boundaries within images. By finding edges, you can extract meaningful information about shapes, objects, and their relationships within the scene. This process is essential for tasks ranging from simple feature extraction to more complex applications like object recognition and computer vision.
Calculating the derivatives of images allows us to delve deeper into the intricacies of image data. By analyzing changes in intensity, edge detectors highlight areas of significant transition, enabling us to identify and isolate key features. This opens up a multitude of possibilities, from enhancing image clarity and detail to facilitating advanced techniques such as texture analysis and motion detection.
Index:
Canny, Gradient, Kirsch Operator, Kirsch Compass, Kirsch Compass Steerable, Laplacian Standard, Laplacian Superior, Line Detector, Line Detector Steerable, Prewitt Standard, Prewitt Steerable, Robert’s Cross Standard, Robert’s Cross Steerable, Sobel Simple, Sobel Standard, Sobel Steerable, Sobel Superior
Canny Edge Detector
GLSL Implementation
The Canny edge detection algorithm was developed to address the need for precise edge detection in images, providing a robust method that balances sensitivity to noise with the ability to accurately identify edges. It does this through a multi-stage process that smooths the image, calculates gradients, and classifies edges while minimizing false positives. The algorithm is ideal for applications requiring clear, well-defined edges, such as object detection, image segmentation, and feature extraction in computer vision.
This component brings the Canny edge detection algorithm into TouchDesigner through GLSL shaders, which is an uncommon approach. Typically, Canny edge detection is implemented using CPU-based methods like OpenCV, but this implementation leverages the GPU for processing. While it isn’t real‐time due to the feedback loop required for hysteresis thresholding, it provides a powerful alternative for edge detection directly within the GPU pipeline. Wai Ching CHUNG has created a more traditional implementation of the Canny Edge Detector in TouchDesigner using OpenCV Python in a Script TOP.
This GLSL implementation of the Canny Edge Detector processes images as follows:
- The input image is converted to luminance, with user-defined control over how this calculation is handled,
- The image is blurred using TouchDesigner's built-in Blur TOP to reduce noise while retaining important structural information,
- The image's Gradient Magnitude and Edge Orientation are calculated using a modified version of the Gradient Component,
- Orientation aware non-maximum suppression is applied to thin out the edges by keeping only the strongest edge points along the gradient direction. This step ensures that suppression is influenced by the orientation of the edges, rather than using a default, direction-agnostic approach.
- A double threshold classifies the remaining edges into strong and weak categories based on user-defined thresholds.
-
Hysteresis Thresholding is performed using a feedback loop, promoting weak edges to strong edges if they are connected to a strong edge.
The feedback loop ensures thorough edge classification, requiring the component to run for multiple frames before the final edge map is fully resolved.
Resources:
Download the .tox files
Canny Edge Detection on Wikipedia
Computerphile’s video on the Canny Edge Detector
Canny Edge Detection on Medium
Original solution code
Parameters
Luminance:
Defines how the luminance of the input image will be calculated.
Type:
Determines the mathematical function used to create the blur.
Extend:
Sets the extend conditions to determine what happens to the blur at the edge of the image.
Pre‐Shrink:
Reduces the image's resolution before applying the blur.
Filter Size:
The amount of blur in pixels. If you want a resolution-independent blur, use an expression like me.par.resolutionw/100 in this parameter (which would result in a 1% image blur).
Operator:
Selects the specific operator used for gradient calculation. Options include:
The Sobel operator, the Prewitt operator, 2x2 Gradient Kernel, or the 1D Gradient Kernel.
Type:
When the Operator parameter is set to Sobel this will allow you to specify which variant of the Sobel operator to convolve the input image with.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
High Threshold:
This parameter defines the upper threshold for the double thresholding process. Pixels with gradient magnitudes above this value are classified as strong edges.
Low Threshold:
This parameter defines the lower threshold for the double thresholding process. Pixels with gradient magnitudes between the low and high thresholds are classified as weak edges.
Alpha:
This parameter determines the output for the alpha channel.
Reset:
Restarts the hysteresis thresholding promotion process. This is useful for recalculating the edge map, ensuring the proper promotion of weak edges after any changes to input parameters.
In‐ / Outputs
Input 0 – TOP image to be processed with the Canny Edge Detector.
Output 0 – TOP the maximum edge magnitude of input 0.
Gradient
Gradient Magnitude & Edge Orientation
This component calculates the Gradient Magnitude ( ) and Edge Orientation ( ) of the input image, providing crucial information for edge detection and image analysis tasks. By determining how pixel intensity changes across an image, the Gradient Magnitude ( ) highlights areas with the most significant variations, while the Edge Orientation ( ) identifies the direction of those changes. These two outputs are essential for processes like edge thinning, feature extraction, and boundary detection.
The Gradient component allows users to choose which edge detection method will be used in the Gradient Magnitude ( ) and Edge Orientation ( ) calculations, from TouchDesigner’s built‐in Slope TOP or any of the following edge detectors:
Sobel and its 3 main variants, Prewitt, 2x2 Gradient Kernel, or the 1D Gradient Kernel.
The Gradient Magnitude ( ) and Edge Orientation ( ) are derived by calculating the horizontal ( ) and vertical ( ) gradients of the input image. These gradients represent the rate of change in pixel intensity in both directions, and together they form a complete picture of how edges are distributed across the image.
The Gradient Magnitude ( ) is computed using the Pythagorean theorem, measuring the overall intensity of the edge at each pixel:
The Edge Orientation ( ) is calculated using the arctangent function, which determines the direction of the edge relative to the horizontal axis:
These formulas provide a robust method for understanding both the strength and direction of edges in an image, critical for edge refinement techniques such as non-maximum suppression and other advanced image processing tasks.
Resources:
Download the .tox files
pyimagesearch description of image gradients
TheAILearner's Understanding Image Gradients
Parameters
Gradient:
This parameter selects the method with which to calculate the gradient of the input image in both the x and y axis.
Slope will use the built in Slope TOP and Custom will provide a list of kernels that can be used to calculate the gradient of the input image.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Method:
Determines what pixels to use when calculating the slope at each pixel in the image.
Zero Point:
Sets the value to output when the slope is zero, similar to a midpoint. Default is .5 since 8-bit pxels are 0 to 1. But with Pixel Format et to 32-bit Float you should set this to 0, and look at the view in Normalized Split mode.
Strength:
Set the strength of the output using this multiplier. Higher values result in higher slope values.
Sample Step:
When sampling the image, this determines the distance from each pixel to the sample pixel. When units are set to pixels, it is the number of pixels away from the current pixel which is sampled to find edges. A Sample Step of 3 would sample pixels 3 pixels away to look for edges.
Operator:
Selects the specific operator used for gradient calculation. Options include:
The Sobel operator, the Prewitt operator, 2x2 Gradient Kernel, or the 1D Gradient Kernel.
Type:
When the Operator parameter is set to Sobel this will allow you to specify which variant of the Sobel operator to convolve the input image with.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize Magnitude:
Normalize values in the calculated Gradient Magnitude image so that all values are scaled and shifted to fall between 0 and 1.
Normalize Orientation:
Normalize values in the calculated Edge Orientation image so that all values are scaled and shifted to fall between 0 and 1. By default, the values in the Edge Orientation image will be between 0 and 180.
In‐ / Outputs
Input 0 – TOP image to be processed.
Output 0 – TOP the calculated gradient magnitude of input 0.
Output 1 – TOP the calculated gradient orientation of input 0.
Kirsch Operator
Standard calculation
The Kirsch Operator is widely used for detecting edges in various image processing tasks due to its comprehensive directional sensitivity. It is particularly effective in highlighting edges in multiple directions.
This component implements the complete method for calculating the Kirsch Operator. It works by convolving the given image with the Kirsch kernel rotated in 45° increments through all 8 compass directions: N, NW, W, SW, S, SE, E, and NE. The final output is then obtained by taking the maximum response from all eight convolutions.
The maximum edge magnitude calculation is:
Resources:
Download the .tox files
Kirsch Operator on wikipedia
Tutorialspoint Kirsch Compass Mask
Edge detectors article on Medium
Parameters
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Invert Kernel:
When on, this will invert the sign of the values in each cell of the kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Kirsch Operator.
Output 0 – TOP the maximum edge magnitude of input 0.
Kirsch Compass
Standard & Steerable
As is mentioned above, for the Kirsch Operator, the Kirsch Operator is widely used for detecting edges in various image processing tasks due to its comprehensive directional sensitivity. It is particularly effective in highlighting edges in multiple directions.
Kirsch Compass:
This variant allows you to select a specific compass direction kernel to convolve the input image with. This allows you to focus on detecting edges in a particular orientation, rather than taking the maximum across all directions.
Kirsch Compass Steerable:
The steerable iteration of the Kirsch Operator allows convolution at any arbitrary angle, providing continuous control over the direction of edge detection within 360°.
Resources:
Download the .tox files
Kirsch Operator on wikipedia
Tutorialspoint Kirsch Compass Mask
Edge detectors article on Medium
Parameters
Orientation:
The standard Kirsch Compass Operator’s orientation is limited to the 8 compass directions.
Where as the Kirsch Steerable Operator allows for any arbitrary angle within 360°.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Invert Kernel:
When on, this will invert the sign of the values in each cell of the kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Kirsch Operator.
Output 0 – TOP the image gradient of input 0
Laplacian Operator
Standard kernel set
Due to its reliance on second derivatives, the Laplacian Operator can be more sensitive to noise. Pre-smoothing the image with a Gaussian filter is often recommended to mitigate this issue and improve performance.
Standard kernels:
Two commonly used discrete approximations to the Laplacian filter:
- von Neuman kernel,
- Moore kernel.
Additional kernels:
These kernels offer variations that can enhance specific image features and provide more flexibility in edge detection and image analysis:
- Diagonal variant of the von Neumann kernel,
- Standard Laplacian of Gaussian kernel,
- Diagonal Laplacian of Gaussian kernel,
- Emphasizes diagonal edges and transitions in the image,
- Provides a stronger central influence with weighted surrounding pixels for enhanced contrast detection.
Extended kernels:
These larger 5x5 kernels offer enhanced edge detection capabilities by considering a broader context within the image:
- Designed for detecting finer details and high-frequency content,
- Provides a broader context for edge detection with uniform influence across the kernel,
- Suitable for detecting edges with less influence from distant pixels, reducing potential noise effects.
Notice all the above kernels are defined using a negative peak, which is more common, but using the opposite sign convention is equally valid. Invert the sign of each cell value with the Invert kernel parameter.
Resources:
Download the .tox files
Laplacian Operator on wikipedia
First Principles of Computer Vision video lecture
Parameters
Kernel:
This menu selects which variant of the Laplacian will be performed on the input image.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Invert Kernel:
When on, this will invert the sign of the values in each cell of the kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be convolved with the Laplacian kernel.
Laplacian Superior
Dynamically generated kernels
The Laplacian Superior component expands on the traditional Laplacian operator by allowing you to specify the size of the kernel, generating a Laplacian kernel of the specified size. This provides a flexible approach to second-derivative edge detection, accommodating various window sizes and smoothing parameters.
The kernel is calculated with this function:
When the parameters are set to generate a kernel of size 9, with a sigma (σ) value of 1.4, and dilation set to 0, the Laplacian function looks like this:
In the above function, (Sigma) determines the spread of the inverted bell curve in the Laplacian kernel which is visualized above. For smaller values of window size () and , you get a large negative number surrounded by 1s. As you increase the window size and , the distribution changes, resulting in a smoother approximation. This adjustment helps in reducing noise and enhancing edge detection.
Resources:
Download the .tox files
Laplacian Operator on wikipedia
First Principles of Computer Vision video lecture
Original solution code
Parameters
Size:
Specify the size of the kernel to be generated. Larger kernels capture broader edges but may lose fine details. Note that the kernel must have a center cell, so the size must be an odd number.
Sigma:
Determines the spread of the inverted bell curve in the Laplacian of Gaussian (LoG) function.
Round to Decimal:
Round the kernel values to a specified number of decimal places for precision control in calculations.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Invert Kernel:
When on, this will invert the sign of the values in each cell of the kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be convolved with the Laplacian kernel.
Line Detector
Standard & Steerable
In image processing, the Line Detector Operator is used to identify linear features within an image. A notable distinction from most edge detectors is that the Line Detector kernels are symmetrical. The symmetry of the kernel means it responds equally to positive and negative changes in intensity along the line's direction. While this makes it effective for detecting lines, it also means that the Line Detector Operator cannot be used to calculate the orientation of edges, as it does not provide directional gradient information.
The 4 standard kernels are defined as the horizontal (), Vertical (), Oblique +45° (), and Oblique -45° ():
In addition to these directions, within the orientation parameter, you can also set the Line Detector to All which will calculate the combined response ( ) from all four directional kernels following this equation:
Resources:
Download the .tox files
Line Detection on wikipedia
HIPR2 documentation on Line Detection
Parameters
Orientation:
The standard Line Detector is limited to the four directions listed on the left, along with a combination of all four.
The Steerable Line Detector allows for any arbitrary angle within 180°.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Invert Kernel:
When on, this will invert the sign of the values in each cell of the kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be passed through the Line Detector.
Output 0 – TOP input 0’s response to the Line Detector convolution.
Prewitt Operator
Standard & Steerable
The Prewitt Operator is a widely used edge detection technique that approximates the gradient of an image intensity function. Out of the discrete differentiation operators the Prewitt Operator is known for its simplicity and computational efficiency, making it a popular choice in various image processing applications.
Examples of the Prewitt kernel are defined as:
As with other edge detectors that utilize both horizontal and vertical kernels, the gradient magnitude ( ) and edge orientation ( ) can easily be calculated by taking the horizontal ( ) and vertical ( ) gradients of the input image. The following formulas, based on the Pythagorean theorem and the arctangent function, can be used:
Resources:
Download the .tox files
Prewitt Operator on wikipedia
DIP article on Medium
Parameters
Orientation:
In the Standard Prewitt Operator, the orientation is limited to the 8 compass directions. The Steerable Prewitt Operator allows for any arbitrary angle within 360°.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Prewitt Operator.
Output 0 – TOP input 0’s response to the Prewitt Operator.
Robert’s Cross
Standard & Steerable
The Robert’s Cross Operator is an early and straightforward edge detection technique used to approximate the gradient of an image. It calculates the differences between diagonally adjacent pixels, making it particularly sensitive to changes in intensity along diagonals.
The operator uses four 2×2 convolution kernels to compute the gradient in the diagonal directions. These kernels are defined as:
By convolving the image with these kernels, the Robert’s Cross Operator computes the gradient magnitude and direction, providing a measure of edge strength and orientation. The gradient magnitude is calculated as:
and the gradient direction is:
The Robert’s Cross Operator is simple and efficient, making it suitable for real‐time applications and situations where computational resources are limited. However, it is sensitive to noise and may not perform well on images with significant high-frequency content or where precise edge detection is required.
Resources:
Download the .tox files
Robert’s Cross Operator on wikipedia
DIP article on Medium
Parameters
Orientation:
In the Standard Robert’s Cross Operator, the orientation is limited to the 4 diagonal directions. The Steerable Robert’s Cross Operator allows for any arbitrary angle within 360°.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Robert’s Cross Operator.
Output 0 – TOP input 0’s response to the Robert’s Cross Operator.
Sobel Operator
Simple, Standard, & Steerable
The Sobel Operator is a versatile tool in image processing used to compute the first derivative of an image. It approximates the gradient of the image intensity function, making it particularly useful for tasks that require gradient magnitude and direction analysis.
Calculating the first derivative of images is crucial for tasks such as edge detection and gradient analysis. It is especially effective when working with Signed Distance Functions (SDFs) for calculating slope orientation, providing detailed information about changes in intensity.
Both Sobel components offer the flexibility to switch between three standard kernels:
Standard kernel (Sobel‐Feldmann kernel):
The classic Sobel‐Feldmann kernel is great for general gradient and edge analysis. It balances noise reduction with edge localization and is suitable for most applications.
Scharr kernel:
This Scharr kernel provides improved rotational symmetry and better performance in high-frequency regions. It is ideal for images with fine details requiring precise gradient approximations.
Scharr Optimized kernel:
This kernel results from Scharr's theory, optimized for minimal angular error (about 0.2°), making it suitable for applications requiring high precision in gradient calculations.
Resources:
Download the .tox files
Sobel Operator on wikipedia
Computerphile’s video on the Sobel Operator
DIP article on Medium
Parameters
Operator:
Specify which of the 3 standard Sobel kernels to convolve input 0 with.
Orientation:
In the Standard Sobel Operator, the orientation is limited to the 8 compass directions. The Steerable Sobel Operator allows for any arbitrary angle within 360°.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Sobel Operator.
Output 0 – TOP the image gradient of input 0, calculated with the Sobel Operator.
Sobel Superior
Dynamically generated kernels
The Superior Sobel Operator expands on the capabilities of the traditional Sobel operator, allowing for a more flexible approach to gradient and edge detection in image processing. Unlike the standard and steerable Sobel operators, the Superior Sobel Operator can handle arbitrary kernel sizes along with angles, making it a powerful tool for advanced image analysis tasks.
For a more in-depth explanation of how this operator generates each kernel, refer to Daniel's response on Stack Overflow in which they lay out the foundational code used in this operator.
An example of a generated kernel, as can be seen in the example operator on the right, is a 5×5 kernel oriented horizontally to the right:
Resources:
Download the .tox files
Sobel Operator on wikipedia
Computerphile’s video on the Sobel Operator
DIP article on Medium
Original solution code
Parameters
Size:
Specify the size of the kernel to be generated. Larger kernels capture broader edges but may lose fine details. Note that the kernel must have a center cell, so the size must be an odd number.
Orientation:
Unlike the Steerable Sobel Operator, this operator will generate a new kernel for any given angle.
Round to Decimal:
Round the kernel values to a specified number of decimal places for precision control in calculations.
Dilation:
Increases the distance between the pixels being sampled by adding null rows and columns to the existing kernel.
Channel:
Specifies which channels of the input image the gradient operation will be calculated on.
Edge Handling:
Determines how the edges of the image are treated during convolution. Extends the image to avoid edge artifacts.
Normalize:
Normalize values in the output image so that they are all scaled and shifted to fall between 0 and 1.
In‐ / Outputs
Input 0 – TOP image to be processed with the Sobel Operator.
Output 0 – TOP the image gradient of input 0, calculated with the Sobel Operator.