Matrix Convolution Filtering-Reinventor of Python Image Processing-

A story about convolution filtering only by matrix operation without relying on the image processing library. Also possible with Pythonista

** Click here for basics **

What is a "reinventor"?

Instead of relying on Open CV or Pillow, I will actually write various image processing using numpy and matplotlib. It's a combination that can also be used with the iOS app Pythonista.

import numpy as np
import matplotlib.pyplot as plt

In addition, the following functions are convenient for displaying images. (For details, Basics)

def img_show(img : np.ndarray, cmap = 'gray', vmin = 0, vmax = 255, interpolation = 'none') -> None:
    '''np.Display an image with array as an argument.'''
    
    #Set dtype to uint8
    img = np.clip(img,vmin,vmax).astype(np.uint8)
    
    #Display image
    plt.imshow(img, cmap = cmap, vmin = vmin, vmax = vmax, interpolation = interpolation)
    plt.show()
    plt.close()

Filtering by convolution

Spatial filtering is easy to understand for the filter by convolution.

First, create a function to convolve a two-dimensional array.

def convolve2d(img, kernel):
    #Calculate the size of the submatrix
    sub_shape = tuple(np.subtract(img.shape, kernel.shape) + 1)
    
    #Since the function name is long, it is omitted once
    strd = np.lib.stride_tricks.as_strided
    
    #Create a matrix of submatrix
    submatrices = strd(img,kernel.shape + sub_shape,img.strides * 2)
    
    #Calculate the Einstein sum of the submatrix and the kernel
    convolved_matrix = np.einsum('ij,ijkl->kl', kernel, submatrices)
 
    return convolved_matrix

The above code convolves using the matrix of the img submatrix. See stackoverflow teacher for more information. In this algorithm, the peripheral part can be removed.

[Trimming]'tiger.jpeg'(http://qiita.com/secang0/items/1229212a37d8c9922901#%E5%9B%9B%E8%A7%92% E5% BD% A2% E3% 81% AB% E3% 83% 88% E3% 83% AA% E3% 83% 9F% E3% 83% B3% E3% 82% B0330% E8% BF% BD% E5% 8A% A0) image [weighted averaging by NTSC coefficient](http://qiita.com/secang0/items/6fba02d7cc3ef34c0c31#ntsc-%E4%BF%82%E6%95%B0%E3%81%AB % E3% 82% 88% E3% 82% 8B% E5% 8A% A0% E9% 87% 8D% E5% B9% B3% E5% 9D% 87% E6% B3% 95) for grayscale images On the other hand, various filters were used.

img = plt.imread('tiger.jpeg')[1200:1500,1400:1700]
img = (0.298912 * img[...,0] + 0.586611 * img[...,1] + 0.114478 * img[...,2])
img_show(img)

img.png

Low pass filter

A low-pass filter is a filter that tends to leave a low frequency part rather than a high frequency part of an image. (High / low is frequency) In short, it blurs by making the sudden changes less. This process is often used for noise removal.

img_show(convolve2d(img, kernel))

In the table below, concatenate is used to line up with the original image.

Method name Kernel / original image / filter image Remarks
Simple moving average filter kernel = np.ones((5,5)/25 sma.png The simplest low pass filter
Gaussian filter kernel = gaussian_kernel(5) gaussian.png Excellent low-pass filter based on normal distribution

In the original image, the light gray part (yellowish green in the color image) in the center is stippled. However, applying a low-pass filter makes confirmation difficult.

In the table, the following Gaussian matrix is used. Reference

def gaussian_kernel(n : int) -> np.ndarray:
    '''(n,n)Make a Gaussian matrix of'''
    
    #[nC0, nC1, ..., nCn]Create
    combs = [1]
    for i in range(1,n):
        ratio = (n-i)/(i)
        combs.append(combs[-1]*ratio)
    combs = np.array(combs).reshape(1,n)/(2**(n-1))
    
    #Make a Gaussian matrix by the product of vertical and horizontal vectors
    result = combs.T.dot(combs)
    return result

High pass filter

A high-pass filter is a filter that tends to leave a high frequency part rather than a low frequency part of an image. As a result, the various parts are emphasized and the image becomes sharp.

A typical sharpening filter is for $ k> 0 $

1.\hspace{5pt}
\left(
\begin{matrix}
0&-k&0\\
-k&1+4k&-k\\
0&-k&0\\
\end{matrix}
\right)
\\
\mbox{Or}
\\
2.\hspace{5pt}

\left(
\begin{matrix}
-k&-k&-k\\
-k&1+8k&-k\\
-k&-k&-k\\
\end{matrix}
\right)
sharp_kernel_1 = lambda k : np.matrix('0,{0},0;{0},{1},{0};0,{0},0'.format(-k,1+4*k))
sharp_kernel_2 = lambda k : np.matrix('{0},{0},{0};{0},{1},{0};{0},{0},{0}'.format(-k,1+8*k))

Call. Reference The larger the value of k, the stronger the filter. Also, if k is the same, 2 is stronger.

The image is 3 * 3 Gaussian filtered, and the display is standardized as described below.

img_gaussian = convolve2d(img, gaussian(3))
img_show(norm_img(convolve2d(img_gaussian, kernel)))

In the table below, concatenate is used to line up with the original image. The light gray part below (yellowish green in a color image) is originally stippled and has many high frequency components. Please pay particular attention.

Kernel / original image / filter image
kernel = sharp_kernel_1(1)sharpen_1_1.png
kernel = sharp_kernel_1(10)sharpen_1_10.png
kernel = sharp_kernel_1(1)sharpen_2_1.png
kernel = sharp_kernel_2(10)sharpen_2_10.png

Since the pixel value of the filter image does not fit in 0 to 255, it is standardized by the following function. This corresponds the interval of $ mean \ pm n_ {std} \ times standard deviation $ to 0 ~ 255. (Actually $ n_ {std} = 2 $) This is a process that is also used for the original image for comparison, and is slightly different from the top image.

def norm_img(img_filtered,n_std):
    '''img_Standardize filtered'''
    mean = img_filtered.mean() #average
    std = img_filtered.std() #standard deviation
    img_norm = 256*((img_filtered - mean)/std + n_std)/(n_std*2)
    return np.clip(img_norm,0,255).astype(np.uint8)

differential

Derivative filters are used for contour extraction. This is because by finding the difference from the adjacent pixel, you can see where the change is large.

reference: ** Actual results are displayed ** ** Explains various differences between filters **.

The image is 3 * 3 Gaussian filtered. In addition, [High-pass filter section](http://qiita.com/secang0/items/f3a3ff629988dc660d87#%E3%83%8F%E3%82%A4%E3%83%91%E3%82%B9 % E3% 83% 95% E3% 82% A3% E3% 83% AB% E3% 82% BF% E3% 83% BC) The standardization process described in) was applied.

img_gaussian = convolve2d(img, gaussian(3))
img_show(norm_img(convolve2d(img_gaussian, kernel)))
Contents Kernel / original image / filter image Remarks Reference link
Simple first derivative filter from bottom to top kernel = np.matrix('0,-1,0;0,1,0;0,0,0')down_up_1.png Sensitivetonoise Withexplanation
Prewitt filter from bottom to top kernel = np.matrix('-1,-1,-1;0,0,0;1,1,1')down_up_prewitt.png Withresultimage&Withexplanation
Sobel filter from bottom to top kernel = np.matrix('-1,-2,-1;0,0,0;1,2,1')down_up_sobel.png np.matrix('-1,-1,-1;0,0,0;1,1,1')*1.33Thedifferencewith Withresultimage&Withexplanation
Laplacian filter kernel = np.matrix('-1,-1,-1;-1,8,-1;-1,-1,-1')laplacian.png Secondderivativeinalldirections\nabla^2 Withresultimage&Withexplanation

Other

Contents Kernel / original image / filter image Remarks Reference link
Embossing kernel = np.matrix('-2,-1,0;-1,1,1;0,1,2')emboss.png 1inthecenterofSobel Resultimageatthebottom

Recommended Posts

Matrix Convolution Filtering-Reinventor of Python Image Processing-
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
python image processing
Basics of binarized image processing with Python
Grayscale by matrix-Reinventor of Python image processing-
Drawing with Matrix-Reinventor of Python Image Processing-
First Python image processing
Image processing with Python
Various processing of Python
Image processing? The story of starting Python for
Image processing with Python (Part 2)
Affine transformation by matrix (scaling, rotation, shearing, movement) -Reinventor of Python image processing-
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing by python (Pillow)
Post processing of python (NG)
[Python] Image processing with scikit-image
Image capture of firefox using python
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Semi-definite matrix check of convolution kernel
Understand the function of convolution using image processing as an example
Straight line drawing by matrix-Inventor's original research of Python image processing-
Image processing with Python 100 knock # 10 median filter
Python: Basics of image recognition using CNN
Image processing by Python 100 knock # 1 channel replacement
[Python] Matrix multiplication processing time using NumPy
(Java, JavaScript, Python) Comparison of string processing
Image processing with Python 100 knocks # 8 Max pooling
Python: Application of image recognition using CNN
[Python] Calculation of image similarity (Dice coefficient)
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
100 image processing by Python Knock # 6 Color reduction processing
Analysis of X-ray microtomography image by Python
The story of blackjack A processing (python)
Light image processing with Python x OpenCV
Comparison of matrix transpose speeds with Python
Status of each Python processing system in 2020
Image processing with Python 100 knocks # 9 Gaussian filter
[Image processing] Posterization
Introduction of Python
Image processing from scratch with python (5) Fourier transform
View the result of geometry processing in Python
[Python] 90 degree clockwise rotation, 90 degree counterclockwise rotation, 180 degree rotation of matrix [AtCoder]
Basics of Python ①
Basics of python ①
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Copy of python
[Python] Matrix operation
Image processing 100 knocks ①
Image processing by Python 100 knock # 11 smoothing filter (average filter)
Addition of fixed processing when starting python interpreter
Image of closure
[Python + OpenCV] Whiten the transparent part of the image
Introduction of Python
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
[Language processing 100 knocks 2020] Summary of answer examples by Python
Extract the table of image files with OneDrive & Python