本篇 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,
1 | #!/usr/bin/env python3 |
結果如下圖所示:
cv2.GaussianBlur 參數的詳細細節請參考這裡
cv2.getGaussianKernel 取得高斯 kernel
這邊介紹使用 cv2.getGaussianKernel 取得高斯 kernel,
1 | #!/usr/bin/env python3 |
輸出結果如下,
1 | [[0.25] |
使用 cv2.sepFilter2D 做高斯濾波
這邊介紹使用 cv2.sepFilter2D 來做高斯濾波也能達成 cv2.GaussianBlur 同樣的效果,先使用 cv2.getGaussianKernel 建立一個 1-D 的 kernel,接著使用 cv2.sepFilter2D 且分別將 kernelX 與 kernelY 參數都設定成剛剛建立好的 1-D 的 kernel,這樣的結果跟 cv2.GaussianBlur 結果一樣。
1 | #!/usr/bin/env python3 |
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 圖片縮放