【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.

Daisuke0209/preprocessing
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.

Lena

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.

Conclusion

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!

機械学習を勉強するためのオススメ参考書(理論・Python・Webアプリ)
統計・機械学習に関しては授業で習ったところありますが、結構忘れてしまっています(。また、Rの演習の授業はありましたが、Pythonは学生のころには使っていません。機械学習・データサイエンスを勉強するにあたり、私が参考にしている参考書を一挙ご紹介したいと思います。

コメント