The Wayback Machine - https://web.archive.org/web/20240723213406/https://www.geeksforgeeks.org/edge-detection-using-prewitt-scharr-and-sobel-operator/
Open In App

Edge detection using Prewitt, Scharr and Sobel Operator

Last Updated : 24 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The discontinuity in the image brightness is called an edge. Edge detection is the technique used to identify the regions in the image where the brightness of the image changes sharply. This sharp change in the intensity value is observed at the local minima or local maxima in the image histogram, using the first-order derivative. 

The techniques used here in this article are first-order derivative techniques namely:

 Prewitt Operator

The Prewitt operator was developed by Judith M. S. Prewitt. Prewitt operator is used for edge detection in an image. Prewitt operator detects both types of edges, these are:

  • Horizontal edges or along the x-axis,
  • Vertical Edges or along the y-axis.

Wherever there is a sudden change in pixel intensities, an edge is detected by the mask. Since the edge is defined as the change in pixel intensities, it can be calculated by using differentiation. Prewitt mask is a first-order derivative mask. In the graph representation of Prewitt-mask’s result, the edge is represented by the local maxima or local minima.

Both the first and second derivative masks follow these three properties:

  • More weight means more edge detection.
  • The opposite sign should be present in the mask. (+ and -)
  • The Sum of the mask values must be equal to zero.

Prewitt operator provides us two masks one for detecting edges in the horizontal direction and another for detecting edges in a vertical direction.

Prewitt Operator [X-axis] = [ -1 0 1; -1 0 1; -1 0 1]

Prewitt Operator [Y-axis] = [-1 -1 -1; 0 0 0; 1 1 1]

Steps:

  • Read the image.
  • Convert into grayscale if it is colored.
  • Convert into the double format.
  • Define the mask or filter.
  • Detect the edges along X-axis.
  • Detect the edges along Y-axis.
  • Combine the edges detected along the X and Y axes.
  • Display all the images.

Imtool() is the inbuilt function in Matlab. It is used to display the image. It takes 2 parameters; the first is the image variable and the second is the range of intensity values. We provide an empty list as the second argument which means the complete range of intensity has to be used while displaying the image.

Example: 

Matlab




% MATLAB code for prewitt
% operator edge detection
k=imread("logo.png");
k=rgb2gray(k);
k1=double(k);
p_msk=[-1 0 1; -1 0 1; -1 0 1];
kx=conv2(k1, p_msk, 'same');
ky=conv2(k1, p_msk', 'same');
ked=sqrt(kx.^2 + ky.^2);
 
% display the images.
imtool(k,[]);
 
% display the edge detection along x-axis.
imtool(abs(kx), []);
 
% display the edge detection along y-axis.
imtool(abs(ky),[]);
 
% display the full edge detection.
imtool(abs(ked),[]);


Output:

 

Scharr Operator

This is a filtering method used to identify and highlight gradient edges/features using the first derivative. Performance is quite similar to the Sobel filter.

Scharr Operator [X-axis] = [-3 0 3; -10 0 10; -3 0 3];

Scharr Operator [Y-axis] = [ 3 10 3; 0 0 0; -3 -10 -3];

Example:

Matlab




%Scharr operator -> edge detection
 k=imread("logo.png");
 k=rgb2gray(k);
 k1=double(k);
 s_msk=[-3 0 3; -10 0 10; -3 0 3];
 kx=conv2(k1, s_msk, 'same');
 ky=conv2(k1, s_msk', 'same');
 ked=sqrt(kx.^2 + ky.^2);
 %display the images.
 imtool(k,[]);
 %display the edge detection along x-axis.
 imtool(abs(kx), []);
 %display the edge detection along y-axis.
 imtool(abs(ky),[]);
 %display the full edge detection.
 imtool(abs(ked),[]);


 
 

Output:

 

 

 

 

 

Sobel Operator

It is named after Irwin Sobel and Gary Feldman. Like the Prewitt operator Sobel operator is also used to detect two kinds of edges in an image:

 

  • Vertical direction
  • Horizontal direction

The difference between Sobel and Prewitt Operator is that in Sobel operator the coefficients of masks are adjustable according to our requirement provided they follow all properties of derivative masks.

 

Sobel-X Operator = [-1 0 1; -2 0 2; -1 0 1]

Sobel-Y Operator = [-1 -2 -1; 0 0 0; 1 2 1]

Example:

 

Matlab




% MATLAB code for Sobel operator
% edge detection
 k=imread("logo.png");
 k=rgb2gray(k);
 k1=double(k);
 s_msk=[-1 0 1; -2 0 2; -1 0 1];
 kx=conv2(k1, s_msk, 'same');
 ky=conv2(k1, s_msk', 'same');
 ked=sqrt(kx.^2 + ky.^2);
  
 %display the images.
 imtool(k,[]);
  
 %display the edge detection along x-axis.
 imtool(abs(kx), []);
  
 %display the edge detection along y-axis.
 imtool(abs(ky),[]);
  
 %display the full edge detection.
 imtool(abs(ked),[]);


Output:

 

 



Similar Reads

MATLAB - Image Edge Detection using Prewitt Operator from Scratch
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. I
3 min read
MATLAB - Image Edge Detection using Sobel Operator from Scratch
Sobel Operator: It is a discrete differentiation gradient-based operator. It computes the gradient approximation of image intensity function for image edge detection. At the pixels of an image, the Sobel 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
3 min read
MATLAB - Image Edge Detection using Robert Operator from Scratch
Robert Operator: This gradient-based operator computes the sum of squares of the differences between diagonally adjacent pixels in an image through discrete differentiation. Then the gradient approximation is made. It uses the following 2 x 2 kernels or masks – [Tex]\[M_{x}=\left[\begin{array}{ccc}1 & 0 \\ 0 & -1\end{array}\right] \quad M_{
2 min read
Edge detection using first derivative operator in MATLAB
An edge in an image is a significant local change in image intensity, usually associated with a discontinuity in image intensity or the first derivative of image intensity. The discontinuities in the intensity of the image can be stepped discontinuities, in which the intensity of the image changes from one value on one side of the discontinuity to
3 min read
Edge detection using in-built function in MATLAB
Edge detection: In an image, an edge is a curve that follows a path of rapid change in intensity of that image. Edges are often associated with the boundaries of the object in a scene environment. Edge detection is used to identify the edges in an image to make image processing easy. Edge detection works by detecting discontinuities in brightness.
2 min read
Matlab | Edge Detection of an image without using in-built function
Problem Statement: Write a matlab code for edge detection of a grayscale image without using in-built function of edge detection. About 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 segmentatio
3 min read
How to Use & and && Operator in MATLAB?
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. Here we see & and
3 min read
How To Use | and || Operator in MATLAB?
In MATLAB, | and || are both logical operators that are used to perform logical OR operations on Boolean variables, however, there is a subtle difference between the two: ||| The element-wise logical OR operator "|" takes two arrays of the same size and returns an array of the same size where each element is the result of the logical OR operation b
4 min read
How to Use Logical Operator Within If Statements in MATLAB?
Logical Operators are used to combining two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a boolean value either true or false. Like any other programming language, logical operators in MATLAB are beneficial, and in this article, we wil
4 min read
Operator Overloading in MATLAB
MATLAB allows you to specify more than one definition for an operator in the same scope which is called Operator Overloading. We can redefine or overload most of the built-in operators available in MATLAB. It is basically a type of polymorphism in which an operator is overloaded to give user-defined meaning to it. By implementing operators that are
3 min read
three90RightbarBannerImg