Edge Detection
Edge Detection
Blog
Timothy Malche
Published Jun 14, 2024 • 18 min read
https://blog.roboflow.com/edge-detection/ 1/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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.
https://blog.roboflow.com/edge-detection/ 2/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
Woods)
A sharp variation of the intensity function across a portion of 2D grayscale image (Source: Digital
Image Processing by R. C. Gonzalez & R. E. Woods)
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.
https://blog.roboflow.com/edge-detection/ 3/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
(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)
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:
https://blog.roboflow.com/edge-detection/ 4/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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.
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.
https://blog.roboflow.com/edge-detection/ 5/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Sobel operators
Gx = cv2.Sobel(blurred_image, cv2.CV_64F, 1, 0, ksize=3)
Gy = cv2.Sobel(blurred_image, cv2.CV_64F, 0, 1, ksize=3)
# Gradient magnitude
G = np.sqrt(Gx**2 + Gy**2)
# Original image
plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
# Gradient in X direction
https://blog.roboflow.com/edge-detection/ 6/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
plt.subplot(2, 2, 2)
plt.imshow(Gx, cmap='gray')
plt.title('Gradient in X direction')
plt.axis('off')
# Gradient in Y direction
plt.subplot(2, 2, 3)
plt.imshow(Gy, cmap='gray')
plt.title('Gradient in Y direction')
plt.axis('off')
# Edge-detected image
plt.subplot(2, 2, 4)
plt.imshow(G, cmap='gray')
plt.title('Sobel Edge Detection')
plt.axis('off')
plt.show()
Here, in the following code for sobel operator cv2.CV_64F specifies the desired depth
of the output image. Using a higher depth helps in capturing precise gradient values,
especially when dealing with small or fine details. For 𝐺𝑥 the values (1, 0) means taking
the first derivative in the x-direction and zero derivative in the y-direction. For 𝐺𝑦 the
values (0, 1) means taking the first derivative in the y-direction and zero derivative in
the x-direction. ksize=3 specifies the size of the extended 3x3 Sobel kernel.
# Sobel operators
Gx = cv2.Sobel(blurred_image, cv2.CV_64F, 1, 0, ksize=3)
Gy = cv2.Sobel(blurred_image, cv2.CV_64F, 0, 1, ksize=3)
https://blog.roboflow.com/edge-detection/ 7/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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:
https://blog.roboflow.com/edge-detection/ 8/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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. 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.
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:
https://blog.roboflow.com/edge-detection/ 9/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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.
import cv2
import numpy as np
import matplotlib.pyplot as plt
https://blog.roboflow.com/edge-detection/ 10/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
# Original image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
# Edge-detected image
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.title('Canny Edge Detection')
plt.axis('off')
plt.show()
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:
https://blog.roboflow.com/edge-detection/ 11/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
This can be implemented using convolution with a Laplacian kernel. Common 3x3
kernels for the Laplacian operator include:
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.
import cv2
import numpy as np
# Original image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
https://blog.roboflow.com/edge-detection/ 12/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
plt.title('Original Image')
plt.axis('off')
plt.imshow(laplacian_abs, cmap='gray')
plt.title('Laplacian Edge Detection')
plt.axis('off')
plt.show()
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.
https://blog.roboflow.com/edge-detection/ 13/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
import cv2
import numpy as np
import matplotlib.pyplot as plt
def prewitt_edge_detection(image):
[-1, 0, 1],
[-1, 0, 1]])
[0, 0, 0],
[1, 1, 1]])
vertical_edges = np.float32(vertical_edges)
https://blog.roboflow.com/edge-detection/ 14/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
threshold = 50
return edges
plt.figure(figsize=(10, 5))
# Original Image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
# Detected Edges
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.show()
https://blog.roboflow.com/edge-detection/ 15/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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.
import cv2
import numpy as np
https://blog.roboflow.com/edge-detection/ 16/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
def roberts_cross_edge_detection(image):
vertical_edges = np.float32(vertical_edges)
return edges
image = cv2.imread('flower.jpg')
plt.figure(figsize=(10, 5))
# Original Image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
# Detected Edges
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.title('Roberts Cross Edge Detection with Thresholding')
https://blog.roboflow.com/edge-detection/ 17/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
plt.axis('off')
plt.show()
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.
https://blog.roboflow.com/edge-detection/ 18/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
import cv2
import numpy as np
import matplotlib.pyplot as plt
def scharr_edge_detection(image):
# Convert to grayscale
Gy = cv2.Scharr(gray_image, cv2.CV_64F, 0, 1)
return gradient_magnitude
def main():
# Load the image
image = cv2.imread('flower.jpg')
if image is None:
edges = scharr_edge_detection(image)
plt.figure(figsize=(15, 5))
# Original Image
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
https://blog.roboflow.com/edge-detection/ 19/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
plt.axis('off')
plt.show()
if __name__ == "__main__":
main()
https://blog.roboflow.com/edge-detection/ 20/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
import cv2
import numpy as np
def preprocess_image(image):
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return blurred_image
def detect_edges(image):
return gradient_magnitude
def main():
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)
https://blog.roboflow.com/edge-detection/ 21/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# 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()
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
https://blog.roboflow.com/edge-detection/ 22/24
11/25/24, 10:02 AM Edge Detection in Image Processing: An Introduction
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/
Written by
https://blog.roboflow.com/edge-detection/ 24/24