Gaussian Filtering is widely used in the field of image processing. It is used to reduce the noise of an image. In this article we will generate a 2D Gaussian Kernel. The 2D Gaussian Kernel follows the below given Gaussian Distribution.
Where y is the distance along the vertical axis from the origin, x is the distance along the horizontal axis from the origin, and ? is the standard deviation.
What is Gaussian Filtering?
Gaussian Filtering is a technique used in image processing to smooth images and reduce noise. It works by applying a blur effect using a mathematical function called the Gaussian function, which gives more weight to the central pixels and less to the surrounding ones. This results in a natural-looking blur that helps remove unwanted details like grain or small artifacts. Gaussian filtering is widely used as a preprocessing step in tasks like edge detection, object recognition, and image enhancement, making it easier for algorithms to focus on important features.
Implementation in C++
// C++ program to generate Gaussian filter
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
// Function to create Gaussian filter
void FilterCreation(double GKernel[][5])
{
// initialising standard deviation to 1.0
double sigma = 1.0;
double r, s = 2.0 * sigma * sigma;
// sum is for normalization
double sum = 0.0;
// generating 5x5 kernel
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
r = sqrt(x * x + y * y);
GKernel[x + 2][y + 2] = (exp(-(r * r) / s)) / (M_PI * s);
sum += GKernel[x + 2][y + 2];
}
}
// normalising the Kernel
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
GKernel[i][j] /= sum;
}
// Driver program to test above function
int main()
{
double GKernel[5][5];
FilterCreation(GKernel);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j)
cout << GKernel[i][j] << "\t";
cout << endl;
}
}
Output:
0.00296902 0.0133062 0.0219382 0.0133062 0.00296902
0.0133062 0.0596343 0.0983203 0.0596343 0.0133062
0.0219382 0.0983203 0.162103 0.0983203 0.0219382
0.0133062 0.0596343 0.0983203 0.0596343 0.0133062
0.00296902 0.0133062 0.0219382 0.0133062 0.00296902
Real-world Applications of Gaussian Filtering
Gaussian filters are used in many everyday technologies to improve image quality and extract useful information:
- Computer Vision: Helps detect edges and shapes by reducing noise before applying detection algorithms.
- Medical Imaging: Used to smooth MRI or CT scans, making it easier to identify tissues and abnormalities.
- Object Detection: Prepares images by removing distractions, allowing models to focus on key features.
- Photo Editing Tools: Commonly used to apply blur effects, soften images, or reduce graininess for a cleaner look.
Comparison with Other Filters
Here’s how Gaussian filter stands out from other common filters:
- Box Filter (Average Filter): Blurs the image by giving equal weight to all surrounding pixels. Gaussian filter is better because it gives more weight to center pixels, creating a smoother, more natural blur.
- Median Filter: Replaces each pixel with the median of nearby values, which is great for removing salt-and-pepper noise. Unlike Gaussian, it doesn't blur the image as much but may distort edges.
- Bilateral Filter: Like Gaussian, but also considers pixel intensity differences, preserving edges while smoothing. It's more advanced but also more computationally heavy.
2D vs 1D Gaussian Filtering
A 2D Gaussian filter can be broken down into two 1D filters — one horizontal and one vertical. This is called separability, and it means we don’t need to apply a full 2D kernel at once.
Why it matters:
Instead of doing heavy calculations with a big 2D kernel (e.g., 5×5), we apply a 1D kernel horizontally, then the same kernel vertically. This cuts down the computation time and gives the same result.
Performance Considerations
Generating and applying a Gaussian kernel can be computationally expensive, especially for large images or kernels.
- Time Complexity:
- For a kernel of size k × k applied to an n × n image, the time complexity is O(n² × k²).
- This is because each pixel operation involves looping over the entire kernel.
- Optimization – Separable Filters:
Gaussian kernels are separable, meaning a 2D filter can be broken into two 1D filters: one horizontal and one vertical.- This reduces the time complexity to O(n² × k), making it much faster for larger kernels.
Using separable filters is a common trick in real-world systems to speed up Gaussian filtering without losing quality.
Must Read
Conclusion
Gaussian filtering is a simple yet powerful technique for reducing image noise and blurring using a smooth, weighted average based on the Gaussian function. In this article, we generated a 2D Gaussian kernel and explored its role in various real-world applications like computer vision, medical imaging, and photo editing. We also compared it with other filters and discussed ways to optimize performance using separable filters. Overall, Gaussian filtering is a foundational tool in image processing, helping improve image quality and making it easier for algorithms to focus on important visual details.