Image processing with Python 100 knocks # 9 Gaussian filter

Introduction

Hi, I'm Ramu. This time, we will implement a Gaussian filter that removes noise in the image.

9th: Gaussian filter

The Gaussian filter is a filter that smoothes images. By applying this filter, you can make the entire image look blurry.

In this filter, the peripheral pixels of the pixel of interest are weighted by a Gaussian distribution, and the closer to the center pixel of the filter, the greater the weight. Weighting by Gaussian distribution is defined as follows.

g(x,y) = \frac{1}{2\pi\sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}

For example, the following filters are often used for 3x3 and 5x5 Gaussian filters. Probably, if you specify around $ σ = 0.85 $ for the argument s, it will be this value.

\frac{1}{16} \frac{2}{16} \frac{1}{16}
\frac{2}{16} \frac{4}{16} \frac{2}{16}
\frac{1}{16} \frac{2}{16} \frac{1}{16}
\frac{1}{256} \frac{4}{256} \frac{6}{256} \frac{4}{256} \frac{1}{256}
\frac{4}{256} \frac{16}{256} \frac{24}{256} \frac{16}{256} \frac{4}{256}
\frac{6}{256} \frac{24}{256} \frac{36}{256} \frac{24}{256} \frac{6}{256}
\frac{4}{256} \frac{16}{256} \frac{24}{256} \frac{16}{256} \frac{4}{256}
\frac{1}{256} \frac{4}{256} \frac{6}{256} \frac{4}{256} \frac{1}{256}

Assuming that the pixel of interest is the center, the sum of the products of the peripheral pixels and the corresponding filter values should be substituted for the pixel of interest. For a 3 × 3 filter, $ I (x_0, y_0) × \ frac {1} {16} + I (x_0, y_1) × \ frac {2} {16} + ... I (x_2, y_2) × Substitute the value of \ frac {1} {16} $ for the pixel of interest.

Also, since the edge of the image cannot be filtered, 0 is used for non-existent pixels. This is called 0 padding.

Source code

gaussianFilter.py


import numpy as np
import cv2
import matplotlib.pyplot as plt


def gaussianFilter(img,k,s):
  w,h,c = img.shape
  size = k // 2

  #0 padding process
  _img = np.zeros((w+2*size,h+2*size,c), dtype=np.uint8)
  _img[size:size+w,size:size+h] = img.copy().astype(np.uint8)
  dst = _img.copy()

  #Create filter
  ker = np.zeros((k,k), dtype=np.float)
  for x in range(-1*size,k-size):
    for y in range(-1*size,k-size):
      ker[x+size,y+size] = (1/(2*np.pi*(s**2)))*np.exp(-1*(x**2+y**2)/(2*(s**2)))

  ker /= ker.sum()

  #Filtering process
  for x in range(w):
    for y in range(h):
      for z in range(c):
        dst[x+size,y+size,z] = np.sum(ker*_img[x:x+k,y:y+k,z])

  dst = dst[size:size+w,size:size+h].astype(np.uint8)

  return dst


#Image reading
img = cv2.imread('image.jpg')

#Gaussian filter
#2nd argument: filter size, 3rd argument: standard deviation(σ)
img = gaussianFilter(img,21,2)

#Save image
cv2.imwrite('result.jpg', img)
#Image display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. You can see that the point noise is reduced.

in conclusion

If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. .. Also, since python is a beginner, please kindly point out any mistakes.

Recommended Posts

Image processing with Python 100 knocks # 9 Gaussian filter
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python 100 knock # 12 motion filter
Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Image processing with scikit-image
python image processing
Image processing 100 knocks ①
Basics of binarized image processing with Python
Image processing with Python & OpenCV [Tone Curve]
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Image processing with MyHDL
First Python image processing
Image Processing with PIL
Getting started with Python with 100 knocks on language processing
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Image processing by Python 100 knock # 11 smoothing filter (average filter)
100 Language Processing with Python Knock 2015
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Notes on HDR and RAW image processing with Python
Image editing with python OpenCV
Acoustic signal processing with Python (2)
100 image processing knocks !! (011 --020) Early game
Acoustic signal processing with Python
[Python] Filter spreadsheets with gspread
Sorting image files with Python (2)
Sorting image files with Python (3)
100 image processing knocks !! (001 --010) Carefully and carefully
Tweet with image in Python
Sorting image files with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
Image processing by python (Pillow)
[Chapter 2] Introduction to Python with 100 knocks of language processing
Image Processing Collection in Python
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Let's play with Python] Image processing to monochrome and dots
Exploring image filter parameters with blackbox optimization in Python Note
Cut out an image with python
[Python] Easy parallel processing with Joblib
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Let's do image scraping with Python
Find image similarity with Python + OpenCV
Gradation image generation with Python [1] | np.linspace
Median filter using xarray (median filter)
Image processing with Python 100 knock # 10 median filter
HTML email with image to send with python
Image processing by Python 100 knock # 1 channel replacement