OpenCV實(shí)現(xiàn)圖像濾波之雙邊濾波
更新時(shí)間:2021年10月11日 16:27:45 作者:Sam Chou
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像濾波之雙邊濾波,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了opencv實(shí)現(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:錨點(diǎn);默認(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é)并濾除掉低頻分量的噪音, 但是雙邊濾波器的效率不是太高,花費(fèi)的時(shí)間相較于其他濾波器而言也比較長。 函數(shù)原型: bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) -> dst 重點(diǎn)參數(shù)解析: d:表示在過濾過程中每個像素鄰域的直徑范圍。如果該值是非正數(shù),則將由sigmaSpace計(jì)算 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中Windows和macOS文件路徑格式不一致的解決方法
在 Python 中,Windows 和 macOS 的文件路徑字符串格式不一致主要體現(xiàn)在路徑分隔符上,這種差異可能導(dǎo)致跨平臺代碼在處理文件路徑時(shí)出錯,下面我們看看如何解決吧2025-03-03Python代理抓取并驗(yàn)證使用多線程實(shí)現(xiàn)
這里沒有使用隊(duì)列只是采用多線程分發(fā)對代理量不大的網(wǎng)頁還行但是幾百幾千性能就很差了,感興趣的朋友可以了解下,希望對你有所幫助2013-05-05對pandas的dataframe繪圖并保存的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄獙andas的dataframe繪圖并保存的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Python使用Pickle庫實(shí)現(xiàn)讀寫序列操作示例
這篇文章主要介紹了Python使用Pickle庫實(shí)現(xiàn)讀寫序列操作,結(jié)合實(shí)例形式分析了pickle模塊的功能、常用函數(shù)以及序列化與反序列化相關(guān)操作技巧,需要的朋友可以參考下2018-06-06