Applying Gaussian Filter Tutorial

Advertisements


We are going to apply Gaussian filter that blurs an image.

We are going to use OpenCV function GaussianBlur to apply Gaussian filter to images. It can be found under Imgproc package. Its syntax is given below:

Imgproc.GaussianBlur(source, destination,Size,SigmaX);

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

Sr.NoArgument & Description
1source
Source image.
2destination
Destiantion image.
3Size
Gaussian kernel size.
4SigmaX
Gaussian kernel standard deviation in X direction.

Apart from the GaussianBlur 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 Gaussian filter to an image.

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;


public class Main {
   public static void main( String[] args )
   {
   try {
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      Mat source = Highgui.imread("digital_image_processing.jpg",
      Highgui.CV_LOAD_IMAGE_COLOR);
      Mat destination = new Mat(source.rows(),source.cols(),source.type());
      Imgproc.GaussianBlur(source, destination,new Size(45,45), 0);
      Highgui.imwrite("Gaussian45.jpg", destination);
      
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

Original Image

Applying Gaussian Filter Tutorial

When this original image is convolved with the Gaussain filter of size 11 and 45 ,the following output screen would appear.

Gaussian filter of size 11.

Applying Gaussian Filter Tutorial

Gaussian filter of size 45.

Applying Gaussian Filter Tutorial

Advertisements
Advertisements