0% found this document useful (0 votes)
8 views

Image_Processing_LECTURE_6_UPDATED

Image_Processing

Uploaded by

Mert DUMAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Image_Processing_LECTURE_6_UPDATED

Image_Processing

Uploaded by

Mert DUMAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

INTRODUCTION TO IMAGE PROCESSING

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 step edge A ramp edge A roof edge


A step edge

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:

: the derivative with respect to 𝑥

: the derivative with respect to y


Image Gradient
Image gradient:
: the derivative with respect to 𝑥

: the derivative with respect to y

𝑔𝑦
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛−1
𝑔𝑥
Edge detection using image gradient
Image gradient:

𝑔𝑦
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛−1
𝑔𝑥
Edge detection using image gradient

The edge is perpendicular to the direction of the gradient


vector at the point where the gradient is computed.
Edge detection using image gradient
−1
𝑔𝑦
𝛼 𝑥, 𝑦 = 𝑡𝑎𝑛
𝑔𝑥

The edge is perpendicular to the direction of the gradient


vector at the point where the gradient is computed.
Edge detection using image gradient
The magnitude of the gradient is a measure of the edge strength
at the point where the gradient is computed.
atan2(y,x) is the angle measure (in radians, with − π < θ ≤ π )
between the positive x-axis and the ray from the origin to the
point (x , y) in the Cartesian plane.
Computing image gradient
Various masks to compute the gradient:
Gradient component along the x-direction
(vertical direction)

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:

3x3 Prewitt Kernel


5x5 Prewitt Kernel

3x3 Sobel Kernel


5x5 Sobel Kernel
Edge detection using gradients

• Compute gradients (𝑔𝑥 , 𝑔𝑦 ) at each pixel by using gradient


kernels
• Compute gradient magnitude at each pixel
• Mark pixels as «edges» when their gradient magnitudes
exceed a threshold
Edge detection using gradients
Large negative values: dark,
Large positive values: bright
in gradient display

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.

Image Gradient Image Gradient


magnitude with noise magnitude

It is wise to smooth the image before computing derivatives.


Disadvantage: Smoothing before edge detection gives thick edges (poor localization).
Derivatives and noise
It is wise to smooth the image before computing derivatives.
Disadvantage: Smoothing before edge detection gives thick edges (poor localization).

Image Gradient Gradient magnitude


with noise magnitude after noise reduction
with smoothing
Davide Scaramuzza
https://rpg.ifi.uzh.ch
Davide Scaramuzza
https://rpg.ifi.uzh.ch
Davide Scaramuzza
https://rpg.ifi.uzh.ch
Davide Scaramuzza
https://rpg.ifi.uzh.ch
Davide Scaramuzza
https://rpg.ifi.uzh.ch
The LoG function
sometimes is called the
Mexican hat operator.

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

𝜎 controls the amount of smoothing


Canny Edge Detection
STEP 1) Smooth the input image with a Gaussian filter

𝜎 controls the amount of smoothing


Canny Edge Detection
STEP 2) Compute the gradient magnitude and direction for each pixel

Use Sobel or Prewitt masks to compute 𝑔𝑥 and 𝑔𝑦 .

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» 𝑔𝑁 (𝑥, 𝑦).

Image smoothed Gradient Nonmaxima-suppressed


by a Gaussian filter magnitude 𝑀(𝑥, 𝑦) gradient magnitude
𝑔𝑁 (𝑥, 𝑦)
Canny Edge Detection
STEP 4) Double threshold

𝑔𝑁𝐻 (𝑥, 𝑦) contains STRONG EDGES

𝑔𝑁𝐿 (𝑥, 𝑦) contains WEAK EDGES


Canny Edge Detection
STEP 4) Mark and connect edges
Mark all strong edges (pixels where 𝑔𝑁𝐻 𝑥, 𝑦 = 1) as VALID EDGES
Canny Edge Detection

Original image Edges obtained by Canny algorithm


Canny Edge Detection

Original image Edges obtained by Canny algorithm


MATLAB SESSION
I = imread('cameraman.tif');
figure(1); imshow(I);
I = double(I);

sobel_mask_x = [-1 -2 -1;


0 0 0;
1 2 1]

sobel_mask_y = [-1 0 1;
-2 0 2; Ix
-1 0 1]

% Gradient component in the vertical direction


Ix = imfilter(I,sobel_mask_x,'replicate');
figure(2); imshow(Ix,[min(Ix(:)) max(Ix(:))]);

% Gradient component in the horizontal direction


Iy = imfilter(I,sobel_mask_y,'replicate');
figure(3); imshow(Iy,[min(Iy(:)) max(Iy(:))]);

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);

% Apply simple thresholding


% to get the edges

Thr = 100; Gradient


E = M > Thr; magnitude
figure(7); Histogram of the gradient
imshow(E); magnitude

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);

Thr = 0.1025 Thr = 0.05 Thr = 0.02


% Adjust threshold for Canny
I = imread('clown.bmp');
figure(1); imshow(I);

E_canny = edge(I,'canny',[0.04 0.06]);


figure(2); imshow(E_canny);

Thr = [0.0438 0.1094] Thr = [0.04 0.06]

You might also like