Apply Median and Wiener Filters to Images in Python

In various cases, you have to denoise images to enhance their visual quality. This is useful when you want to improve the overall clarity of your images. Also, denoising is used to pre-process images before further analysis such as recognition, segmentation, and so on. Median and wiener filters are commonly used to denoise and smoothen the images. So let’s have a look at how to apply median and wiener filters on images in Python.

Python Library to Apply Median and Wiener Image Filters

To apply the median and wiener filters on images, we will use Aspose.Imaging for Python - a powerful image processing library that lets you manipulate images effortlessly. To use the library, you can either download or install it using the following command.

> pip install aspose-imaging-python-net 

Apply Median Filter to an Image in Python

The median filter is a commonly used denoising method that uses a nonlinear digital filtering technique. The following are the steps to apply a median filter to an image in Python.

  • First, load the image using Image.load() method.
  • Then, cast image to RasterImage type.
  • Create an instance of MedianFilterOptions class and initialize it with the size of the rectangle.
  • Apply the median filter using RasterImage.filter(Rectangle, MedianFilterOptions) method.
  • Finally, save the filtered image using RasterImage.save() method.

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

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

Apply Median Filter to Image Python

Apply Gauss Wiener Filter to an Image in Python

Gauss wiener is another commonly used method to enhance clarity and reduce noise in the images. Let’s have a look at the steps required to apply the Gauss wiener filter to an image in Python.

  • First, load the image using Image.load() method.
  • Then, cast image to RasterImage type.
  • Create an instance of GaussWienerFilterOptions class and initialize it with radius size and smooth value.
  • (Optional) To get a grayscale image, set GaussWienerFilterOptions.grayscale property to true.
  • Apply Gauss wiener filter using RasterImage.filter(Rectangle, GaussWienerFilterOptions) method.
  • Finally, save the resultant image using RasterImage.save() method.

The following code sample shows how to apply a Gauss wiener filter to an image in Python.

Below is the image before and after applying the Gauss wiener filter with the grayscale 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

Motion Wiener Filter for an Image in Python

Motion wiener filter is used to reduce the blurring or degradation caused by motion blur. This kind of blur happens because of the relative motion between the camera and the object. The following are the steps to apply the motion wiener filter to an image in Python.

  • First, load the image using Image.load() method.
  • Then, cast image to RasterImage type.
  • Create an instance of MotionWienerFilterOptions class and initialize it with length, smooth value, and angle.
  • Apply motion wiener filter using RasterImage.filter(Rectangle, MotionWienerFilterOptions) method.
  • Finally, save the resultant image using RasterImage.save() method.

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

Apply Motion Weiner Filter to Image in Python

Free Python Image Filter Library

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

Free Online Image Editing App

Use our free web-based image editing tool to edit your images online. This image editor utilizes Aspose.Imaging for Python and doesn’t asks you to create an account.

Conclusion

In this article, you have learned how to apply median and wiener filters to images in Python. The steps and code samples have demonstrated how to apply different types of filters to denoise images programmatically. In addition, we have covered how to reduce the noise of moving objects in an image using a motion wiener filter. Finally, we have provided you with an online image editing app that is completely free and you can use it without signing up.

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

See Also