You are on page 1of 6

ADSIP LAB (EC-18021)

SESSION 2020-2021

LAB REPORTSUBMITTED

By

Arnimesh Kumar Singh


(20185037)
EC-1
Experiment-5
Aim: Write a program to detect the edges of a colored image using Prewitt operators and
verify the results using the inbuilt commands.

Tool & Apparatus Used: MATLABr2021

Theory:

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.
All the masks that are used for edge detection are also known as derivative masks. Because as
we have stated many times before in this series of tutorials that image is also a signal so
changes in a signal can only be calculated using differentiation. So that’s why these operators
are also called as derivative operators or derivative masks.
All the derivative masks should have the following properties:

 Opposite sign should be present in the mask.


 Sum of mask should be equal to zero.
 More weight means more edge detection.
Prewitt operator provides us two masks one for detecting edges in horizontal direction and
another for detecting edges in a vertical direction.
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

-1 -1 -1

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.

MATLAB Code:
clc;
close all;
input_image = imread('flower.jpg’);

% Displaying Input Image


input_image = uint8(input_image);
figure, imshow(input_image); title('Input Image');

% Convert the truecolor RGB image to the grayscale image


input_image = rgb2gray(input_image);

% Convert the image to double


input_image = double(input_image);

% Pre-allocate the filtered_image matrix with zeros


filtered_image = zeros(size(input_image));
% Prewitt Operator Mask
Mx = [-1 0 1; -1 0 1; -1 0 1];
My = [-1 -1 -1; 0 0 0; 1 1 1];

% Edge Detection Process

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

% Gradient approximations
Gx = sum(sum(Mx.*input_image(i:i+2, j:j+2)));
Gy = sum(sum(My.*input_image(i:i+2, j:j+2)));

% Calculate magnitude of vector


filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);

end
end

% Displaying Filtered Image


filtered_image = uint8(filtered_image);
figure, imshow(filtered_image); title('Filtered Image');

% Define a threshold value


thresholdValue = 100; % varies between [0 255]
output_image = max(filtered_image, thresholdValue);
output_image(output_image == round(thresholdValue)) = 0;

% Displaying Output Image


output_image = im2bw(output_image);
figure, imshow(output_image); title('Edge Detected Image');

K = edge(input_image,'Prewitt');
figure, imshow(K);
title('Edge detected using inbuilt commands');

Output:-

Original Image

Filtered Image
Edge Detected Image

Result:
Successfully verified and detected the edges of a colored image using Prewitt
operators.
.

You might also like