OpenCV實現(xiàn)圖像濾波之雙邊濾波
發(fā)布日期:2021-12-26 21:17 | 文章來源:站長之家
本文實例為大家分享了opencv實現(xiàn)雙邊濾波的具體代碼,供大家參考,具體內(nèi)容如下
1、2D卷積
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
使用自定義卷積核進行圖像2D卷積操作
函數(shù)原型:
filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst
函數(shù)返回值:dst:2d卷積操作后的結(jié)果
函數(shù)解析:
ddepth:指定輸出圖像深度,-1表示與src深度保持一致
kernel:卷積內(nèi)核大小, 需大于零,可以不同,如核大?。?,5)
anchor:錨點;默認值Point(-1,-1)表示錨位于內(nèi)核中央
delta:在將它們存儲在dst中之前,將delta可選值添加到已過濾的像素中,默認為None
borderType:邊框模式用于圖像外部的像素, 默認邊緣像素拷貝
"""
import cv2 as cv
import numpy as np
img = cv.imread('./test.png')
# 自定義的一些卷積核
kernel = np.ones((5, 5), np.float32) / 25
kernel_user_1 = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]) / 9
kernel_user_2 = np.array([[1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1]]) / 9
kernel_user_3 = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]) / 9
kernel_user_4 = np.array([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]]) / 16
dst = cv.filter2D(img, -1, kernel)
dst1 = cv.filter2D(img, -1, kernel_user_1)
dst2 = cv.filter2D(img, -1, kernel_user_2)
dst3 = cv.filter2D(img, -1, kernel_user_3)
dst4 = cv.filter2D(img, -1, kernel_user_4)
h1 = np.hstack((img, dst, dst1))
h2 = np.hstack((dst2, dst3, dst4))
cv.imshow('show', np.vstack((h1, h2)))
cv.waitKey(0)
cv.destroyAllWindows()
# 理解提高
small = np.array(range(10, 55, 5), np.uint8).reshape(3, -1)
print(small)
print('*' * 60)
small_filter = cv.filter2D(small, -1, (np.ones((3, 3), np.float32) / (3 * 3)))
print(small_filter)

2、雙邊濾波
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
雙邊濾波器可以很好的保存圖像邊緣細節(jié)并濾除掉低頻分量的噪音,
但是雙邊濾波器的效率不是太高,花費的時間相較于其他濾波器而言也比較長。
函數(shù)原型:
bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) -> dst
重點參數(shù)解析:
d:表示在過濾過程中每個像素鄰域的直徑范圍。如果該值是非正數(shù),則將由sigmaSpace計算
sigmaColor:顏色空間過濾器的sigma值,值越大表示有越寬廣的顏色混合到一起
sigmaSpace: 坐標(biāo)空間中濾波器的sigma值,如果該值較大,則意味著越遠的像素將相互影響
borderType:邊框模式用于圖像外部的像素, 默認邊緣像素拷貝
"""
import cv2 as cv
import numpy as np
# img_path = './images/Fig4.11(a).jpg'
# img_path = './images/Fig5.08(b).jpg'
# img_path = './images/Fig0519(a)(florida_satellite_original).tif'
img_path = 'noisy2.png'
img = cv.imread(img_path)
def nothing(x):
pass
cv.namedWindow('image')
# 創(chuàng)建滑動條
cv.createTrackbar('d', 'image', 0, 100, nothing)
cv.createTrackbar('sigmaColor', 'image', 0, 200, nothing)
cv.createTrackbar('sigmaSpace', 'image', 0, 200, nothing)
cv.imshow('img', img)
cv.imshow('image', img)
while True:
k = cv.waitKey(25) & 0XFF
if chr(k) == 'q':
break
if chr(k) == 'k':
d = cv.getTrackbarPos('d', 'image')
sigmaColor = cv.getTrackbarPos('sigmaColor', 'image')
sigmaSpace = cv.getTrackbarPos('sigmaSpace', 'image')
b_filter = cv.bilateralFilter(img, d, sigmaColor, sigmaSpace)
ret, thresh = cv.threshold(b_filter, 127, 255, cv.THRESH_BINARY)
sava_name = ''.join(('outputs/', 'b_filter', str(d), '_', str(sigmaColor), '_', str(sigmaColor)))
cv.imshow('image', np.hstack((b_filter, thresh)))
cv.imwrite(sava_name + '.jpg', b_filter)
cv.imwrite(sava_name + '_thr.jpg', thresh)
cv.destroyAllWindows()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信