Edge Detection in Image Processing
Edge Detection in Image Processing
Edge detection is a fundamental image processing technique for identifying and locating the boundaries or edges of
objects in an image. It is used to identify and detect the discontinuities in the image intensity and extract the outlines of
objects present in an image. The edges of any object in an image (e.g. flower) are typically defined as the regions in an
image where there is a sudden change in intensity. The goal of edge detection is to highlight these regions.
There are various types of edge detection techniques, which include the following:
The goal of edge detection algorithms is to identify the most significant edges within an image or scene. These detected
edges should then be connected to form meaningful lines and boundaries, resulting in a segmented image that contains
two or more distinct regions. The segmented results are subsequently used in various stages of a machine vision
system for tasks such as object counting, measuring, feature extraction, and classification.
Edge Models
Edge models are theoretical constructs used to describe and understand the different types of edges that can occur in
an image. These models help in developing algorithms for edge detection by categorizing the types of intensity changes
that signify edges. The basic edge models are Step, Ramp and Roof. A step edge represents an abrupt change in
intensity, where the image intensity transitions from one value to another in a single step. A ramp edge describes a
gradual transition in intensity over a certain distance, rather than an abrupt change. A roof edge represents a peak or
ridge in the intensity profile, where the intensity increases to a maximum and then decreases.
From left to right, models (ideal representations) of a step, a ramp, and a roof edge, and their corresponding
intensity profiles. (Source: Digital Image Processing by R. C. Gonzalez & R. E. Woods)
The image intensity function represents the brightness or intensity of each pixel in a grayscale image. In a color image,
the intensity function can be extended to include multiple channels (e.g., red, green, blue in RGB images).
1/9
The first derivative of an image measures the rate of change of pixel intensity. It is useful for detecting edges because
edges are locations in the image where the intensity changes rapidly. It detects edges by identifying significant changes
in intensity. The first derivative can be approximated using gradient operators like the Sobel, Prewitt, or Scharr
operators.
The second derivative measures the rate of change of the first derivative. It is useful for detecting edges because zero-
crossings (points where the second derivative changes sign) often correspond to edges. It detects edges by identifying
zero-crossings in the rate of change of intensity. The second derivative can be approximated using the Laplacian
operator.
(a) Two regions of constant intensity separated by an ideal vertical ramp edge. (b) Detailed near the
edge, showing a horizontal intensity profile, together with its first and second derivatives. (Source:
Digital Image Processing by R. C. Gonzalez & R. E. Woods)
Sobel edge detection is a popular technique used in image processing and computer vision for detecting edges in an
image. It is a gradient-based method that uses convolution operations with specific kernels to calculate the gradient
magnitude and direction at each pixel in the image. Here's a detailed explanation of Sobel edge detection.
The Sobel operator uses two 3x3 convolution kernels (filters), one for detecting changes in the x-direction (horizontal
edges) and one for detecting changes in the y-direction (vertical edges). These kernels are used to compute the gradient
of the image intensity at each point, which helps in detecting the edges. Here are the Sobel kernels:
This kernel is used to detect horizontal edges by emphasizing the gradient in the x-direction.
The 𝐺𝑥 kernel emphasizes changes in intensity in the horizontal direction. The positive values (+1 and +2) on the right
side will highlight bright areas, while the negative values (-1 and -2) on the left side will highlight dark areas, effectively
detecting horizontal edges.
This kernel is used to detect vertical edges by emphasizing the gradient in the y-direction.
2/9
The 𝐺𝑦 kernel emphasizes changes in intensity in the vertical direction. Similarly, the positive values (+1 and +2) at the
bottom will highlight bright areas, while the negative values (-1 and -2) at the top will highlight dark areas, effectively
detecting vertical edges.
Let's walk through an example of Sobel edge detection using Python and the OpenCV library. Here’s the Step-by-Step
Example:
1. Load and Display the Image: First, we need to load a sample image and display it to understand what we're
working with.
2. Convert to Grayscale: Convert the image to grayscale as the Sobel operator works on single-channel images.
3. Apply Gaussian Smoothing (Optional): Apply a Gaussian blur to reduce noise and make edge detection more
robust.
4. Apply Sobel Operator: Use the Sobel operator to calculate the gradients in the x and y directions.
5. Calculate Gradient Magnitude: Compute the gradient magnitude from the gradients in the x and y directions. A
threshold is applied to the gradient magnitude image to classify pixels as edges or non-edges. Pixels with gradient
magnitude above the threshold are considered edges.
6. Normalization: The gradient magnitude and individual gradients are normalized to the range 0-255 for better
visualization.
7. Display the Resulting Edge Image: Normalize and display the edge-detected image.
1. Noise Reduction using Gaussian Blurring: The first step in the Canny edge detection algorithm is to smooth the
image using a Gaussian filter. This helps in reducing noise and unwanted details in the image. The Gaussian filter
is applied to the image to convolve it with a Gaussian kernel. The Gaussian kernel (or Gaussian function) is
defined as:
This step helps to remove high-frequency noise, which can cause spurious edge detection.
2. Gradient Calculation:
After noise reduction, the Sobel operator is used to calculate the gradient intensity and direction of the image. This
involves calculating the intensity gradients in the x and y directions (𝐺𝑥 and 𝐺𝑦). The gradient magnitude and direction
are then computed using these gradients.
3/9
Finding Intensity Gradient of the Image
3. Non-Maximum Suppression: To thin out the edges and get rid of spurious responses to edge detection, non-
maximum suppression is applied. This step retains only the local maxima in the gradient direction. The idea is to
traverse the gradient image and suppress any pixel value that is not considered to be an edge, i.e., any pixel that
is not a local maximum along the gradient direction.
Non-maximum Suppression
In the above image, point A is located on the edge in the vertical direction. The gradient direction is perpendicular to the
edge. Points B and C lie along the gradient direction. Therefore, Point A is compared with Points B and C to determine if
it represents a local maximum. If it does, Point A proceeds to the next stage; otherwise, it is suppressed and set to zero.
4. Double Thresholding: After non-maximum suppression, the edge pixels are marked using double thresholding.
This step classifies the edges into strong, weak, and non-edges based on two thresholds: high and low. Strong
edges are those pixels with gradient values above the high threshold, while weak edges are those with gradient
values between the low and high thresholds.
Given the gradient magnitude 𝑀 and two thresholds 𝑇high and 𝑇low, the classification can be mathematically expressed
as:
Strong Edges:
Weak Edges:
Non-Edges:
5. Edge Tracking by Hysteresis: The final step is edge tracking by hysteresis, which involves traversing the image to
determine which weak edges are connected to strong edges. Only the weak edges connected to strong edges are
retained, as they are considered true edges. This step ensures that noise and small variations are ignored,
resulting in cleaner edge detection.
To simplify the process of Canny Edge detection, OpenCV provides cv.Canny() function. Following is the code for Canny
Edge detection.
4/9
Example of Canny Edge Detection
The Laplacian operator is used to detect edges by calculating the second derivative of the image intensity.
Mathematically, the second derivative of an image 𝑓(𝑥, 𝑦) can be represented as:
This can be implemented using convolution with a Laplacian kernel. Common 3x3 kernels for the Laplacian operator
include:
cv.Laplacian() is a function provided by the OpenCV library used for performing Laplacian edge detection on images.
This function applies the Laplacian operator to the input image to compute the second derivative of the image intensity.
Following are the steps for Edge Detection Using Laplacian
1. Convert the Image to Grayscale: Edge detection usually starts with a grayscale image to simplify computations.
2. Apply Gaussian Blur (Optional): Smoothing the image with a Gaussian blur can reduce noise and prevent false
edge detection.
3. Apply the Laplacian Operator: Convolve the image with a Laplacian kernel to calculate the second derivative.
Prewitt edge detection uses two kernels, one for detecting edges in the horizontal direction and the other for the vertical
direction. These kernels are applied to the image using convolution.
5/9
Vertical Prewitt Kernel (Gy):
1. Convert the Image to Grayscale: Prewitt edge detection typically operates on grayscale images. If the input image
is in color, it needs to be converted to a single channel (grayscale) image.
2. Apply the Horizontal and Vertical Prewitt Kernels: Convolve the image with the horizontal Prewitt kernel (Gx) to
detect horizontal edges and with the vertical Prewitt kernel (Gy) to detect vertical edges.
3. Compute Gradient Magnitude: Combine the horizontal and vertical edge maps to compute the gradient magnitude
of the image intensity at each pixel. The gradient magnitude represents the strength of the edge at each pixel.
4. Thresholding (Optional): Apply a threshold to the gradient magnitude image to highlight significant edges and
suppress noise. Thresholding helps in identifying prominent edges while reducing false detections.
Roberts Cross edge detection uses two kernels, one for detecting edges in the horizontal direction and the other for the
vertical direction. These kernels are applied to the image using convolution.
1. Convert the Image to Grayscale: Roberts Cross edge detection typically operates on grayscale images. If the input
image is in color, it needs to be converted to a single channel (grayscale) image.
2. Apply the Horizontal and Vertical Roberts Cross Kernels: Convolve the image with the horizontal Roberts Cross
kernel (Gx) to detect horizontal edges and with the vertical Roberts Cross kernel (Gy) to detect vertical edges.
3. Compute Gradient Magnitude: Combine the horizontal and vertical edge maps to compute the gradient magnitude
of the image intensity at each pixel. The gradient magnitude represents the strength of the edge at each pixel.
4. Thresholding (Optional): Apply a threshold to the gradient magnitude image to highlight significant edges and
suppress noise. Thresholding helps in identifying prominent edges while reducing false detections.
6/9
Example of Roberts Cross Edge Detection
The horizontal gradient kernel (Gx) is designed to approximate the rate of change of intensity in the horizontal direction,
while the vertical gradient kernel (Gy) approximates the rate of change of intensity in the vertical direction. The Scharr
kernels are as follows.
The cv.Scharr() method in OpenCV is a function used to calculate the first-order derivatives of an image using the
Scharr operator. The Scharr operator is a derivative mask that is used to detect edges in an image. It is similar to the
Sobel operator but is optimized to provide better rotational symmetry and more accurate edge detection for specific
applications.
Gradient-based edge detection involves finding the gradient of the image intensity function. The gradient measures the
rate of change of intensity, and edges are typically located where this rate of change is maximized. The primary tools
used in gradient-based edge detection are gradient operators like Sobel, Prewitt, and Scharr.
Gaussian-based edge detection involves smoothing the image with a Gaussian filter to reduce noise and then detecting
edges, often using second-order derivatives like the Laplacian of Gaussian (LoG). This approach helps to find edges by
locating zero-crossings of the second derivative of the image intensity function
7/9
maintenance and safety. Edge detection techniques can be used effectively to identify and analyze cracks - a critical
task in structural health monitoring.
import cv2
import numpy as np
import matplotlib.pyplot as plt
def preprocess_image(image):
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return blurred_image
def detect_edges(image):
# Apply Sobel edge detection
sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
return gradient_magnitude
def main():
# Read the image
image = cv2.imread('concrete-shrinkage-crack.jpg')
if image is None:
print("Error: Image not found.")
return
# Detect edges
edges = detect_edges(preprocessed_image)
# Original Image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Concrete Surface Image')
plt.axis('off')
# Detected Edges
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.title('Detected Cracks (Edges)')
plt.axis('off')
plt.show()
if __name__ == "__main__":
main()
8/9
Detecting Cracks in Concrete Surface
Conclusion
Edge detection is a key stage in many computer vision applications, and it is used to determine the boundaries between
various regions in an image. Edge detection works by looking for variations in intensity, colour, or texture within an image
that match to object boundaries. Edge detection methods include the Canny edge detector, the Sobel operator, the
Laplacian of Gaussian (LoG) operator etc., as detailed above.
Edge detection may be used for a range of tasks in computer vision, including image segmentation, feature extraction,
object detection and recognition, and motion analysis. Edge information, for example, can be used to segment an image
into separate sections, which can then be utilised for additional analysis or processing. Edge information may also be
used to extract elements from an image, such as corners or lines, that can be used for object detection or tracking over
time.
Timothy Malche. (Jun 14, 2024). Edge Detection in Image Processing: An Introduction. Roboflow Blog:
https://blog.roboflow.com/edge-detection/
Edited by
9/9