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
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 ;
Mat src = new Mat();
Mat dst = new Mat();
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String filename = "D:\\InputImage.jpg" ;
src = Imgcodecs.imread(filename,
Imgcodecs.IMREAD_COLOR);
for ( int i = 1 ; i < MAX_KERNEL_LENGTH; i = i + 2 ) {
Imgproc.blur(src, dst, new Size(i, i),
new Point(- 1 , - 1 ));
displayDst(DELAY_BLUR);
}
for ( int i = 1 ; i < MAX_KERNEL_LENGTH; i = i + 2 ) {
Imgproc.GaussianBlur(src, dst, new Size(i, i),
0 , 0 );
displayDst(DELAY_BLUR);
}
for ( int i = 1 ; i < MAX_KERNEL_LENGTH; i = i + 2 ) {
Imgproc.medianBlur(src, dst, i);
displayDst(DELAY_BLUR);
}
for ( int i = 1 ; i < MAX_KERNEL_LENGTH; i = i + 2 ) {
Imgproc.bilateralFilter(src, dst, i, i * 2 ,
i / 2 );
displayDst(DELAY_BLUR);
}
}
}
|
Output: The output image is a blurred image of the corresponding input image:
Output Image
Share your thoughts in the comments
Please Login to comment...