MATLAB – Image Edge Detection using Prewitt Operator from Scratch

17 May, 2020
Prewitt Operator: It is a gradient-based operator. It is one of the best ways to detect the orientation and magnitude of an image. It computes the gradient approximation of image intensity function for image edge detection. At the pixels of an image, the Prewitt operator produces either the normal to a vector or the corresponding gradient vector. It uses two 3 x 3 kernels or masks which are convolved with the input image to calculate approximations of the derivatives – one for horizontal changes, and one for vertical –
    
Approach:
Step 1: Input – Read an imageStep 2: Convert the true-color RGB image to the grayscale imageStep 3: Convert the image to doubleStep 4: Pre-allocate the filtered_image matrix with zerosStep 5: Define Prewitt Operator MaskStep 6: Edge Detection Process (Compute Gradient approximation and magnitude of vector)Step 7: Display the filtered imageStep 8: Thresholding on the filtered imageStep 9: Display the edge-detected image
Implementation in MATLAB:
% MATLAB Code | Prewitt Operator from Scratch  % Read Input Imageinput_image = imread('[name of input image file].[file format]');  % Displaying Input Imageinput_image = uint8(input_image);figure, imshow(input_image); title('Input Image');  % Convert the truecolor RGB image to the grayscale imageinput_image = rgb2gray(input_image);  % Convert the image to doubleinput_image = double(input_image);  % Pre-allocate the filtered_image matrix with zerosfiltered_image = zeros(size(input_image));  % Prewitt Operator MaskMx = [-1 0 1; -1 0 1; -1 0 1];My = [-1 -1 -1; 0 0 0; 1 1 1];  % Edge Detection Process% When i = 1 and j = 1, then filtered_image pixel  % position will be filtered_image(2, 2)% The mask is of 3x3, so we need to traverse % to filtered_image(size(input_image, 1) - 2%, size(input_image, 2) - 2)% Thus we are not considering the borders.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);             endend  % Displaying Filtered Imagefiltered_image = uint8(filtered_image);figure, imshow(filtered_image); title('Filtered Image');  % Define a threshold valuethresholdValue = 100; % varies between [0 255]output_image = max(filtered_image, thresholdValue);output_image(output_image == round(thresholdValue)) = 0;  % Displaying Output Imageoutput_image = im2bw(output_image);figure, imshow(output_image); title('Edge Detected Image');
Input Image –
Filtered Image:
Edge Detected Image:
Advantages:
Good performance on detecting vertical and horizontal edgesBest operator to detect the orientation of an image
Good performance on detecting vertical and horizontal edges
Best operator to detect the orientation of an image
Limitations:
The magnitude of coefficient is fixed and cannot be changedDiagonal direction points are not preserved always
The magnitude of coefficient is fixed and cannot be changed
Diagonal direction points are not preserved always
Image-Processing
MATLAB
Advanced Computer Subject
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.