Java Program to Blur Images using OpenCV
Last Updated :
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 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
Similar Reads
Java Program to Blur Image using Smoothing
Blurring is a simple and frequently used image processing operation. It is also called as Smoothing. 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 unda
4 min read
Rotating Images using OpenCV in Java
Image rotation is a common image processing routine used to rotate images at any desired angle. This helps in image reversal, flipping, and obtaining an intended view of the image. Image rotation has applications in matching, alignment, and other image-based algorithms. OpenCV is a well-known librar
6 min read
Java Program to Convert PNG Images to JPEG
PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each oth
4 min read
Java Program to Add Text to an image in OpenCV
OpenCV is the cross-platform open-source library for computer vision, image processing, and machine learning. Â It plays a major role nowadays in real-time operations in improving modules provides adequate functions for image manipulation. It has C++, C, Python, and Java interfaces and supports Windo
3 min read
Java Program to Crop Image Using BufferedImage Class
In the Java programming language, we need some classes to crop an image. So these classes are as follows: 1. To read and write an image file we have to import the File class. This class represents file and directory path names in general. import java.io.File 2. To handle errors we use the IOExceptio
2 min read
Java OpenCV Programs - Basic to Advanced
Java is a popular programming language that can be used to create various types of applications, such as desktop, web, enterprise, and mobile. Java is also an object-oriented language, which means that it organizes data and behaviour into reusable units called classes and objects. Java is known for
2 min read
Java Program to Copy and Paste an image in OpenCV
OpenCV is a Machine Learning and open-source computer vision software library, the main purpose for which it was developed is to enable a common medium to increase the use of machine perception in commercial businesses and accelerate the development of computer vision applications, it is a smooth an
3 min read
Java Program to Create Grayscale Image
The RGB model where red, green and blue lights are added in various different intensities and permutations and combinations of them producing broad spectrum array of colors whereas grayscale refers to the white and black colors only where the weakest intensity gives birth to black color and stronges
4 min read
Java Program to Increase or Decrease Brightness of an Image
Before understanding how the brightness is adjusted in any image first we have to understand how an image is represented. image is represented in the 2D form of a pixel, a pixel is the smallest element of an image. the value of pixel at any point corresponds to the intensity of light of the photons
3 min read
Java Applet | Implementing Flood Fill algorithm
Flood Fill Algorithm is to replace a certain closed or a similarly colored field with a specified color. The use of the FloodFill algorithm can be seen in paints and other games such as minesweeper.In this article, FloodFill is used for a connected area by a specified color, in Java Applet by using
5 min read