Applying Gaussian Filter in MATLAB to Noisy Images
Applying Gaussian Filter to Noisy Image

If you’re a MATLAB enthusiast or just starting out with image processing, you’ve likely stumbled upon the Gaussian filter. Maybe you’ve heard whispers of it in the MATLAB community or seen it mentioned in image enhancement techniques. Well, buckle up because today, we’re diving deep into how to Apply Gaussian filter in MATLAB By the end, you’ll be a pro at not only understanding Gaussian filters but also implementing them step-by-step in MATLAB with user input. πŸš€


What Is a Gaussian Filter?

The Gaussian filter is a fundamental tool in image processing. It’s a type of low-pass filter that’s super effective at reducing noise and smoothing an image while preserving important details like edges.

Formula of Gaussian Filter


At its core, the Gaussian filter uses the famous Gaussian function:

    \[G(x,y) = \frac{2\pi}{\sigma^2} e^{-\frac{x^2 + y^2}{2\sigma^2}}\]

Here:

  • G(x,y) is the filter value at point (x,y),
  • \sigma (sigma) controls the “spread” or width of the filter,
  • e is the mathematical constant.

In simpler terms:

  • The Gaussian filter applies a bell-shaped curve (think of the classic “Gaussian bell curve“) to blur or smooth the image.
  • The larger the \sigma, the more smoothing.

Why Use a Gaussian Filter?

Let’s address the big question: why should you care about Gaussian filters?

  1. Noise Reduction:
    Images often come with noise (unwanted pixel variations). The Gaussian filter smooths these variations without destroying the image’s structure. πŸ™Œ
  2. Preprocessing for Other Filters:
    Many advanced image processing techniques (like edge detection) perform better after an image is smoothed with a Gaussian filter.
  3. Human-like Smoothing:
    Gaussian smoothing mimics how our eyes naturally perceive blurred edges, making images look more natural.

Parameters for the Gaussian Filter

Before diving into the code, let’s clarify the key parameters you’ll need to set:

  • Sigma \sigma: Determines how much blurring occurs.
  • Kernel Size: Determines the dimensions of the filter matrix (e.g., 3×3, 5×5, etc.).

Both of these parameters significantly influence the output, so you’ll want to adjust them based on your application.


Step-by-Step Tutorial: Apply Gaussian Filter in MATLAB

Ready to get your hands dirty? 🧀 Follow this detailed guide to apply Gaussian filter in MATLAB.

Step 1: Import an Image

We’ll start by loading an image into MATLAB. Ensure the image is in your current directory.


Step 2: Get Parameters from the User

Ask the user for the Gaussian filter’s sigma and kernel size.


Step 3: Apply the Gaussian Filter

Now, we’ll create a Gaussian kernel using MATLAB’s built-in functions and apply it to the image.


Step 4: Save and Compare

Save the filtered image for future use and compare it side-by-side with the original.


Apply Gaussian Filter in MATLAB to Noisy Images & Results

So, I have utilized noisy images generated from Adding Noise to Image in MATLAB. The resultant Images were saved into their respective files and passed in the program one by one, the images were generated by applying three noise filters.

  • Gaussian Noise
  • Salt & Pepper Noise
  • Speckle Noise

For the sake of Simplicity, the Parameters I have chosen are value of Sigma to be 0.5 and Kernel Size to be 3. (we won’t get the ideal result, but we can see the difference)

The Input Image and their Respective Gaussian Filter Application can be seen from results shown below in form of images.

Original Image

This was the image that was the original Image, and all the noise filters were applied to this image, to learn how to apply these filters you can check it out here

original image to grayscale
original image

Image with Gaussian Noise Present

Image affected by Gaussian Noise Filter
Image affected by Gaussian Noise Filter
Gaussian Noisy Image filtered with Gaussian Filter
Gaussian Noisy Image filtered with Gaussian Filter

Image with Salt & Pepper Noise Present

Image affected by Salt and Pepper Noise Filter
Image affected by Salt and Pepper Noise Filter
Salt and Pepper Noisy Image filtered with Gaussian Filter
Salt and Pepper Noisy Image filtered with Gaussian Filter

Image with Speckle Noise Present

Image affected by Speckle Noise Filter
Image affected by Speckle Noise Filter
Speckle Noisy Image filtered with Gaussian Filter
Speckle Noisy Image filtered with Gaussian Filter

The above images show the comparison between the noisy images and the filtered Images, hopefully you can see the difference, further noise can be reduced by tweaking the parameters or using another filter

The Program also creates comparison Images in the end if you run it locally haven’t attached them but it’s the same as the Images given above.

Full MATLAB Code: From Input to Output

Here’s the entire code snippet for convenience:


Pro Tips when we Apply Gaussian Filter in MATLAB

  1. Experiment with Sigma: Start with small values (like 0.5 or 1) and gradually increase to see the effect on smoothing.
  2. Use Grayscale for Simplicity: If working with a color image is too complex, convert it to grayscale using rgb2gray().
  3. Automate for Batch Processing: Want to process multiple images? Use a loop and the same code to handle an entire folder.

If you need to discover more blogs related to MATLAB do so by checking out Implementation of MMSE and Adaptive Median Filter and Add Noise to Image in MATLAB: A Comprehensive Guide πŸŽ‰


Common Questions About Gaussian Filters in MATLAB

1. What’s the Best Sigma Value?

There’s no β€œbest” value it depends on your application. A smaller value focuses on minor noise, while a larger value achieves heavy smoothing. We can see from the implementation example above all of them require different values, if we use the same value there is not winner thus proving the point there is no best value.

2. Why Must Kernel Size Be Odd?

An odd-sized kernel ensures the filter has a center pixel for symmetry, which is essential for accurate filtering.

3. Can I Use Gaussian Filters for Edge Detection?

Yes, but indirectly. Gaussian filters are often used to preprocess an image before applying edge-detection algorithms like Sobel or Canny.


Wrap-Up: Master Gaussian Filtering in MATLAB

Applying a Gaussian filter in MATLAB isn’t just a great way to smooth images, it’s also a steppingstone to advanced image processing techniques. Whether you’re cleaning up noisy photos, prepping for edge detection, or just experimenting, this guide has got you covered! πŸ₯³

Now it’s your turn. Try out the code, tweak the parameters, and watch your images transform. If you have questions, drop them in the comments below. Happy coding! πŸš€

Thank you sooooo much for Reading this blog on Application of Gaussian Filter in MATLAB ❀️❀️❀️

Leave a Reply

Your email address will not be published. Required fields are marked *