Image_Processing_LECTURE_6_UPDATED
Image_Processing_LECTURE_6_UPDATED
LECTURE 6
Edge Detection
EDGES
Edges correspond to discontinuities in intensity or color.
Origin of edges
Edge models
Edges correspond to pixels where intensity discontinuities occur.
A roof edge
A ramp edge
Edge descriptors
• Edge normal: unit vector in the direction of maximum intensity change.
• Edge direction: unit vector along edge (perpendicular to edge normal).
• Edge position or center: the image position at which the edge is located.
• Edge strength or magnitude: local image contrast along the normal
Image Gradient
Image gradient:
𝑔𝑦
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛−1
𝑔𝑥
Edge detection using image gradient
Image gradient:
𝑔𝑦
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛−1
𝑔𝑥
Edge detection using image gradient
Apply the
above Sobel
mask
to obtain 𝑔𝑥
Gradient component along the y-direction
(horizontal direction)
Apply the
above Sobel
mask
to obtain 𝑔𝑦
Various masks to compute the gradient:
Ix Iy
I(x,y)
Edge detection using gradients
Apply a threshold
to the gradient
magnitude
Davide Scaramuzza
https://rpg.ifi.uzh.ch
Derivatives and noise
The image derivative operations are very sensitive to noise.
They amplify noise and unwanted small details.
5 × 5 kernel
approximation
Marr-Hildreth edge-detection
Marr-Hildreth edge-detection
Canny Edge Detection
STEPS:
1) Smooth the input image with a Gaussian filter
2) Compute the gradient magnitude and angle images
3) Apply nonmaxima suppression to the gradient magnitude image
4) Use double thresholding
5) Connectivity analysis to detect and link edges
Canny Edge Detection
STEP 1) Smooth the input image with a Gaussian filter
Gradient magnitude:
Gradient direction:
Canny Edge Detection
STEP 3) Nonmaxima suppression for thin edges
Use the gradient magnitude 𝑀(𝑥, 𝑦) and gradient
direction α(𝑥, 𝑦) to create the «nonmaxima-
suppressed gradient magnitude» 𝑔𝑁 (𝑥, 𝑦).
Canny Edge Detection
STEP 3) Nonmaxima suppression
Use the gradient magnitude 𝑀(𝑥, 𝑦) and gradient direction α(𝑥, 𝑦) to create the
«nonmaxima-suppressed gradient magnitude» 𝑔𝑁 (𝑥, 𝑦).
𝑑1
If gradient direction α(𝑥, 𝑦) is in region 𝑑1 :
𝑑2 𝑑4
Horizontal edge
𝑑3
𝑑3
𝑑4 𝑑2
𝑑1
Gradient direction
α(𝑥, 𝑦)
Canny Edge Detection
STEP 3) Nonmaxima suppression
Use the gradient magnitude 𝑀(𝑥, 𝑦) and gradient direction α(𝑥, 𝑦) to create the
«nonmaxima-suppressed gradient magnitude» 𝑔𝑁 (𝑥, 𝑦).
𝑑1
• If gradient direction α(𝑥, 𝑦) is in region 𝑑1 :
𝑑2 𝑑4
Horizontal edge
Examine the pixels 𝑀(𝑥 + 1, 𝑦) and 𝑀(𝑥 − 1, 𝑦).
If one of them is greater than 𝑀(𝑥, 𝑦) set 𝑔𝑁 𝑥, 𝑦 to 0. 𝑑3
Otherwise, let 𝑔𝑁 𝑥, 𝑦 = 𝑀(𝑥, 𝑦). 𝑑3
𝑑4 𝑑2
• If gradient direction α(𝑥, 𝑦) is in region 𝑑2 :
45° edge 𝑑1
Examine the pixels 𝑀(𝑥 + 1, 𝑦 + 1) and 𝑀(𝑥 − 1, 𝑦 − 1). Gradient direction
If one of them is greater than 𝑀(𝑥, 𝑦) set 𝑔𝑁 𝑥, 𝑦 to 0. α(𝑥, 𝑦)
Otherwise, let 𝑔𝑁 𝑥, 𝑦 = 𝑀(𝑥, 𝑦).
Canny Edge Detection
STEP 3) Nonmaxima suppression
Use the gradient magnitude 𝑀(𝑥, 𝑦) and gradient direction α(𝑥, 𝑦) to create the
«nonmaxima-suppressed gradient magnitude» 𝑔𝑁 (𝑥, 𝑦).
𝑑1
• If gradient direction α(𝑥, 𝑦) is in region 𝑑3 :
𝑑2 𝑑4
Vertical edge
Examine the pixels 𝑀(𝑥, 𝑦 + 1) and 𝑀(𝑥, 𝑦 − 1).
If one of them is greater than 𝑀(𝑥, 𝑦) set 𝑔𝑁 𝑥, 𝑦 to 0. 𝑑3
Otherwise, let 𝑔𝑁 𝑥, 𝑦 = 𝑀(𝑥, 𝑦). 𝑑3
𝑑4 𝑑2
• If gradient direction α(𝑥, 𝑦) is in region 𝑑4 :
-45° edge 𝑑1
Examine the pixels 𝑀(𝑥 + 1, 𝑦 − 1) and 𝑀(𝑥 − 1, 𝑦 + 1). Gradient direction
If one of them is greater than 𝑀(𝑥, 𝑦) set 𝑔𝑁 𝑥, 𝑦 to 0. α(𝑥, 𝑦)
Otherwise, let 𝑔𝑁 𝑥, 𝑦 = 𝑀(𝑥, 𝑦).
Canny Edge Detection
STEP 3) Nonmaxima suppression
Use the gradient magnitude 𝑀(𝑥, 𝑦) and gradient direction α(𝑥, 𝑦) to create the
«nonmaxima-suppressed gradient magnitude» 𝑔𝑁 (𝑥, 𝑦).
sobel_mask_y = [-1 0 1;
-2 0 2; Ix
-1 0 1]
Iy
% Gradient magnitude
M = sqrt(Ix.^2 + Iy.^2);
figure(4); imshow(M,[0 max(M(:))]);
% Gradient direction
D = atan2(Ix,Iy);
figure(5); imshow(D,[-pi,pi]);
Gradient Gradient
magnitude direction
% Histogram of the gradient magnitude
figure(6);
hist(M(:),100);
Thr = 300;
E = M > Thr;
figure(8);
imshow(E);
Thr = 500;
E = M > Thr;
figure(9);
imshow(E); Thr = 500
Thr = 100 Thr = 300
% MATLAB's edge function
I = imread('cameraman.tif');
figure(1); imshow(I);
[E_sobel,thr] = edge(I,'sobel');
figure(2); imshow(E_sobel);
disp(thr)
Sobel
[E_prewitt,thr] = edge(I,'prewitt');
figure(3); imshow(E_prewitt);
disp(thr)
[E_canny,thr] = edge(I,'canny');
figure(4); imshow(E_canny);
disp(thr)
Prewitt Canny
% MATLAB's edge function
I = imread('clown.bmp');
figure(1); imshow(I);
[E_sobel,thr] = edge(I,'sobel');
figure(2); imshow(E_sobel);
disp(thr)
Sobel
[E_prewitt,thr] = edge(I,'prewitt');
figure(3); imshow(E_prewitt);
disp(thr)
[E_canny,thr] = edge(I,'canny');
figure(4); imshow(E_canny);
disp(thr)
Prewitt Canny
% Adjust threshold for Sobel
I = imread('clown.bmp');
figure(1); imshow(I);
E_sobel = edge(I,'sobel',0.02);
figure(2); imshow(E_sobel);