Edge Detection: 1) Noise Reduction 2) Edge Enhancement 3) Edge Localisation
Edge Detection: 1) Noise Reduction 2) Edge Enhancement 3) Edge Localisation
The gradient method detects the edges by looking for the maximum
and minimum in the first derivative of the image.
The Laplacian method searches for zero crossings in the second
derivative of the image .
We will look at two examples of the gradient method, Sobel and
Prewitt.
40
Edge Detection
Edge detection is a major application for convolution.
What is an edge:
• A location in the image where is a sudden change in the
intensity/colour of pixels.
• A transition between objects or object and background.
• From a human visual perception perspective it attracts attention.
Problem: Images contain noise, which also generates sudden
transitions of pixel values.
Usually there are three steps in the edge detection process:
1) Noise reduction
Suppress as much noise as possible without removing edges.
2) Edge enhancement
Highlight edges and weaken elsewhere (high pass filter).
3) Edge localisation
Look at possible edges (maxima of output from previous filter) and
eliminate spurious edges (often noise related).
41
Edge Detection
Gradient Estimation
Estimation of the intensity gradient at a pixel in the x and y
direction, for an image f, is given by:
∂f
= f ( x + 1, y ) − f ( x − 1, y )
∂x
∂f
= f ( x, y + 1) − f ( x, y − 1)
∂y
We can introduce noise smoothing by convoluting with a low pass filter
(e.g. mean, Gaussian, etc)
The gradient calculation (gx,gy) can be expressed as:
g x = hx ∗ f ( x, y )
g y = hy ∗ f ( x , y )
42
Sobel Filter
The Sobel filter is used for edge detection.
It works by calculating the gradient of image intensity at each pixel
within the image. It finds the direction of the largest increase from
light to dark and the rate of change in that direction.
The result shows how abruptly or smoothly the image changes at each
pixel, and therefore how likely it is that that pixel represents an edge.
It also shows how that edge is likely to be oriented.
The result of applying the filter to a pixel in a region of constant
intensity is a zero vector.
The result of applying it to a pixel on an edge is a vector that points
across the edge from darker to brighter values.
43
Sobel Filter
The sobel filter uses two 3 x 3 kernels. One for changes in the
horizontal direction, and one for changes in the vertical direction.
The two kernels are convolved with the original image to calculate the
approximations of the derivatives.
If we define Gx and Gy as two images that contain the horizontal and
vertical derivative approximations respectively, the computations
are:
1 0 −1 −1 −2 −1
G x = 2 0 −2 ∗ A and G y = 0 0 0 ∗ A
1 0 −1 1 2 1
Where A is the original source image.
44
Sobel Filter
To compute Gx and Gy we move the appropriate kernel (window) over
the input image, computing the value for one pixel and then shifting
one pixel to the right. Once the end of the row is reached, we move
down to the beginning of the next row.
The example below shows the calculation of a value of Gx:
a11 a12 a13 b11 b12 b13
… …
1 0 -1
a21 a22 a23 b21 b22 b23
… …
kernel = 2 0 -2
b31 b32 b33
a31 a32 a33
… 1 0 -1 …
… … … … … … … …
This means the output image will contain positive and negative values.
The measure of an edge is its amplitude and angle. These are readily
calculated from Gx and Gy.
46
Sobel Filter
At each pixel in the image, the gradient approximations given by Gx and
Gy are combined to give the gradient magnitude, using:
G = Gx2 + G y2
The gradient’s direction is calculated using:
Gy
Θ = arctan
Gx
A Θ value of 0 would indicate a vertical edge that is darker on the left
side.
47
Sobel Filter
1 0 −1
The image to the right above is Gx, calculated as: Gx = 2 0 −2 ∗ A
Where A is the original image to the left. 1 0 −1
Notice the general orientation of the edges.
What would you expect to be different in Gy?
48
Sobel Filter
−1 −2 −1
The image to the right above is Gy, calculated as: Gy = 0 0 0 ∗ A
1 2 1
Where A is the original image to the left.
What do we expect from the combined image?
49
Sobel Filter
The image to the right above is the result of combining the Gx and Gy
derivative approximations calculated from image A on the left.
50
1 0 -1
10 50 10 50 10
2 0 -2
10 55 10 55 10 1 0 -1
10 65 10 65 10
10 50 10 50 10
10 55 10 55 10 -1 -2 -1
0 0 0
Original image 1 2 1
51
Sobel filter example
• Compute Gx and Gy, gradients of the image performing the
convolution of Sobel kernels with the image
• Use zero-padding to extend the image 0 30 30 0 -30
0 40 40 0 -40
0 0 10 10 10 0 40 40 0 -40
Gx
0 0 10 10 10 0 40 40 0 -40
0 30 30 0 -30
0 0 10 10 10
-10 -30 -40 -30 -10
0 0 10 10 10 0 0 0 0 0
y 0 0 10 10 10 Gy 0 0 0 0 0
0 0 0 0 0
x
10 30 40 30 10
1 0 -1 -1 -2 -1
2 0 -2 0 0 0
1 0 -1 1 2 1
52
hx hy
1 0 -1 -1 -2 -1 0 0
2 0 -2 0 0 0 0 0
Gy
1 0 -1 1 2 1 Θ = arctan 0 0
Gx 0 0 53
hx hy
0 0
Sobel filter example
• Compute Gx and Gy, gradients of the image performing the
convolution of Sobel kernels with the image
• Use border values to extend the image 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 Gx 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
10 10 10 10 10
0 0 0 0 0
10 10 10 10 10 -40 -40 -40 -40 -40
y Gy -40 -40 -40 -40 -40
10 10 10 10 10
0 0 0 0 0
x 0 0 0 0 0
1 0 -1 -1 -2 -1
2 0 -2 0 0 0
Gy
1 0 -1 1 2 1 Θ = arctan
Gx 54
hx hy