Python OpenCV cv2.GaussianBlur 高斯濾波

本篇 ShengYu 將介紹 Python 使用 OpenCV cv2.GaussianBlur 來作影像平滑模糊化,在寫 Python 影像處理程式時常會用到 OpenCV 圖片平滑模糊化的功能,而高斯濾波 Gaussian Filtering 是其中一個方法,接下來介紹怎麼使用高斯濾波 cv2.GaussianBlur 來進行影像平滑模糊化。

cv2.GaussianBlur 高斯濾波

這邊我們介紹高斯濾波 Gaussian Filtering,它與平均濾波 Averaging 類似,平均濾波 Averaging 的 kernel 裡的每個 pixel 權重都是1,而高斯濾波給予每個 pixel 不同權重,中心 pixel 的權重最高,越往邊角權重就越低,相較於平均濾波 Averaging 這樣可以讓圖片失真較少,高斯濾波通常去除雜訊也有不錯的效果。

以下範例為 kernel size 為 5x5,sigma 為 0,

opencv-GaussianBlur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('opencv-logo.png')

blur = cv2.GaussianBlur(img, (5, 5), 0)

plt.subplot(121), plt.imshow(img), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(blur), plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()

結果如下圖所示:

cv2.GaussianBlur 參數的詳細細節請參考這裡

cv2.getGaussianKernel 取得高斯 kernel

這邊介紹使用 cv2.getGaussianKernel 取得高斯 kernel,

opencv-GaussianBlur2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

kernel_size = 3
sigma = 0

# get 1-D Gaussian kernel
kernel_1d = cv2.getGaussianKernel(kernel_size, sigma)
print(kernel_1d)
kernel_2d = kernel_1d * kernel_1d.T
print(kernel_2d)

輸出結果如下,

1
2
3
4
5
6
[[0.25]
[0.5 ]
[0.25]]
[[0.0625 0.125 0.0625]
[0.125 0.25 0.125 ]
[0.0625 0.125 0.0625]]

使用 cv2.sepFilter2D 做高斯濾波

這邊介紹使用 cv2.sepFilter2D 來做高斯濾波也能達成 cv2.GaussianBlur 同樣的效果,先使用 cv2.getGaussianKernel 建立一個 1-D 的 kernel,接著使用 cv2.sepFilter2D 且分別將 kernelX 與 kernelY 參數都設定成剛剛建立好的 1-D 的 kernel,這樣的結果跟 cv2.GaussianBlur 結果一樣。

opencv-GaussianBlur3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('opencv-logo.png')

kernel_size = 5
sigma = 0
kernel_1d = cv2.getGaussianKernel(kernel_size, sigma)

blur = cv2.sepFilter2D(img, -1, kernel_1d, kernel_1d)

plt.subplot(121), plt.imshow(img), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(blur), plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()

cv2.sepFilter2D 參數的詳細細節請參考這裡

以上就是 Python OpenCV cv2.GaussianBlur 高斯濾波介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

參考
OpenCV: Smoothing Images
https://docs.opencv.org/4.x/d4/d13/tutorial_py_filtering.html
[Python]Gaussian Filter-概念與實作. 實作 Gaussian Filter
https://medium.com/@bob800530/python-gaussian-filter-%E6%A6%82%E5%BF%B5%E8%88%87%E5%AF%A6%E4%BD%9C-676aac52ea17
How Blurs & Filters Work - Computerphile
https://youtu.be/C_zFhWdM4ic
opencv 高斯核是怎么通过参数ksize和sigma计算得到的 cv2.getGaussianKernel()
https://blog.csdn.net/weixin_37804469/article/details/113843829
Gaussian Blurring | TheAILearner
https://theailearner.com/2019/05/06/gaussian-blurring/

其它相關文章推薦
Python OpenCV 影像平滑模糊化 blur
Python OpenCV cv2.medianBlur 中值濾波
Python OpenCV 影像二值化 Image Thresholding
Python OpenCV 彩色轉灰階(RGB/BGR to GRAY)
Python OpenCV 彩色轉HSV(RGB/BGR to HSV)
Python OpenCV 彩色轉YCbCr(RGB/BGR to YCbCr)
Python OpenCV 影像邊緣偵測 Canny Edge Detection
Python OpenCV resize 圖片縮放