[PYTHON] I examined OpenCV filtering

at first

I investigated the filtering process provided by OpenCV

Operating environment

Python3,OpenCV

Simple smoothing (blurring / blurring) processing

Assuming that the original image has the following data (grayscale), simple smoothing (blurring) is performed. Untitled.png

Simple smoothing takes the simple average of a rectangle of multiple pixels surrounding each pixel as the value for that pixel. Here, since the size of the rectangle is (3 * 3), it is calculated as follows. Untitled.png

The total pixels are calculated as follows Untitled.png

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cm

black = [0x00, 0x00, 0x00]
white = [0xFF, 0xFF, 0xFF]

img = np.array([
                [black, black, black, black, black, black, black]
                ,[black, black, black, black, black, black, black]
                ,[black, black, white, white, white, black, black]
                ,[black, black, white, white, white, black, black]
                ,[black, black, white, white, white, black, black]
                ,[black, black, black, black, black, black, black]
                ,[black, black, black, black, black, black, black]                               
                ]
                , dtype=np.uint8)

#Grayscale
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

#Graph drawing area
fig = plt.figure(figsize=(8, 3))
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')

# X,Y
_x = np.arange(img.shape[1])
_y = np.arange(img.shape[0])
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
width = depth = 1

#height
top = img.ravel()
bottom = np.zeros_like(top)

# 0-255 to 0.0-1.Definition of function to convert to 0(For grayscale display)
norm = colors.Normalize(0, 255)

#original
color_values = cm.gray(top.tolist())
ax1.bar3d(x, y, bottom, width, depth, top, color=color_values)
ax1.set_title('Original')

#Blur (kernel size 3*3)
blurImg = cv.blur(img, (3, 3))
top = blurImg.ravel()
color_values = cm.gray(norm(top.tolist()))
ax2.bar3d(x, y, bottom, width, depth, top, color=color_values)
ax2.set_title('Blur')

plt.show()
Screen Shot 2020-05-30 at 14.47.09.png Screen Shot 2020-05-30 at 14.48.04.png

Median filtering

The median filter takes the median value of the rectangle that surrounds each pixel as the value for that pixel. Untitled.png

The total pixels are calculated as follows Untitled.png

#medianBlur (kernel size 3)*3)
mBlurImg = cv.medianBlur(img, 3)
top = mBlurImg.ravel()
color_values = cm.gray(norm(top.tolist()))
ax2.bar3d(x, y, bottom, width, depth, top, color=color_values)
ax2.set_title('medianBlur')
Screen Shot 2020-05-30 at 15.00.22.png Screen Shot 2020-05-30 at 15.02.24.png

Gaussian filtering

The Gaussian filter multiplies the rectangle surrounding each pixel by a matrix called the Gaussian kernel and takes the sum as the value for that pixel. The 3 * 3 Gaussian kernel used here is as follows.

\begin{pmatrix}
1/16 & 2/16 & 1/16 \\
2/16 & 4/16 & 2/16 \\ 
1/16 & 2/16 & 1/16
\end{pmatrix}

Untitled.png

PixelA is calculated as

import numpy as np

pixelA = np.array([[0, 0, 0]
                   ,[0, 255, 255]
                   ,[0, 255, 255]])
gaussKernel = np.array([[1/16, 2/16, 1/16]
                        ,[2/16, 4/16, 2/16]
                        ,[1/16, 2/16, 1/16]])
print(sum(sum(pixelA * gaussKernel))) 
#143  

The total pixels are calculated as follows Untitled.png

# GaussianBlur
gBlurImg = cv.GaussianBlur(img, (3, 3), 0, 0)
top = gBlurImg.ravel()
color_values = cm.gray(norm(top.tolist()))
ax2.bar3d(x, y, bottom, width, depth, top, color=color_values)
ax2.set_title('GaussianBlur')
Screen Shot 2020-05-30 at 14.56.10.png Screen Shot 2020-05-30 at 14.57.17.png

Recommended Posts

I examined OpenCV filtering
I tried'Beauty'with OpenCV
I examined the device tree
I tried face recognition with OpenCV
I want to detect objects with OpenCV
OpenCV AI Kit (OAK-D) I tried @ windows10
I checked the options of copyMakeBorder of OpenCV
I tried non-photorealistic rendering with Python + opencv