0% found this document useful (0 votes)
17 views4 pages

CVR

CVR WEEK 2

Uploaded by

aimlds.python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

CVR

CVR WEEK 2

Uploaded by

aimlds.python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

A screenshot of a segment of windows explorer

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:

2 Dimensional gaussian function

Where,

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:

Syntax: Filter(Kernel)

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.

# ImageFilter for using filter() function


from PIL import Image, ImageFilter

# Opening the image


# (R prefixed to string in order to deal with '\' in paths)
image = Image.open(r"IMAGE_PATH")

# Blurring image by sending the ImageFilter.


# GaussianBlur predefined kernel argument
image = image.filter(ImageFilter.GaussianBlur)

# Displaying the image


image.show()
Output:

Blurred Image

Explanation:

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)

to

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:

from PIL import Image, ImageFilter

image = Image.open(r"FILE_PATH")

# Cropping the image


smol_image = image.crop((0, 0, 150, 150))
# Blurring on the cropped image
blurred_image = smol_image.filter(ImageFilter.GaussianBlur)

# Pasting the blurred image on the original image


image.paste(blurred_image, (0,0))

# Displaying the image


image.save('output.png')
Output:

Only the top left region of the image blurred

Spatial Filtering and its Types

Spatial Filtering technique is used directly on pixels of an image. Mask is usually


considered to be added in size so that it has specific center pixel. This mask is
moved on the image such that the center of the mask traverses all image pixels.

Classification on the basis of Linearity


There are two types:

1. Linear Spatial Filter


2. Non-linear Spatial Filter

General Classification:
Smoothing Spatial Filter
Smoothing filter is used for blurring and noise reduction in the image. Blurring is
pre-processing steps for removal of small details and Noise Reduction is
accomplished by blurring.

Types of Smoothing Spatial Filter

1. Linear Filter (Mean Filter)


2. Order Statistics (Non-linear) filter

These are explained as following below.

Mean Filter: Linear spatial filter is simply the average of the pixels contained in
the neighborhood of the filter mask. The idea is replacing the value of every pixel
in an image by the average of the grey levels in the neighborhood define by the
filter mask. Below are the types of mean filter:
Averaging filter: It is used in reduction of the detail in image. All coefficients
are equal.
Weighted averaging filter: In this, pixels are multiplied by different
coefficients. Center pixel is multiplied by a higher value than average filter.
Order Statistics Filter: It is based on the ordering the pixels contained in the
image area encompassed by the filter. It replaces the value of the center pixel
with the value determined by the ranking result. Edges are better preserved in this
filtering. Below are the types of order statistics filter:
Minimum filter: 0th percentile filter is the minimum filter. The value of the
center is replaced by the smallest value in the window.
Maximum filter: 100th percentile filter is the maximum filter. The value of the
center is replaced by the largest value in the window.
Median filter: Each pixel in the image is considered. First neighboring pixels are
sorted and original values of the pixel is replaced by the median of the list.
Sharpening Spatial Filter
It is also known as derivative filter. The purpose of the sharpening spatial filter
is just the opposite of the smoothing spatial filter. Its main focus in on the
removal of blurring and highlight the edges. It is based on the first and second
order derivative.

First Order Derivative:

Must be zero in flat segments.


Must be non zero at the onset of a grey level step.
Must be non zero along ramps.
First order derivative in 1-D is given by:

f' = f(x+1) - f(x)


Second Order Derivative:

Must be zero in flat areas.


Must be non zero at the onset and end of a ramp.
Must be zero along ramps.
Second order derivative in 1-D is given by:

f'' = f(x+1) + f(x-1) - 2f(x)

You might also like