【Python】Averaging and Gaussian Filtering of Images

gaussian filter

A pre-processing is required to extract edges and corners from local areas of the images. This pre-processing is called spatial filtering. Spatial filtering is important process in convolutional nueral network. Today i introduce averaging filter and Gaussian filter what are one of the spatial filtering. Python scripts are as bellow.

Contribute to Daisuke0209/preprocessing development by creating an account on GitHub.

Mathematical definition of spatial filterings

Spatial filtering can be classified into linear and nonlinear filtering. Here we consider linear filtering. Let \((x,y)\) be the coordinate of pixel, I be the input image, \({\tilde I}\) be the output image and \((2N+1)×(2N+1)\) be the size of the filter. Then the spatial filter is as bellow.

\[{\tilde I}(x,y)=\sum_{j=-N}^{N} \sum_{j=-N}^{N} F(i,j)I(x+i,y+j) (Correlation)\]

\[{\tilde I}(x,y)=\sum_{j=-N}^{N} \sum_{j=-N}^{N} F(i,j)I(x-i,y-j) (Convolution)\]

F is called filter or Kernel. If F is symmetric, correlation and convolution is same calculation.

[Python]Average filtering

First we will try to use the average filtering. We can use google colaboratory.

Next we import libraries

Then we use the picuture of Lena which is very famous in image recognision.


Next we make the image monochrome.


【Python】Correlation and Convolution Function

We make the correlation and convolution function by Python.

Then by using the correlation function, we apply the averaging filter to the image.

averaging filterIt can be seen that the image is blurred. This process is caleed smoothing.

【Python】Gaussian Filter

Second we will try to use the Gaussian filter. We make functions to make the Gaussian kernel.

Then  by using the correlation function, we apply the averaging filter to the image.

gaussian filterThis image is also blurred.


In this article, I introduce the average filter and Gaussian filter which are spatial filter. Filtered images are blurred both.

In the bellow article, I introduce the references to study Machine Learning.Please see it too!