I investigated the filtering process provided by OpenCV
Python3,OpenCV
Assuming that the original image has the following data (grayscale), simple smoothing (blurring) is performed.
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.
The total pixels are calculated as follows
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()
The median filter takes the median value of the rectangle that surrounds each pixel as the value for that pixel.
The total pixels are calculated as follows
#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')
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}
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
# 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')
Recommended Posts