You are on page 1of 6

LAB EXPERIMENT#08

Edge Detection

LAB OBJECTIVES:
The objective is to understand and implement the following edge detection techniques:
1. Sobel
2. Prewitt
3. Canny

Edge Detection:
Edge detection is an image processing technique for finding the boundaries of objects within images. It
works by detecting discontinuities in brightness. Edge detection is used for image segmentation and
data extraction in areas such as image processing, computer vision, and machine vision.
Common edge detection algorithms include Sobel, Canny, Prewitt and Roberts.
Sobel:
The Sobel operator performs a 2-D spatial gradient measurement on an image and so emphasizes
regions of high spatial frequency that correspond to edges. Typically it is used to find the approximate
absolute gradient magnitude at each point in an input grayscale image.

These kernels are designed to respond maximally to edges running vertically and horizontally relative to
the pixel grid, one kernel for each of the two perpendicular orientations. The kernels 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:

Typically, an approximate magnitude is computed using:

Which is much faster to compute.The angle of orientation of the edge (relative to the pixel grid) giving
rise to the spatial gradient is given by:
Example:
I = imread('circuit.tif');
imshow(I)
BW3 = edge(I,'Sobel');
figure, imshow(BW3)

ORIGINAL IMAGE SOBEL DETECTED IMAGE

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

 Horizontal edges
 Vertical Edges
Edges are calculated by using difference between corresponding pixel intensities of an image.

Since the Prewitt kernels can be decomposed as the products of an averaging and a differentiation
kernel, they compute the gradient with smoothing. Therefore, it is a separable filter. For example, Gxcan
be written as

The x-coordinate is defined here as increasing in the "right"-direction, and the y-coordinate is defined as
increasing in the "down"-direction. At each point in the image, the resulting gradient approximations
can be combined to give the gradient magnitude, using:
Using this information, we can also calculate the gradient's direction:

where, for example, Θ is 0 for a vertical edge which is darker on the right side.
CANNY:
Canny edge detection is a multi-step algorithm that can detect edges with noise supressed at the same
time.
1. Smooth image with a Gaussian
• optimizes the trade-off between noise filtering and edge localization
2. Compute the Gradient magnitude using approximations of partial derivatives
• 2x2 filters
3. Thin edges by applying non-maxima suppression to the gradient magnitude
4. Detect edges by double thresholding.

Task 1:
Apply Sobel detection technique on an image and note the results.Also attach the snapshots.
CODE:

A=imread('images.jpg');
subplot(131)
imshow(A)
title('Original Image')
B=rgb2gray(A);
subplot(132)
imshow(B)
title('Grayscale Image')
C=double(B);

for i=1:size(C,1)-2
for j=1:size(C,2)-2

Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));

Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
B(i,j)=sqrt(Gx.^2+Gy.^2);

end
end

subplot(133)
imshow(B);
title('Sobel gradient');

OUTPUT:

Task 2:
Apply Prewitt detection technique on an image and note the results. Also attach the snapshots
CODE:
Image = rgb2gray(imread('download (1).jpg'));
Image = im2double(Image);

b=[-1 -1 -1;0 0 0;1 1 1]/6;


c=[-1 0 1; -1 0 1; -1 0 1]/6;
Gx=abs(conv2(Image,c,'same'));
Gy=abs(conv2(Image,b,'same'));
G = sqrt( Gx.^2 + Gy.^2);
out = G > 0.08995;
figure;
imshow(out);

figure;
edge(openImage,'Prewitt', [], 'both', 'nothinning');

OUTPUT:
Task 3:
Apply Canny detection technique on an image and note the results. Also attach the snapshots.

You might also like