Java Program to Blur Images using OpenCV - GeeksforGeeks

17 May, 2021
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:
blur()
GaussianBlur()
medianBlur()
bilateralFilter()
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
src: Source image
dst: Destination image
Size( w, h ): Size of the kernel of width w pixels and height h pixels
Point(-1, -1): Indicates the anchor point location with respect to the neighborhood. The center of the kernel is considered the anchor point if there is a negative value.
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
src: Source image
dst: Destination image
Size(w, h): The size of the kernel to be used. The size will be calculated using the σx and σy arguments if w and h are not odd and positive numbers.
σx, σy: The standard deviation in x and y. Writing 0 implies that σx and σy is calculated using kernel size.
Method 3. medianBlur(): This function performs smoothing using a Median filter. 
Syntax:
Imgproc.medianBlur(src, dst, i);
Parameters: This function requires 3 arguments
src: Source image
dst: Destination image
i: Size of the kernel. It must be odd.
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
src: Source image
dst: Destination image
d: The diameter of each pixel neighborhood.
σColor: Standard deviation in the color space.
σSpace: Standard deviation in the coordinate space (in pixel terms)
Implementation: Input image is as follows which is supposed to be blurred out.
Input Image
Java
// Importing OpenCV libraries// to use inbuilt methodsimport 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
 
simmytarika5
Java-OpenCV
OpenCV
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character