Open In App

Java Program to Blur Images using OpenCV

Blurring is a simple and frequently used image processing operation. It is also called as Smoothing. OpenCV library provides many functions to apply diverse linear filters to smooth images or blur images.

Smoothing of an image removes noisy pixels from the image and applying a low-pass filter to an image. A low-pass filter means removing noise from an image while leaving the majority of the image undamaged. The most common type of filter is linear. In a linear filter, the weighted sum of the input pixels value determines the output pixels value.



OpenCV functions which can be used to blur an image are as follows:

Return type of above functions is modified image of the same directory which is supposed to be taken as sample input image. All functions described above are as follows



Method1. Blur() : This function performs smoothing using normalized block filter. 

Syntax:

Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));

Parameters: This function requires 4 arguments

Method 2. GaussianBlur(): This function performs smoothing using a Gaussian filter. 

Syntax:

Imgproc.GaussianBlur(src, dst, new Size(i, i), 0, 0);

Parameters: This function requires 4 arguments

Method 3. medianBlur(): This function performs smoothing using a Median filter. 

Syntax:

Imgproc.medianBlur(src, dst, i);

Parameters: This function requires 3 arguments

Method 4. bilateralFilter(): This function performs smoothing using a Bilateral filter. 

Syntax: 

Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);

Parameters: This function requires 5 arguments

Implementation: Input image is as follows which is supposed to be blurred out.

Input Image




// Importing OpenCV libraries
// to use inbuilt methods
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
 
class GFG {
 
    int DELAY_BLUR = 100;
    int MAX_KERNEL_LENGTH = 31;
 
    // Source Image by creating Matlab object
    Mat src = new Mat();
 
    // Destination Image by creating Matlab object
    Mat dst = new Mat();
 
    // Main driver code
    public static void main(String[] args)
    {
        // Load the native library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 
        // Taking input image from directory
        String filename = "D:\\InputImage.jpg";
        src = Imgcodecs.imread(filename,
                               Imgcodecs.IMREAD_COLOR);
 
        // 4 different methods of Imgproc class
        // to blur out input image
 
        // Method 1. Standard blur method
        // using blur()
        for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
            Imgproc.blur(src, dst, new Size(i, i),
                         new Point(-1, -1));
 
            // Display blurred input image
            displayDst(DELAY_BLUR);
        }
 
        // Method 2. Gaussian blur method
        // using GaussianBlur()
        for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
            Imgproc.GaussianBlur(src, dst, new Size(i, i),
                                 0, 0);
            // Display blurred input image
            displayDst(DELAY_BLUR);
        }
 
        // Method 3. Median blur method
        // using medianBlur()
        for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
            Imgproc.medianBlur(src, dst, i);
 
            // Display blurred input image
            displayDst(DELAY_BLUR);
        }
 
        // Method 4. Bilateral filter Method
        // using bilateralFilter()
        for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
            Imgproc.bilateralFilter(src, dst, i, i * 2,
                                    i / 2);
 
            // Display blurred input image
            displayDst(DELAY_BLUR);
        }
    }
}

 

 

Output: The output image is a blurred image of the corresponding input image:

 

Output Image

 


Article Tags :