# 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.