Sunteți pe pagina 1din 10

EXPERIMENT 5

TITLE:
Compute the edges of the given image using the following edge detectors and comment
on their performance:

1. Robert

2. Prewitt

3. Sobel

4. Canny

OBJECTIVE:
To understand the edge detection using different operators and comment on their
performance.

THEORY:

What are Edges?


The sudden changes of discontinuities in an image are called as edges.Significant
transitions in an image are called as edges.

Type of edges:
Generally edges are of three types:

1. Horizontal Edges
2. Vertical Edges
3. Diagonal Edges

Why detect edges


Most of the shape information of an image is enclosed in edges.So first we detect these
edges in an image and by using these filters and then by enchancing those areas of image
which contains edges,sharpness of an image will increase and image will become clearer.

Here are some of the masks of edge detection that we discuss:

1. Robert Operator
2. Prewitt Operator
3. Sobel Operator
4. Canny Operator

Above mentioned all the filters are linear filters or smoothening filters.
Robert Operator
The Roberts operator performs a simple, quick to compute, 2-D spatial gradient
measurement on an image. It thus highlights regions of high spatial gradient which often
correspond to edges. In its most common usage, the input to the operator is a grey scale
image, as is the output. Pixel values at each point in the output represent the estimated
absolute magnitude of the spatial gradient of the input image at that point.

How It Works
In theory, the operator consists of a pair of 2×2 convolution masks as shown in Figure 1.
One mask is simply the other rotated by 90°. This is very similar to the Sobel operator.

+1 0 0 +1
0 -1 -1 0
Gx Gy

Figure 1. Roberts Cross convolution masks

These masks are designed to respond maximally to edges running at 45° to the pixel grid,
one mask for each of the two perpendicular orientations. The masks can be applied
separately to the input image, to produce separate measurements of the gradient
component in each orientation (call these Gx and Gy). These can then be combined
together to find the absolute magnitude of the gradient at each point and the orientation of
that gradient. The gradient magnitude is given by:

|G|=√Gx^2+Gy^2
although typically, an approximate magnitude is computed which is much faster to
compute.

|G|=|Gx|+|Gy|
The angle of orientation of the edge (relative to the pixel grid) giving rise to the spatial
gradient is given by:

=arctan(Gy/Gx)-3∏/4

In this case, orientation 0 is taken to mean that the direction of maximum contrast from
black to white runs from left to right on the image, and other angles are measured
anticlockwise from this.
Often, this absolute magnitude is the only output the user sees --- the two components of
the gradient are conveniently computed and added in a single pass over the input image
using the pseudo-convolution operator shown in Figure
P1 P2
P3 P4

Figure: Pseudo-Convolution masks used to quickly compute approximate gradient


Magnitude

Using this mask the approximate magnitude is given by:

|G|=|P1-P4|+|P2-P3|

CODE FOR ROBERT OPERATOR:

newImg = imread('cameraman.tif'); %% create a new figure to show


the image.
figure(1);
imshow(newImg);
figure(2);
robertsResult = edge(newImg,'roberts'); %% apply roberts filter.
imshow(robertsResult);
subplot(2,2,1);imshow(newImg);title(“Original Image”);
subplot(2,2,4);imshow(robertsResult);title(“After applying Robert ”);

Prewitt Operator

Prewitt operator is used for edge detection in an image. It detects two types of edges

1. Horizontal edges
2. Vertical Edges

Prewitt operator provides us two masks one for detecting edges in horizontal direction
and another for detecting edges in an vertical direction.
The prewitt operator is based on convolving the image with a small ,separable,and integer
valued filter in horizontal and vertical direction and is therefore relatively inexpensive
in terms of computation.
Mathematically,the operator uses two 3x3 kernels .

Consider the arrangement of pixels about the pixels( i , j):


Z1 Z2 Z3
Z4 Z5 Z6
Z7 Z8 Z9

The partial derivatives can be computed by:

Mx=(z3+cz6+z9)-(z1+cz4+z7)
My=(z7+cz8+z9)-(z1+cz2+z3)
(the constant c implies the emphasis given to pixels closer to the center of the mask)

Vertical direction

-1 0 1
-1 0 1
-1 0 1

Above mask will find the edges in vertical direction and it is because the zeros column in
the vertical direction. When you will convolve this mask on an image, it will give you the
vertical edges in an image.

Horizontal Direction

Horizontal Horizontal Horizontal


0 0 0
1 1 1

Above mask will find edges in horizontal direction and it is because that zeros column is
in horizontal direction. When you will convolve this mask onto an image it would
prominent horizontal edges in the image.

Fig . shows the process of prewitt edge detection operator


Threshold Value
Horizontal
Gradient
h Input image Gradient Apply Threshold
gradient Combining

Vertical
Gradient Detector Output
CODE FOR PREWITT OPERATOR:

newImg = imread('cameraman.tif'); %% create a new figure to show the


image.
figure(1);
imshow(newImg);
figure(2);
afterFilter = edge(newImg,'prewitt'); %% apply prewitt filter.
imshow(afterFilter);
subplot(2,2,1);imshow(newImg);title(“OriginalImage”);
subplot(2,2,4);imshow(afterFilter);title(“After applying Prewitt ”);

Sobel Operator

The sobel operator is very similar to Prewitt operator. It is also a derivate mask and is used
for edge detection. It also calculates edges in both horizontal and vertical direction.
It is used to compute an approximation of the gradient of image intensity function for edge
detection.

Following is the vertical Mask of Sobel Operator:

-1 0 1
-2 0 2
-1 0 1

This mask works exactly same as the Prewitt operator vertical mask. There is only one
difference that is it has “2” and “-2” values in center of first and third column. When applied
on an image this mask will highlight the vertical edges.

Following is the horizontal Mask of Sobel Operator

-1 -2 -1
0 0 0
1 2 1

Above mask will find edges in horizontal direction and it is because that zeros column is
in horizontal direction. When you will convolve this mask onto an image it would
prominent horizontal edges in the image. The only difference between it is that it have 2
and -2 as a center element of first and third row.
CODE FOR SOBEL EDGE DETECTOR

newImg = imread('cameraman.tif'); %% create a new figure to show the image


.
figure(1);
imshow(newImg);
figure(2);
BW = edge(newImg,'sobel',0.1) ; %% apply sobel filter.
imshow(BW);
subplot(2,2,1);imshow(newImg);title(“OriginalImage”);
subplot(2,2,4);imshow(BW);title(“After applying Sobel ”);

Canny Operator

The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to
detect a wide range of edges in images. It was developed by John F. Canny in 1986. Canny
also produced a computational theory of edge detection explaining why the technique
works.
Gradient direction is always perpendicular to edges. It is rounded to one of four angles
representing vertical, horizontal and two diagonal directions

Canny Algorithm:

• Apply Gaussian filter to smooth the image in order to remove the noise
• Find the intensity gradients of the image
• Apply non-maximum suppression to get rid of spurious response to edge detection
• Apply double threshold to determine potential edges
• Track edge by hysteresis: Finalize the detection of edges by suppressing all the
other edges that are weak and not connected to strong edges.

CODE FOR CANNY OPERATOR:

newImg = imread('cameraman.tif'); %% create a new figure to show


the image.
figure(1);
imshow(newImg);
Comparison of Image Segmentation Operators

Operators Comparative Parameter Advantages Disadvantages


Study
1. Sobel It produces thin Threshold It is simple. Better Sensitivity to
edges. It does not approximation to Noise. Not accurate
offer detailed gradient magnitude. in locating edges.
information. Accuracy descends
when magnitude of
the edges decreases.

2. Prewitt More sensitive to Threshold Detection of edges Inaccurate. Size of


horizontal and and their the coefficient and
vertical edges. orientations are kernel filter is fixed
high. and cannot be
changed to a given
image.

3. Roberts Works with binary Threshold It is simple and fast. Localization is not
images only. Does Detects thicker good.Weak re-
not detect edges edges. sponse to genuine
when a minimal edge.
change in gray scale
value.

4. Canny It operates more better Double It provides fine It is time


than other three.It Threshold details. Consuming
operates on horizontal, due to its
vertical and diagonal complex
edges. Computation.

Conclusion
From this experiment we have learnt how to detect edges in the digital image using
different edge detecting operators.
Hence we can comment on their performance as :
Robert<Prewitt<Sobel<Canny.
OUTPUTS

Original Image After applying Robert

Original Image After applying Prewitt


Original Image After applying Sobel

Original Image After applying Canny

S-ar putea să vă placă și