Apply a Gauss filter to an image with Python
A Gaussian Filter is a low pass filter used for reducing noise (high frequency components) and blurring regions of an image. The filter is implemented as an Odd sized Symmetric Kernel (DIP version of a Matrix) which is passed through each pixel of the Region of Interest to get the desired effect. The kernel is not hard towards drastic color changed (edges) due to it the pixels towards the center of the kernel having more weightage towards the final value then the periphery. A Gaussian Filter could be considered as an approximation of the Gaussian Function (mathematics). In this article we will learn methods of utilizing Gaussian Filter to reduce noise in images using Python programming language.
We would be using the following image for demonstration:
Process to Apply a Gauss filter
In the process of using Gaussian Filter on an image we firstly define the size of the Kernel/Matrix that would be used for demising the image. The sizes are generally odd numbers, i.e. the overall results can be computed on the central pixel. Also the Kernels are symmetric & therefore have the same number of rows and column. The values inside the kernel are computed by the Gaussian function, which is as follows:
x → X coordinate value
y → Y coordinate value
???? → Mathematical Constant PI (value = 3.13)
σ → Standard Deviation
Using the above function a gaussian kernel of any size can be calculated, by providing it with appropriate values. A 3×3 Gaussian Kernel Approximation(two-dimensional) with Standard Deviation = 1, appears as follows
Implementing the Gaussian kernel in Python
We would be using PIL (Python Imaging Library) function named filter() to pass our whole image through a predefined Gaussian kernel. The function help page is as follows:
Takes in a kernel (predefined or custom) and each pixel of the image through it (Kernel Convolution).
Parameter: Filter Kernel
Return: Image Object
In the following example, we would be blurring the aforementioned image.
Firstly we imported the Image and ImageFilter (for using filter()) modules of the PIL library. Then we created an image object by opening the image at the path IMAGE_PATH (User defined). After which we filtered the image through the filter function, and providing ImageFilter.GaussianBlur (predefined in the ImageFilter module) as an argument to it. The kernel dimensions of ImageFilter.GaussianBlur is 5×5. In the end we displayed the image.
Note: The size of kernel could be manipulated by passing as parameter (optional) the radius of the kernel. This changes the following line from.
image = image.filter(ImageFilter.GaussianBlur)
image = image.filter(ImageFilter.GaussianBlur(radius=x))
where x => blur radius (size of kernel in one direction, from the center pixel)
Blurring a small region in an image:
Instead of the whole image, certain sections of it could also be selectively blurred. This could be performed by firstly cropping the desired region of the image, and then passing it through the filter() function. The output of which (the blurred sub image) would be pasted on top of the original image. This would give us the desired output.
The code for which is as follows: