Image processing by Python 100 knock # 11 smoothing filter (average filter)

Introduction

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

11th: Smoothing filter (average filter)

A smoothing filter is a filter that smoothes an image. By applying this filter, you can make the entire image look blurry.

This filter replaces the pixel of interest with the average value of the peripheral pixels. For example, a 3x3 or 5x5 average filter looks like this:

\frac{1}{9} \frac{1}{9} \frac{1}{9}
\frac{1}{9} \frac{1}{9} \frac{1}{9}
\frac{1}{9} \frac{1}{9} \frac{1}{9}
\frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25}
\frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25}
\frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25}
\frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25}
\frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25} \frac{1}{25}

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} {9} + I (x_0, y_1) × \ frac {1} {9} + ... I (x_2, y_2) × Substitute the value of \ frac {1} {9} $ for the pixel of interest. Now, the average value of the peripheral pixels is assigned to the pixel of interest.

Also, as in the previous time, since the edge part of the image cannot be filtered, 0 padding processing using 0 is performed for pixels that do not exist.

Source code

meanFilter.py


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


def meanFilter(img,k):
  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/k**2)

  #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')

#Average filter
img = meanFilter(img,9)

#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. The output image looks blurry, and you can see that smoothing has been performed.

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 by Python 100 knock # 11 smoothing filter (average filter)
Image processing with Python 100 knock # 10 median filter
Image processing by Python 100 knock # 1 channel replacement
Image processing with Python 100 knock # 12 motion filter
100 image processing by Python Knock # 6 Color reduction processing
Image processing by python (Pillow)
100 Language Processing Knock Chapter 1 by Python
python image processing
Grayscale by matrix-Reinventor of Python image processing-
Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python 100 knocks # 9 Gaussian filter
Communication processing by Python
First Python image processing
Image processing with Python
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image Processing Collection in Python
[Python] Image processing with scikit-image
Image processing 100 knocks Q9, Q10 (filter) speedup
Image processing with Python 100 knock # 10 median filter
Image processing with Python 100 knock # 12 motion filter
Image processing with Python 100 knocks # 9 Gaussian filter
Image processing by Python 100 knock # 11 smoothing filter (average filter)
python image processing
Image processing 100 knocks ①
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Image processing with Python 100 knocks # 2 Grayscale
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
Straight line drawing by matrix-Inventor's original research of Python image processing-
Basics of binarized image processing with Python
Socket communication and multi-thread processing by Python
100 Language Processing Knock-89: Analogy by Additive Constitutiveness
"Data Science 100 Knock (Structured Data Processing)" Python-007 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-006 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-001 Explanation
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python & OpenCV [Tone Curve]
"Data Science 100 Knock (Structured Data Processing)" Python-002 Explanation
Easy image processing in Python with Pillow
Analysis of X-ray microtomography image by Python
[Python] Data Science 100 Knock (Structured Data Processing) 021 Explanation
Image processing 100 knock Q.6. Color reduction processing explanation
"Data Science 100 Knock (Structured Data Processing)" Python-005 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-004 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 020 Explanation
Light image processing with Python x OpenCV
[Python] Data Science 100 Knock (Structured Data Processing) 025 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-003 Explanation
Matrix Convolution Filtering-Reinventor of Python Image Processing-
[Python] Data Science 100 Knock (Structured Data Processing) 019 Explanation
Entry where Python beginners do their best to knock 100 language processing little by little
Affine transformation by matrix (scaling, rotation, shearing, movement) -Reinventor of Python image processing-
Python inexperienced person tries to knock 100 language processing 14-16
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image processing? The story of starting Python for
Image Processing with Python Environment Setup for Windows