OpenCV實現(xiàn)圖像濾波之雙邊濾波
更新時間:2021年10月11日 16:27:45 作者:Sam Chou
這篇文章主要為大家詳細(xì)介紹了OpenCV實現(xiàn)圖像濾波之雙邊濾波,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了opencv實現(xiàn)雙邊濾波的具體代碼,供大家參考,具體內(nèi)容如下
1、2D卷積
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 使用自定義卷積核進(jìn)行圖像2D卷積操作 函數(shù)原型: filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst 函數(shù)返回值:dst:2d卷積操作后的結(jié)果 函數(shù)解析: ddepth:指定輸出圖像深度,-1表示與src深度保持一致 kernel:卷積內(nèi)核大小, 需大于零,可以不同,如核大?。?,5) anchor:錨點;默認(rèn)值Point(-1,-1)表示錨位于內(nèi)核中央 delta:在將它們存儲在dst中之前,將delta可選值添加到已過濾的像素中,默認(rèn)為None borderType:邊框模式用于圖像外部的像素, 默認(rèn)邊緣像素拷貝 """ 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 -*- """ 雙邊濾波器可以很好的保存圖像邊緣細(xì)節(jié)并濾除掉低頻分量的噪音, 但是雙邊濾波器的效率不是太高,花費的時間相較于其他濾波器而言也比較長。 函數(shù)原型: bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) -> dst 重點參數(shù)解析: d:表示在過濾過程中每個像素鄰域的直徑范圍。如果該值是非正數(shù),則將由sigmaSpace計算 sigmaColor:顏色空間過濾器的sigma值,值越大表示有越寬廣的顏色混合到一起 sigmaSpace: 坐標(biāo)空間中濾波器的sigma值,如果該值較大,則意味著越遠(yuǎn)的像素將相互影響 borderType:邊框模式用于圖像外部的像素, 默認(rèn)邊緣像素拷貝 """ 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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python標(biāo)準(zhǔn)庫datetime?date模塊的詳細(xì)介紹
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫datetime?date模塊的詳細(xì)介紹,datetime是Python提供的操作日期和時間的標(biāo)準(zhǔn)庫,主要有datetime.date模塊、datetime.time模塊及datetime.datetime模塊2022-07-07Python循環(huán)語句之while循環(huán)和for循環(huán)詳解
在Python中,循環(huán)語句用于重復(fù)執(zhí)行一段代碼,直到滿足某個條件為止,在Python中,有兩種主要的循環(huán)語句:for循環(huán)和while循環(huán),本文就來給大家介紹一下這兩個循環(huán)的用法,需要的朋友可以參考下2023-08-08詳解pandas使用drop_duplicates去除DataFrame重復(fù)項參數(shù)
這篇文章主要介紹了詳解pandas使用drop_duplicates去除DataFrame重復(fù)項參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python中強大的函數(shù)map?filter?reduce使用詳解
Python是一門功能豐富的編程語言,提供了許多內(nèi)置函數(shù),以簡化各種編程任務(wù),在Python中,map(),filter()和reduce()是一組非常有用的函數(shù),它們允許對可迭代對象進(jìn)行操作,從而實現(xiàn)數(shù)據(jù)轉(zhuǎn)換、篩選和累積等操作,本文將詳細(xì)介紹這三個函數(shù),包括它們的基本用法和示例代碼2023-11-11