Apply Median and Wiener Filters to Images in Java

In the previous blog post, we demonstrated how to add the blur effect to the images. Today, we are going to show you how to apply median and wiener filters, which are commonly used to denoise and smoothen the images. So let’s see how to apply median and wiener filers to an image programmatically in Java.

Apply Median and Wiener Image Filters in Java - API Installation

To apply the median and wiener filters on images, we will use Aspose.Imaging for Java. It is a powerful image editing API to manipulate the images from within the Java applications. You can either download the API or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-imaging</artifactId>
    <version>22.9</version>
</dependency>

Apply Median Filter to an Image in Java

The median filter is a nonlinear digital filtering technique, which is a popular way to denoise the images. The following are the steps to apply a median filter to an image in Java.

The following code sample shows how to apply a median filter to an image in Java.

// Load the noisy image
Image image = Image.load("jack.jpg");
// caste the image into RasterImage
RasterImage rasterImage = (RasterImage) image;
if (rasterImage == null) {
return;
}
// Create an instance of MedianFilterOptions class and set the size.
MedianFilterOptions options = new MedianFilterOptions(4);
// Apply MedianFilterOptions filter to RasterImage object.
rasterImage.filter(image.getBounds(), options);
// Save the resultant image
image.save("Jac_median_denoise.jpg");

Below is the image before and after applying the median filter.

Apply Median Filter to Image Java

Apply Gauss Wiener Filter to an Image in Java

Gauss wiener is another commonly used method for image grading by minimizing additive noise and blurring. The following are the steps to apply the gauss wiener filter to an image in Java.

The following code sample shows how to apply a gauss wiener filter to an image in Java.

// Load the image
Image image = Image.load("jack.jpg");
// caste the image into RasterImage
RasterImage rasterImage = (RasterImage) image;
if (rasterImage == null) {
return;
}
// Create an instance of GaussWienerFilterOptions class and set the radius size and smooth value.
GaussWienerFilterOptions options = new GaussWienerFilterOptions(12, 3);
options.setGrayscale(true);
// Apply GaussWienerFilterOptions filter to RasterImage object.
rasterImage.filter(image.getBounds(), options);
// Save the resultant image
image.save("Jac_guass_weiner.jpg");

Below is the image before and after applying the gauss wiener filter with the greyscale option.

Apply Gauss Weiner Filter to Image Greyscaling

The following is the image before and after applying the gauss wiener filter without greyscaling.

Apply Gauss Weiner Color Filter to Image

Use Motion Wiener Filter for an Image in Java

Motion wiener filter is used to remove blurring of an image that is produced because of the moving objects. The following are the steps to apply the motion wiener filter to an image in Java.

The following code sample shows how to apply a motion wiener filter to an image in Java.

// Load the image
Image image = Image.load("jack.jpg");
// caste the image into RasterImage
RasterImage rasterImage = (RasterImage) image;
if (rasterImage == null) {
return;
}
// Create an instance of MotionWienerFilterOptions class and set the length, smooth value and angle.
MotionWienerFilterOptions options = new MotionWienerFilterOptions(10, 2, 10);
//options.setGrayscale(true);
// Apply MotionWienerFilterOptions filter to RasterImage object.
rasterImage.filter(image.getBounds(), options);
// Save the resultant image
image.save("Jac_motion_weiner.jpg");
Apply Motion Weiner Filter to Image in Java

Java Image Filtering API - Get a Free License

You can get a free temporary license and apply median and wiener filters to images without evaluation limitations.

Conclusion

In this article, you have learned how to apply median and wiener filters to images in Java. Furthermore, we have covered how to reduce the noise of moving objects in an image. You can easily use these features in your Java application to integrate image editing capabilities.

Read More

You can explore more about the Java image processing API using documentation. Also, you can share your queries with us via our forum.

See Also