You are on page 1of 50

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Edges and Differences

Lecture weeks 17 and 18

Page 1

Lecture notes

Digital Image Processing by Jorma Kekalainen

Introduction
Edges contain the most useful information in an image.
In an image, an edge is a curve that follows a path of rapid
change in image intensity.
Edges are often associated with the boundaries of objects in a
scene.
We may use edges
to measure the size of objects in an image;
to isolate particular objects from their background;
to recognize or classify objects.

Edge detection is used to identify the edges in an image.

Jorma Kekalainen

Digital Image Processing

859

Edge-finding algorithms
There are a large number of edge-finding
algorithms, and we shall look at some of the most
straightforward of them.
Here, we shall show how to create edge images
using basic filtering methods, and discuss the
Matlab edge function.
The general Matlab command for finding edges is
edge(image,'method',parameters. . . )
where the available parameters depend on the
method used.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

860

Page 2

Lecture notes

Digital Image Processing by Jorma Kekalainen

Definition of edge
An edge may be loosely defined as a line of pixels showing an
observable difference.
For example, consider the two blocks of pixels

In the right hand block, there is a clear difference between


the gray values in the second and third columns.
This would be easily discernible in an image.
The human eye can pick out gray differences of this
magnitude with relative ease.
Our aim is to develop methods which will enable us to pick
out the edges of an image.
Jorma Kekalainen
Digital Image Processing

861

Edges
Suppose we follow the gray values of pixels in a line
crossing an edge, as illustrated below.

If we consider the gray values along this line, and


plot their values, we will have something similar to
that a step edge and a ramp edge shown in the
following figure.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

862

Page 3

Lecture notes

Digital Image Processing by Jorma Kekalainen

Gray values across edges


A step edge

A ramp edge

A line

Jorma Kekalainen

A roof

Digital Image Processing

863

Differences
If we now plot the differences between each gray
value and its predecessor from the ramp edge, we
would obtain a graph
Differences of the edge function

To see where this graph comes from, suppose that


the values of the ramp edge in the previous figure
are, from left to right:
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

864

Page 4

Lecture notes

Digital Image Processing by Jorma Kekalainen

Differences and edges


If we form the differences, by subtracting each value
from its successor, we obtain:
which are plotted above.
It appears that the difference tends to enhance
edges, and reduce other components.
So if we could difference the image, we would obtain
the effect we want.

Jorma Kekalainen

Digital Image Processing

865

Different differences
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

866

Page 5

Lecture notes

Digital Image Processing by Jorma Kekalainen

Generalized differences
An image is a function of two variables, so we
must generalize those definitions to include
both the x and y values

where the subscripts in each case indicate the


direction of the difference.

Jorma Kekalainen

Digital Image Processing

867

Finding vertical edges


To see how we might use x
to determine edges in the x direction, consider the function
values around a point (x,y) and using the convention that x
values increase to the right, y values to the bottom:

To find the filter which returns the value x , we just compare


the coefficients of the function values in x with their position
in the array:
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

868

Page 6

Lecture notes

Digital Image Processing by Jorma Kekalainen

Finding vertical edges


This filter

thus will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter

Jorma Kekalainen

Digital Image Processing

869

Prewitt filter Px
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:

>> A=[0 0 0;-1 0 1;0 0 0]


A=
0 0 0
-1 0 1
0 0 0
>> B=[0 1 0;0 1 0;0 1 0]
B=
0 1 0
0 1 0
0 Jorma
1 Kekalainen
0

Lecture weeks 17 and 18

Digital Image Processing

>> Px=conv2(A,B,'same')
Px =
-1 0 1
-1 0 1
870
-1 0 1

Page 7

Lecture notes

Digital Image Processing by Jorma Kekalainen

Finding horizontal edges


To find the filter which returns the value y ,
we just compare the coefficients of the function
values in y with their position in the previous
function array.

Jorma Kekalainen

Digital Image Processing

871

Finding horizontal edges


To see how we might use y
to determine edges in the y direction, consider the function
values around a point (x,y) and using the convention that x
values increase to the right, y values to the bottom:

To find the filter which returns the value y , we just compare


the coefficients of the function values in y with their position
in the array:
Jorma Kekalainen

Lecture weeks 17 and 18

0 -1 0
0 0 0
0 1 0
Digital Image Processing

872

Page 8

Lecture notes

Digital Image Processing by Jorma Kekalainen

Finding horizontal edges


This filter
0 -1
0 0
0 1

0
0
0

thus will find horizontal edges in an image.


However, the edges in the result can be a bit
irregular or jumpy; this can be overcome by
smoothing the result in the opposite direction by
using the filter
0
1
0
Jorma Kekalainen

0
1
0

0
1
0

Digital Image Processing

873

Prewitt filter Py
Both of the previous filters can be applied at
once convolving them.
As a result of convolution we get a combined
filter:
A =
0 -1
0 0
0 1
B' =
0
1
0

0
0
0

0 0
1 1
Jorma Kekalainen
0 0

Lecture weeks 17 and 18

Digital Image Processing

>> Py=conv2(A',B','same')
Py =
-1 -1 -1
0 0 0
874
1 1 1

Page 9

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
>> A=[0 0 0;-1 0 1;0 0 0]
A=
0 0 0
-1 0 1
0 0 0
>> B=[0 1 0;0 1 0;0 1 0]
B=
0 1 0
0 1 0
0 0
0 1 0
1 1
>> Px=conv2(A,B,'same')
0 0
Px =
-1 0 1
0 0
-1 0 1
1 1
-1 0 1
0 0
>> Py=conv2(A',B','same')
Py =
-1 -1 -1
0 0 0
Digital Image Processing 1
1 1

A=[0 0 0;-1 0 1;0 0 0]


B=[0 1 0;0 1 0;0 1 0]
>> A'
ans =
0 -1
0 0
0 1
0 -1
0 0
0 1

0
0
0
0
0
0

Jorma Kekalainen

>> B'
ans =
0
1
0
0
1
0

875

Prewitt filters
Filter for finding vertical edges

and its companion for finding horizontal edges

are the Prewitt filters for edge detection.


Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

876

Page 10

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
If we change increasing directions in the function array just
the opposite we correspondingly get matrices flilr(Px) and
flipud(Py)
1
1
1

Py =
-1 -1 -1
0 0 0
1 1 1

>> fliplr(Px)
ans =
1 0 -1
1 0 -1
1 0 -1

>>flipud(Py)
ans =
1 1 1
0 0 0
-1 -1 -1

Px =
-1
-1
-1

0
0
0

Flipud flip matrix up to down and Fliplr flip matrix left to right.
Jorma Kekalainen

Digital Image Processing

877

Note
h = fspecial('prewitt') returns the 3-by-3 filter
h=[ 1 1 1; 0 0 0; -1 -1 -1 ]
that emphasizes horizontal edges by approximating a
>> fspecial('prewitt')
vertical gradient.
ans =
1 1 1
0 0 0
-1 -1 -1

If you need to emphasize vertical edges, transpose


the filter h'.
To find vertical edges, or for x-derivatives, use h'.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

878

Page 11

Lecture notes

Digital Image Processing by Jorma Kekalainen

Applaying Prewitt filters


We are now in the position of having to apply two filters to an
image.
Each filter is applied independently, and formed the output
image from a function of the filter value produced.
So if px and py were the gray values produced by applying Px
and Py to an image, then the output gray value v can be
chosen by any of these methods:

Jorma Kekalainen

Digital Image Processing

879

Example
Let us take the image of
the integrated circuit ,
which can be read into
Matlab with
>> ic=imread('ic.tif');
Applying each of Px and
Py individually provides
the results shown in the
following slice
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

An original image

880

Page 12

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
After filtering with the Prewitt filters
Figure (a)

Jorma Kekalainen

Figure (b)

Digital Image Processing

881

Example
Figure (a) was produced with the following Matlab
commands:
px=[-1 0 1;-1 0 1;-1 0 1];
icx=filter2(px,ic);
figure,imshow(icx/255)
and figure (b) with
py=px';
icy=filter2(py,ic);
figure,imshow(icy/255)
Note that the filter Px highlights vertical edges, and Py
horizontal edges.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

882

Page 13

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

%Edge detection
ic=imread('ic.tif');
figure,imshow(ic)
title('Original image ic.tif')
%
px=[-1 0 1;-1 0 1;-1 0 1];
icx=filter2(px,ic);
figure,imshow(icx/255)
title('Vertical edges in ic.tif')
%
py=px';
icy=filter2(py,ic);
figure,imshow(icy/255)
title('Horizontal edges in ic.tif')
%
% All the edges with:
edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
title('All edges in the gray-scale image ic.tif')

Jorma Kekalainen

>> px=[-1 0 1;-1 0 1;-1 0 1]


px =
-1 0 1
-1 0 1
-1 0 1
>> py=px'
py =
-1 -1 -1
0 0 0
1 1 1

Digital Image Processing

883

Example
>> %
level=graythresh(ic)
BW=im2bw(ic,level);
figure,imshow(BW)
title('Binary image thresholded from ic.tif using graythresh leveling')
xlabel(['Threshold level= ',num2str(level)])
level =
0.6784

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

884

Page 14

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
>> edge_th=im2bw(edge_p/255,graythresh(edge_p));
figure,imshow(edge_th)
title('Binary image thresholded after Prewitt process using graythresh
leveling')
xlabel(['Threshold level=',num2str(graythresh(edge_p))])
%

Jorma Kekalainen

Digital Image Processing

885

Example
We can create a figure containing all the edges with:
edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
This is a gray-scale image; a binary image containing
edges only can be produced by thresholding.
edge_t=im2bw(edge_p/255,0.3);

>> whos('edge_p','edge_t')
Name
Size
Bytes Class Attributes
edge_p 256x256
524288 double
Jorma Kekalainen
edge_t
256x256
65536 logicalDigital Image Processing

Lecture weeks 17 and 18

886

Page 15

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

% All the edges with:


edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
title('All edges in the grayscale image ic.tif')
edge_t=im2bw(edge_p/255,0.
3);
figure,imshow(edge_t)
title('Binary image
thresholded from ic.tif')
xlabel('Threshold level=0.3')

Jorma Kekalainen

Digital Image Processing

887

Note
We can obtain edges by the Prewitt filters directly by
using the command
>> edge_p=edge(ic,'prewitt');
and the edge function takes care of all the filtering, and
of choosing a suitable threshold level.
The result is shown in the following figure.
Note that the previous figure and the next figure seem
different to each other.
This is because the edge function does some extra
processing besides taking the square root of the sum of
the squares of the filters.
Jorma Kekalainen
Image Processing
Note:
See the help text of theDigital
edge
function for more information.888

Lecture weeks 17 and 18

Page 16

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
%We can obtain edges by the Prewitt filters directly by using the command
edge_p=edge(ic,'prewitt');
figure, imshow(edge_p)
title('Edges of ic.tif directly by using the Prewitt filters')
xlabel('Using command edge(ic,''prewitt'');')

Jorma Kekalainen

Digital Image Processing

889

Sobel filters
The Sobel filters are similar to the Prewitt filters, in that they
apply a smoothing filter in the opposite direction to the
central difference filter.
Now the smoothing takes the form which gives slightly more
prominence to the central pixel
0
1
0

Jorma Kekalainen

Lecture weeks 17 and 18

0
2
0

0
1
0

0
0
0

Digital Image Processing

1
2
1

0
0
0

890

Page 17

Lecture notes

Digital Image Processing by Jorma Kekalainen

Sobel filter Sx
As we previously notice in Prewitts case this filter

will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter
0
0
0
Jorma Kekalainen

1
2
1

0
0
0

In the Sobel filters, the


smoothing gives slightly
more prominence to the
central pixel.

Digital Image Processing

891

Sobel filter Sx
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:
Sx =

Sx will find vertical edges in an image.


>> A=[0 0 0;-1 0 1;0 0 0]
A=
0 0 0
-1 0 1
Jorma Kekalainen
0 0 0

Lecture weeks 17 and 18

>> B'=[0 1 0;0 2 0;0 1 0]


B' =
0 1 0
0 2 0Digital Image Processing
0 1 0

>> Sx=conv2(A,B','same')
Sx =
-1 0 1
-2 0 2
892
-1 0 1

Page 18

Lecture notes

Digital Image Processing by Jorma Kekalainen

Sobel filter Sy
As we previously notice in Prewitts case this filter
0 -1
0 0
0 1

0
0
0

will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter
0
1
0
Jorma Kekalainen

0
2
0

0
1
0

In the Sobel filters, the


smoothing gives slightly
more prominence to the
central pixel.

Digital Image Processing

893

Sobel filter Sy
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:
Sy =
Sy will find horizontal edges in an image.
>> Sx=conv2(A,B','same')
Sx =
-1 0 1
-2 0 2
Jorma Kekalainen
-1 0 1

Lecture weeks 17 and 18

Digital Image Processing

Sy=Sx'=(conv2(A,B','same'))'
Sy =
-1 -2 -1
0 0 0
894
1 2 1

Page 19

Lecture notes

Digital Image Processing by Jorma Kekalainen

Sobel filters
Filter for finding vertical edges
Sx =
and its companion for finding horizontal edges
Sy =
are the Sobel filters for edge detection.
Jorma Kekalainen

Digital Image Processing

895

Note
If we change increasing directions in the function array just
the opposite we correspondingly get matrices flilr(Sx) and
flipud(Sy)
Sx =
-1
-2
-1

1
2
1

Sy =
-1 -2 -1
0 0 0
1 2 1

>> fliplr(Sx)
ans =
1 0 -1
2 0 -2
1 0 -1

>> flipud(Sy)
ans =
1 2 1
0 0 0
-1 -2 -1

0
0
0

Flipud flip matrix up to down and Fliplr flip matrix left to right.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

896

Page 20

Lecture notes

Digital Image Processing by Jorma Kekalainen

fspecial('sobel')
h = fspecial('sobel') returns a 3-by-3 filter
h =[ 1 2 1; 0 0 0; -1 -2 -1 ]
that emphasizes horizontal edges using the
smoothing effect by approximating a vertical
gradient.
If you need to emphasize vertical edges,
transpose the filter h'.

Jorma Kekalainen

Digital Image Processing

h=
1 2 1
0 0 0
-1 -2 -1

h=h' =
1 0 -1
2 0 -2
897
1 0 -1

Example
edge_r=edge(ic,'roberts');
figure,imshow(edge_r)

edge_s=edge(ic,'sobel');
figure,imshow(edge_s)

Of the three filters, the Sobel filters are probably the best; they provide good edges,
and they
perform reasonably well in Digital
the presence
of noise.
Jorma Kekalainen
Image Processing
898

Lecture weeks 17 and 18

Page 21

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

%Roberts and Sobel filters


edge_r=edge(ic,'roberts');
figure,imshow(edge_r)
title('Edge finding of ic.tif using Roberts cross-gradient filters')
xlabel('Using command edge(ic,''proberts'');')
%
%and
%
edge_s=edge(ic,'sobel');
figure,imshow(edge_s)
title('Edge finding of ic.tif using Sobel filters')
xlabel('Using command edge(ic,''sobel'');')

Jorma Kekalainen

Digital Image Processing

899

Example

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

900

Page 22

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Second Differences

Difference of differences
Another class of edge-detection method is
obtained by considering the difference of
differences.
These are called second differences.
E.g.,
x2 = x x f ( x, y )
y2 = y y f ( x, y )

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

902

Page 23

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen

Digital Image Processing

903

Note
An image is a function of two variables, so we
must generalize those definitions to include
both the x and y values

where the subscripts in each case indicate the


direction of the difference.

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

904

Page 24

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
x2 = x x f ( x, y ) = x [ f ( x + 1, y ) f ( x, y )]

= [ f ( x + 1, y ) f ( x, y )] [ f ( x, y ) f ( x 1, y )]
= f ( x + 1, y ) 2 f ( x, y ) + f ( x 1, y )

Jorma Kekalainen

Digital Image Processing

905

Central second differences


To calculate a central second difference, take the backward
difference of a forward difference:

x2 = x x f ( x, y ) = x [ f ( x + 1, y ) f ( x, y )]

= [ f ( x + 1, y ) f ( x, y )] [ f ( x, y ) f ( x 1, y )]
= f ( x + 1, y ) 2 f ( x, y ) + f ( x 1, y )
The corresponding filter for second differences x2 in the x
direction can be implemented by the filter:

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

906

Page 25

Lecture notes

Digital Image Processing by Jorma Kekalainen

Central second differences


To calculate a central second difference, take the backward
difference of a forward difference:

y2 = y y f ( x, y ) = y [ f ( x, y + 1) f (x, y )]

= [ f ( x, y + 1) f (x, y )] [ f ( x, y ) f ( x, y 1)]
= f ( x, y + 1) 2 f ( x, y ) + f ( x, y 1)
The corresponding filter for second differences y2 in the y
direction can be implemented by the filter

Jorma Kekalainen

Digital Image Processing

907

Laplacian filter
The corresponding filters for second differences x2 in the x
direction and for second differences y2 in the y direction are

The sum of these two is written


and implemented by the filter, which is known as a discrete
Laplacian.

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

908

Page 26

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Take the pixel values describing a ramp edge

If we form the differences, by subtracting each value from its


successor, we obtain:

Differences of the edge function

Jorma Kekalainen

Digital Image Processing

909

Laplacian
We see that the Laplacian (after taking an
absolute value, or squaring) gives double
edges.
It is also extremely sensitive to noise.
However, the Laplacian does have the
advantage of detecting edges in all directions
equally well.

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

910

Page 27

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Suppose we enter the Matlab commands:
l=fspecial('laplacian',0);% Basic laplacian mask
ic_l=filter2(l,ic);
figure,imshow(mat2gray(ic_l))
The result of which is shown in the following
figure.

Jorma Kekalainen

Digital Image Processing

911

Example
%
ic=imread('ic.tif');
figure,imshow(ic)
title('Original image ic.tif')
%
L=fspecial('laplacian',0);
ic_L=filter2(L,ic);
figure,imshow((ic_L))
title('Filtering by discrete Laplacian')
xlabel('fspecial(''laplacian'',0)')
%

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

912

Page 28

Lecture notes

Digital Image Processing by Jorma Kekalainen

Result after filtering with a


discrete laplacian
Although the result is
adequate, it is very
messy when compared
to the results of the
Prewitt and Sobel
methods discussed
earlier.

Jorma Kekalainen

Digital Image Processing

913

Other Laplacian masks


Other Laplacian masks can be used, e.g.,:

In Matlab, Laplacians of all sorts can be generated using the


fspecial function, in the form fspecial('laplacian',alpha) which
produces the Laplacian

If the parameter alpha (which is optional) is omitted, it is


assumed to be 0.2.
Jorma Kekalainen
Digital Image Processing
Note: The value 0 gives the Laplacian
developed earlier.

Lecture weeks 17 and 18

914

Page 29

Lecture notes

Digital Image Processing by Jorma Kekalainen

fspecial('laplacian', alpha)
fspecial('laplacian', alpha) returns a 3-by-3
filter approximating the shape of the twodimensional Laplacian operator.
The parameter alpha controls the shape of the
Laplacian and must be in the range 0.0 to 1.0.
The default value for alpha is 0.2.

Jorma Kekalainen

Digital Image Processing

915

fspecial('laplacian', alpha)

>> fspecial('laplacian',1.0)

ans =

0.5000
0 0.5000

0 -2.0000
0

0.5000
0 0.5000

>> fspecial('laplacian',0.9)

ans =

0.4737 0.0526 0.4737

0.0526 -2.1053 0.0526

0.4737 0.0526 0.4737

>> fspecial('laplacian',0.8)

ans =

0.4444 0.1111 0.4444

0.1111 -2.2222 0.1111

0.4444 0.1111 0.4444

>> fspecial('laplacian',0.7)

ans =

0.4118 0.1765 0.4118

0.1765 -2.3529 0.1765

0.4118 0.1765 0.4118

>> fspecial('laplacian',0.6)

ans =

0.3750 0.2500 0.3750

0.2500 -2.5000 0.2500

0.3750 0.2500 0.3750

>> fspecial('laplacian',0.5)

ans =

0.3333 0.3333 0.3333

0.3333 -2.6667 0.3333


Jorma Kekalainen
0.3333 0.3333 0.3333

Lecture weeks 17 and 18

>> fspecial('laplacian',0.4)
ans =
0.2857 0.4286 0.2857
0.4286 -2.8571 0.4286
0.2857 0.4286 0.2857
>> fspecial('laplacian',0.3)
ans =
0.2308 0.5385 0.2308
0.5385 -3.0769 0.5385
0.2308 0.5385 0.2308
>> fspecial('laplacian',0.2)
ans =
0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667

>> fspecial('laplacian',0.1)
ans =
0.0909 0.8182 0.0909
0.8182 -3.6364 0.8182
0.0909 0.8182 0.0909

>> fspecial('laplacian',0.0)
ans =
0 1 0
1 -4 1
0 1 0

Digital Image Processing

default value
alpha=0.2

916

Page 30

Lecture notes

Digital Image Processing by Jorma Kekalainen

Zero crossings
A more appropriate use for the Laplacian is to find the
position of edges by locating zero crossings.
If we look at figure below, we see that the position of the
edge is given by the place where the value of the filter takes
on a zero value.
In general, these are places where the result of the filter
changes sign.

Jorma Kekalainen

Digital Image Processing

917

Definition of the zero crossings


We define the zero crossings
in such a filtered image to be
pixels which satisfy either of
the following:
1. They have both a negative
gray value and they are next to
(by four-adjacency) a pixel
whose gray value is positive,
2. They have both a value of
zero and they are between
negative and positive valued
pixels.

Jorma Kekalainen

Lecture weeks 17 and 18

Filtered image
Zero crossings are inside red rectangles

Digital Image Processing

918

Page 31

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Zero crossings are inside red circles

Jorma Kekalainen

Digital Image Processing

919

Example
To give an indication of the
way zero-crossings work,
look at the edge plots and
their second differences.
In each case the zerocrossing is circled.
The important point is to
note that across any edge
there can be only one zerocrossing.
Thus an image formed from
zero-crossings has the
potential to be very handy.
Jorma Kekalainen

Lecture weeks 17 and 18

Edges and second differences

Digital Image Processing

920

Page 32

Lecture notes

Digital Image Processing by Jorma Kekalainen

Locating zero crossings in an


image
Consider a small image (a), and the result after
filtering with a Laplacian mask in figure (b).
Zero crossings are colored with red.
(a) Small image
50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
200
200
200
200
50
50
50
50

(b) After laplace filtering

-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
50 50 50 50 50 50 50
-50 0 150 150 150 150 150 150 0 -50
50 50 50 50 50 50 50
200 200 200 200 200 50 50 -50 150 -300 -150 -150 -150 -150 -300 150 -50
200 200 200 200 200 50 50 -50 150 -150 0 0 0 0 -150 150 -50
200 200 200 200 200 50 50 -50 150 -150 0 0 0 0 -150 150 -50
200 200 200 200 200 50 50 -50 150 -300 -150 0 0 0 -150 150 -50
-50 0 150 300 -150 0 0 -150 150 -50
50 200 200 200 200 50 50
-50 0 0 150 -300 -150 -150 -300 150 -50
50 200 200 200 200 50 50
-50 0 0 0 150 150 150 150 0 -50
50 50 50 50 50 50 50
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
50 50 50 50 50 50 50

Jorma Kekalainen

Digital Image Processing

921

Zerocross option of edge


We now have a further method of edge detection: take the
zero-crossings after a laplace filtering.
This is implemented in Matlab with the zerocross option of
edge, which takes the zero crossings after filtering with a
given filter:
>> l=fspecial('laplacian',0);
>> icz=edge(ic,'zerocross',l);
>> imshow(icz)

The result is not a very good - far too


many gray level changes have been
interpreted as edges by this method.
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

922

Page 33

Lecture notes

Digital Image Processing by Jorma Kekalainen

Marr-Hildreth method
As we saw previously far too many gray level changes have
been interpreted as edges by that method.
To eliminate this problem, we may first smooth the image
with a Gaussian filter.
This leads to the following sequence of steps for edge
detection called Marr-Hildreth method:
1. smooth the image with a Gaussian filter,
2. convolve the result with a laplacian,
3. find the zero crossings.

Jorma Kekalainen

Digital Image Processing

923

Marr-Hildreth method
This method was designed to provide an edge detection method to
be as close as possible to biological vision.
The first two steps (smooting and convolving) can be combined
into one, to produce a Laplacian of Gaussian (LoG) or 'log' filter.
These filters can be created with the fspecial function.
If no extra parameters are provided to the zerocross edge option,
then the filter is chosen to be the LoG filter found by
fspecial('log',13,2)
This means that the following command:
edge(ic,'log');
produces exactly the same result as the commands:
log=fspecial('log',13,2);
edge(ic,'zerocross',log);
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

924

Page 34

Lecture notes

Digital Image Processing by Jorma Kekalainen

fspecial('log', hsize, sigma)


h = fspecial('log', hsize, sigma) returns a
rotationally symmetric Laplacian of Gaussian
(LoG) filter of size hsize with standard
deviation sigma (positive).
hsize can be a vector specifying the number of
rows and columns in h, or it can be a scalar, in
which case h is a square matrix.
The default value for hsize is [5 5] and 0.5 for
sigma.
Jorma Kekalainen

Digital Image Processing

925

Marr-Hildreth method
In fact the LoG and
zerocross options
implement the same edge
finding method; the
difference being that the
zerocross option allows
us to specify our own
filter.
The result after applying
an LoG filter and finding
its zero crossings is given
in figure beside
Jorma Kekalainen

Lecture weeks 17 and 18

Using an LoG filter first and


finding then zero crossings

Digital Image Processing

926

Page 35

Lecture notes

Digital Image Processing by Jorma Kekalainen

Marr-Hildreth method
log=fspecial('log',13,2);
edge(ic,'zerocross',log);

Jorma Kekalainen

edge(ic,'log');

Digital Image Processing

927

Edge detection
In an image, an edge is a curve that follows a path of rapid
change in image intensity.
Edges are often associated with the boundaries of objects in a
scene.
Edge detection is used to identify the edges in an image.
To find edges, we can use the edge function.
This function looks for places in the image where the intensity
changes rapidly, using one of these two criteria:
Places where the first derivative of the intensity is larger in magnitude
than some threshold
Places where the second derivative of the intensity has a zero crossing

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

928

Page 36

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
There are a a vast number of different edge detection algorithms
and methods.
As to which method is the best; like so many image processing
tasks, this is highly subjective.
The choice of an suitable edge detector will depend very much on
the nature of the image, the amount (and type) of noise, and the
use for which the edges are to be put.
The edge function provides a number of derivative estimators.
The most powerful edge-detection method that Matlabs edge
function provides is the Canny method.
The Canny method differs from the other edge-detection methods
in that it uses two different thresholds (to detect strong and weak
edges), and includes the weak edges in the output only if they are
connected to strong edges.
This method is therefore less likely than the others to be fooled by
noise, and more likely to detect true weak edges.
Jorma Kekalainen

Digital Image Processing

929

Example
I = imread('coins.png');

figure,imshow(I)
>> BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
figure;
imshowpair(BW1,BW2,'montage')
title('Sobel Filter
Canny Filter');

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

930

Page 37

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exercise
Construct the next matrix image using matrix operations
50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
200
200
200
200
50
50
50
50

50 50 50 50 50 50 50
50 50 50 50 50 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
50 200 200 200 200 50 50
50 200 200 200 200 50 50
50 50 50 50 50 50 50
50 50 50 50 50 50 50

Filter the previous image with fspecial('laplacian',0) and


construct the corresponding zero crossing chart.
Jorma Kekalainen

Digital Image Processing

931

Solution
>> B=50*ones(10)
B=
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50
50
50
50
50

>> B(3:8,3:8)=200*ones(6)
B=
50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50

>> B(7:8,3:4)=50*ones(2)
B=
50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 50 50 200 200 200 200 50 50
50 50 50 50 200 200 200 200 50 50
50 50 50 50 50 50 50 50 50 50
Jorma Kekalainen
Digital Image Processing
50 50 50 50 50 50 50 50 50 50

Lecture weeks 17 and 18

50
50
50
50
50
50
50
50
50
50

50
50
50
50
50
50

B=100*ones(10)
B(3:8,3:8)=200*ones(6)
B(7:8,3:4)=100*ones(2)
l=fspecial('laplacian',0);
B_L=filter2(l,B)
932

Page 38

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
l=fspecial('laplacian',0);
B_L=filter2(l,B)
B_L =
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
-50 0 150 150 150 150 150 150 0 -50
-50 150 -300 -150 -150 -150 -150 -300 150 -50
-50 150 -150 0 0 0 0 -150 150 -50
-50 150 -150 0 0 0 0 -150 150 -50
-50 150 -300 -150 0 0 0 -150 150 -50
-50 0 150 300 -150 0 0 -150 150 -50
-50 0 0 150 -300 -150 -150 -300 150 -50
-50 0 0 0 150 150 150 150 0 -50
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100

Jorma Kekalainen

Digital Image Processing

933

Digital Image Processing


Edge Enhancement

Lecture weeks 17 and 18

Page 39

Lecture notes

Digital Image Processing by Jorma Kekalainen

Edge enhancement
Previously we have seen how to isolate edges
in an image.
A related operation is to make edges in an
image slightly sharper, which generally results
in an image more pleasing to the human eye.
The operation is called edge enhancement, or
unsharp masking.
This last term comes from the printing
industry.
Jorma Kekalainen

Digital Image Processing

935

Idea of unsharp
The idea of unsharp masking is to subtract a scaled unsharp
version of the image from the original.
In practice, we can achieve this affect by subtracting a scaled
blurred image from the original.
The schema for unsharp masking

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

936

Page 40

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Suppose we have an image x of type uint8, then we can apply
unsharp masking by the following sequence of commands:
f=fspecial('average');
xf=filter2(f,x);
xu=double(x)-xf/1.5;% scale k=1/1.5<1
imshow(xu/70)
The last command scales the result so that imshow displays
an appropriate image; the value may need to be adjusted
according to the input image.

Jorma Kekalainen

Digital Image Processing

937

Example
%Unsharp masking
%Suppose we have an image C of type uint8. Then we can apply
%unsharp masking by the following sequence of commands:
%
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image')
f=fspecial('average');
Cf=filter2(f,C);
Cu=double(C)-Cf/1.5;
figure, imshow(Cu/70)
title('Unsharp masking with 3*3 averaging filter')

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

938

Page 41

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
Suppose that C is the image shown in figure(a), then the result
of unsharp masking is given in figure (b).
The result appears to be a better image than the original; the
edges are sharper and more clearly defined.
(a) Original image

Jorma Kekalainen

(b) Image after unsharp masking

Digital Image Processing

939

Unsharp masking
To see why this unsharp
masking works, we may
consider the function of
gray values as we travel
across an edge, as shown
in graph (a).
As a scaled (k<1) blur is
subtracted from the
original, the result is that
the edge is enhanced, as
shown in graph (c).
Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

940

Page 42

Lecture notes

Digital Image Processing by Jorma Kekalainen

Unsharp masking filter


We can in fact perform the filtering and subtracting operation
in one command, using the linearity of the filter, and that the
3*3 filter

is the identity filter.


Hence unsharp masking can be implemented by a filter of the
form

where k is a constant chosen to provide the best result.


Jorma Kekalainen

Digital Image Processing

941

Unsharp masking filter


Alternatively, the unsharp masking filter may
be defined as

so then we are subtracting a blur from a


scaled version of the original.

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

942

Page 43

Lecture notes

Digital Image Processing by Jorma Kekalainen

Unsharp masking filter


The scaling factor may also be split between the identity and
blurring filters.
The unsharp option of fspecial function produces such filters;
the filter created has the form

where is an optional parameter which defaults to 0.2.


If =0.5 the filter is

Jorma Kekalainen

Digital Image Processing

943

Exercise
Verify with Matlab the previous equality

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

944

Page 44

Lecture notes

Digital Image Processing by Jorma Kekalainen

Solution
Verify with Matlab the previous equality

>> B=-3*1/9*ones(3)
B=
-0.3333 -0.3333 -0.3333
-0.3333 -0.3333 -0.3333
>> C=1/3*[-1,-1,-1;-1,11,-1;-1,-1,-1]
-0.3333 -0.3333 -0.3333
C=
>> A+B
-0.3333 -0.3333 -0.3333
ans =
-0.3333 3.6667 -0.3333
-0.3333 -0.3333 -0.3333
-0.3333 -0.3333 -0.3333
-0.3333 3.6667 -0.3333
Jorma Kekalainen
Digital Image Processing
-0.3333 -0.3333 -0.3333

945

Example
Figure below was created using the Matlab commands
p=imread('cameraman.tif');
u=fspecial('unsharp',0.5);
pu=filter2(u,p);
imshow(p),figure,imshow(pu/255)
Original

Jorma Kekalainen

Lecture weeks 17 and 18

Figure after unsharp


masking, appears sharper
and cleaner than the
original.

After unsharp masking

Digital Image Processing

Note: Although we have


used averaging filters above,
we can use any low pass
946
filter for unsharp masking.

Page 45

Lecture notes

Digital Image Processing by Jorma Kekalainen

High boost filtering


Allied to unsharp masking filters are the high boost filters,
which are obtained by
high boost = A(original)-(low pass)
where A is an amplication factor.
If A=1, then the high boost filter becomes an ordinary high
pass filter.
If we take as the low pass filter the 3*3 averaging filter, then a
high boost filter will have the form
where z > 8

Jorma Kekalainen

Digital Image Processing

947

High boost filtering


If we put z=11 , we obtain a filtering very similar to the
unsharp filter above, except for a scaling factor.
Thus the commands:
>> f=[-1 -1 -1;-1 11 -1;-1 -1 -1]/9;
>> Cf=filter2(C,f);
>> imshow(Cf/90)
will produce a image similar to that in an earlier figure.
The value 90 was obtained by trial and error to
produce an image with similar intensity to the original.

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

948

Page 46

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image')
f=[-1 -1 -1;-1 11 -1;-1 -1 -1]/9;
Cf=filter2(f,C);
figure, imshow(Cf/90)
title('Filtering very similar to the unsharp filter above, except
for a scaling factor')
xlabel('Coefficient 1/90 was obtained by trial and error')

Jorma Kekalainen

Digital Image Processing

949

Example

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

950

Page 47

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
We can also write the high boost formula above as

Best results for high boost filtering are obtained if we multiply


the equation by a factor so that the filter values sum to 1;
this requires A- =1 or =1/(A-1).
So a general unsharp masking formula is

Jorma Kekalainen

Digital Image Processing

951

High boost filters


A different version of the high boost filter is

where for best results A taken so that 3/5 A5/6.


If we take A=3/5 , the formula becomes

If we take A=5/6

Jorma Kekalainen

Lecture weeks 17 and 18

Digital Image Processing

952

Page 48

Lecture notes

Digital Image Processing by Jorma Kekalainen

Special high boost filters


Using the identity and averaging filters, we can obtain high
boost filters by:
>>id=[0 0 0;0 1 0;0 0 0];
>>f=fspecial('average');
>>hb1=3*id-2*f
hb1 =
-0.2222 -0.2222 -0.2222
-0.2222 2.7778 -0.2222
-0.2222 -0.2222 -0.2222
>> hb2=1.25*id-0.25*f
hb2 =
-0.0278 -0.0278 -0.0278
-0.0278 1.2222 -0.0278
Jorma
Kekalainen
Digital Image Processing
-0.0278
-0.0278 -0.0278

953

Special high boost filters


id=[0 0 0;0 1 0;0 0 0]
f=fspecial('average')
id =
0 0 0
0 1 0
0 0 0
f=
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
Jorma Kekalainen

Lecture weeks 17 and 18

hb1=3*id-2*f
hb2=1.25*id-0.25*f
hb1 =
-0.2222 -0.2222 -0.2222
-0.2222 2.7778 -0.2222
-0.2222 -0.2222 -0.2222
hb2 =
-0.0278 -0.0278 -0.0278
-0.0278 1.2222 -0.0278
-0.0278 -0.0278 -0.0278

Digital Image Processing

954

Page 49

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

If each of the filters hb1 and hb2 are applied to an image with filter2, the
result will have enhanced edges.
The images were obtained with
x1=filter2(hb1,x);
x2=filter2(hb1,x);
imshow(x2/255)
imshow(x1/255)

(a) High boost filtering with hb1

Jorma Kekalainen

Lecture weeks 17 and 18

(b) High boost filtering with hb2

Digital Image Processing

hb1 appears
to produce the
best result;
hb2 produces
an image not
very much
sharper than
955
the original.

Page 50

You might also like