Applying Kirsch operator Tutorial

Advertisements


Kirsch compass masks are another type of derivative mask which are used for edge detection. This operator is also known as direction mask. In this operator we take one mask and rotate it in all the 8 compass major directions to get edges of the 8 directions.

We are going to use OpenCV function filter2D to apply Kirsch operator to images. It can be found under Imgproc package. Its syntax is given below:

filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

The arguments of the functions and their description is listed below:

Sr.NoArgument & Description
1src
Source image.
2dst
Destiantion image.
3ddepth:
The depth of dst. A negative value (such as -1) indicates that the depth is the same as the source.
4kernel
The kernel to be scanned through the image.
5anchor
The position of the anchor relative to its kernel. The location Point(-1, -1) indicates the center by default.
6delta
A value to be added to each pixel during the convolution. By default it is 0.
7BORDER_DEFAULT
We let this value by default.

Apart from the filter2D method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.

Example

The following example demonstrates the use of Imgproc class to apply Kirsch 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,-3);
         put(0,1,-3);
         put(0,2,-3);
         
		 put(1,0-3);
         put(1,1,0);
         put(1,2,-3);

         put(2,0,5);
         put(2,1,5);
         put(2,2,5);
      }
      };	      
      Imgproc.filter2D(source, destination, -1, kernel);
      Highgui.imwrite("output.jpg", destination);
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

Original Image

Applying Kirsch operator Tutorial

This original image has been convolved with the Kirsch operator of East edges , which is given below:

Kirsch East

-3-3-3
-30-3
555

Convolved Image(Kirsch East).

Applying Kirsch operator Tutorial

This original image has also been convolved with the Kirsch operator of South West edges , which is given below:

Kirsch South West

55-3
50-3
-3-3-3

Convolved Image (Kirsch South West).

Applying Kirsch operator Tutorial

Advertisements
Advertisements