Java DIP - Sobel Operator

Sobel operator is very similar to Prewitt operator. It is also a derivative mask and is used for edge detection. Sobel operator is used to detect two kinds of edges in an image: Vertical direction edges and Horizontal direction edges.
We are going to use OpenCV function filter2D to apply Sobel operator to images. It can be found under Imgproc package. Its syntax is given below −
filter2D(src, dst, depth , kernel, anchor, delta, BORDER_DEFAULT );

The function arguments are described below −
src
It is source image.
dst
It is destination image.
depth
It is the depth of dst. A negative value (such as -1) indicates that the depth is the same as the source.
kernel
It is the kernel to be scanned through the image.
anchor
It is the position of the anchor relative to its kernel. The location Point (-1, -1) indicates the center by default.
delta
It is a value to be added to each pixel during the convolution. By default it is 0.
BORDER_DEFAULT
We let this value by default.
Apart from the filter2D method, there are other methods provide by the Imgproc class. They are described briefly −
cvtColor(Mat src, Mat dst, int code, int dstCn)
It converts an image from one color space to another.
dilate(Mat src, Mat dst, Mat kernel)
It dilates an image by using a specific structuring element.
equalizeHist(Mat src, Mat dst)
It equalizes the histogram of a grayscale image.
filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta)
It convolves an image with the kernel.
GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
It blurs an image using a Gaussian filter.
integral(Mat src, Mat sum)
It calculates the integral of an image.
The following example demonstrates the use of Imgproc class to apply Sobel operator to an image of Grayscale.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class convolution {
   public static void main( String[] args ) {
   
      try {
         int kernelSize = 9;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         
         Mat source = Highgui.imread("grayscale.jpg",  Highgui.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         
         Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F) {
            {
               put(0,0,-1);
               put(0,1,0);
               put(0,2,1);

               put(1,0-2);
               put(1,1,0);
               put(1,2,2);

               put(2,0,-1);
               put(2,1,0);
               put(2,2,1);
            }
         };	      
         
         Imgproc.filter2D(source, destination, -1, kernel);
         Highgui.imwrite("output.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}
When you execute the given code, the following output is seen −
This original image is convolved with the Sobel operator of vertical edges, which is given below −
This original is convolved with the Sobel operator of horizontal edges, which is given below −

    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page