# OpenCV 中的模糊滤镜

Salman Mehmood 2024年2月15日

## 在 OpenCV 中使用不同类型的模糊滤镜

numpy 创建了这种平方内核。所以在同质滤波器中，内核看起来像这个图像。

``````IMG = cv2.imread("opencv-logo.jpg")
``````

``````IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)
``````

``````K = np.ones((5, 5), np.float32) / 25
``````

``````HMG = cv2.filter2D(IMG, -1, K)
``````

``````for j in range(2):
plot.subplot(1, 2, j + 1), plot.imshow(IMGS[j], "gray")
``````

``````import numpy as np
import matplotlib.pyplot as plot

IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

K = np.ones((5, 5), np.float32) / 25
HMG = cv2.filter2D(IMG, -1, K)

T = ["Original IMG", "2D Convolution"]
IMGS = [IMG, HMG]

for j in range(2):
plot.subplot(1, 2, j + 1), plot.imshow(IMGS[j], "gray")
plot.title(T[j])
plot.xticks([]), plot.yticks([])

plot.show()
``````

### 模糊滤镜

`blur()` 方法也称为平均方法，我们将使用它来应用平均算法来制作模糊图像。该方法有两个参数：第一个是图像，第二个是内核，即 (5,5)。

``````import cv2
import numpy as np
import matplotlib.pyplot as plot

IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

K = np.ones((5, 5), np.float32) / 25
HMG = cv2.filter2D(IMG, -1, K)
BL = cv2.blur(IMG, (5, 5))

T = ["Original IMG", "2D Convolution", "Blur"]
IMGS = [IMG, HMG, BL]

for j in range(3):
plot.subplot(1, 3, j + 1), plot.imshow(IMGS[j], "gray")
plot.title(T[j])
plot.xticks([]), plot.yticks([])

plot.show()
``````

### 高斯滤波器

``````import cv2
import numpy as np
import matplotlib.pyplot as plot

IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

K = np.ones((5, 5), np.float32) / 25
HMG = cv2.filter2D(IMG, -1, K)
BL = cv2.blur(IMG, (5, 5))
GB = cv2.GaussianBlur(IMG, (5, 5), 0)

T = ["Original IMG", "2D Convolution", "Blur", "GaussianBlur"]
IMGS = [IMG, HMG, BL, GB]

for j in range(4):
plot.subplot(2, 2, j + 1), plot.imshow(IMGS[j], "gray")
plot.title(T[j])
plot.xticks([]), plot.yticks([])

plot.show()
``````

### 中值滤波器

``````import cv2
import numpy as np
import matplotlib.pyplot as plot

IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

K = np.ones((5, 5), np.float32) / 25
HMG = cv2.filter2D(IMG, -1, K)
BL = cv2.blur(IMG, (5, 5))
GB = cv2.GaussianBlur(IMG, (5, 5), 0)
MB = cv2.medianBlur(IMG, 5)

T = ["Original IMG", "2D Convolution", "Blur", "GaussianBlur", "medianBlur"]
IMGS = [IMG, HMG, BL, GB, MB]

for j in range(5):
plot.subplot(2, 3, j + 1), plot.imshow(IMGS[j], "gray")
plot.title(T[j])
plot.xticks([]), plot.yticks([])

plot.show()
``````

### 双边过滤器

`bilateralFilter()` 方法将图像作为第一个参数。第二个参数是过滤时使用的每个像素的直径，第三个参数是 Sigma 颜色，第四个是 Sigma 空间。

Sigma 颜色是颜色空间中的滤波器 Sigma，Sigma 空间是坐标空间中的滤波器 Sigma。

``````import cv2
import numpy as np
import matplotlib.pyplot as plot

IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

K = np.ones((5, 5), np.float32) / 25
HMG = cv2.filter2D(IMG, -1, K)
BL = cv2.blur(IMG, (5, 5))
GB = cv2.GaussianBlur(IMG, (5, 5), 0)
MB = cv2.medianBlur(IMG, 5)
BF = cv2.bilateralFilter(IMG, 9, 75, 75)

T = [
"Original IMG",
"2D Convolution",
"Blur",
"GaussianBlur",
"medianBlur",
"bilateralFilter",
]
IMGS = [IMG, HMG, BL, GB, MB, BF]
plot.figure(figsize=(8, 6))
for j in range(6):
plot.subplot(2, 3, j + 1), plot.imshow(IMGS[j], "gray")
plot.title(T[j])
plot.xticks([]), plot.yticks([])

plot.show()
``````

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.