Image processing with Python 100 knock # 12 motion filter

Introduction

Hi, I'm Ramu. This time, we will implement a motion filter, which is a filter that adds movement to the image.

12th: Motion filter

A smoothing filter is a filter that smoothes an image. By applying this filter, you can make the entire image look blurry. Especially with this motion filter, fluid blurring is possible.

This filter replaces the pixel of interest with the diagonal mean of the surrounding pixels. For example, a 3x3 or 5x5 motion filter looks like this:

\frac{1}{3} 0 0
0 \frac{1}{3} 0
0 0 \frac{1}{3}
\frac{1}{5} 0 0 0 0
0 \frac{1}{5} 0 0 0
0 0 \frac{1}{5} 0 0
0 0 0 \frac{1}{5} 0
0 0 0 0 \frac{1}{5}

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} {3} + I (x_1, y_1) × \ frac {1} {3} + I (x_2, y_2) × \ frac { Substitute the value of 1} {3} $ for the pixel of interest. Now, the average value of the diagonal lines 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

motionFilter.py


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


def motionFilter(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 i in range(-1*size,k-size):
    ker[i+size,i+size] = 1/k

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

#Motion filter
#Second argument: Filter size
img = motionFilter(img,21)

#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. Also, unlike other smoothing filters, it flows in the diagonal direction of the filter.

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 knock # 12 motion filter
Image processing with Python 100 knock # 10 median filter
Image processing with Python
Image processing with Python 100 knocks # 9 Gaussian filter
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing by Python 100 knock # 11 smoothing filter (average filter)
[Python] Image processing with scikit-image
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock with Python (Chapter 3)
Image processing with Python 100 knocks # 2 Grayscale
python image processing
Basics of binarized image processing with Python
Image processing with Python 100 knocks # 8 Max pooling
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python & OpenCV [Tone Curve]
100 image processing by Python Knock # 6 Color reduction processing
100 Language Processing Knock with Python (Chapter 2, Part 1)
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
First Python image processing
Image Processing with PIL
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 with PIL (Pillow)
Notes on HDR and RAW image processing with Python
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (2)
Sorting image files with Python (3)
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Tweet with image in Python
Sorting image files with Python
Image processing by python (Pillow)
Image Processing Collection in Python
[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
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
100 Language Processing Knock Chapter 1 in Python
[Python] Using OpenCV with Python (Image transformation)
Personal notes for python image processing
Image processing 100 knocks Q9, Q10 (filter) speedup
Let's do image scraping with Python
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Find image similarity with Python + OpenCV
100 Language Processing Knock Chapter 1 by Python
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Create an image processing viewer with PySimpleGUI
Create a dummy image with Python + PIL.