python數(shù)字圖像處理之圖像簡(jiǎn)單濾波實(shí)現(xiàn)
引言
對(duì)圖像進(jìn)行濾波,可以有兩種效果:一種是平滑濾波,用來(lái)抑制噪聲;另一種是微分算子,可以用來(lái)檢測(cè)邊緣和特征提取。
skimage庫(kù)中通過(guò)filters模塊進(jìn)行濾波操作。
1、sobel算子
sobel算子可用來(lái)檢測(cè)邊緣
函數(shù)格式為:skimage.filters.sobel(image, mask=None)
from skimage import data,filters import matplotlib.pyplot as plt img = data.camera() edges = filters.sobel(img) plt.imshow(edges,plt.cm.gray)

2、roberts算子
roberts算子和sobel算子一樣,用于檢測(cè)邊緣
調(diào)用格式也是一樣的:
edges = filters.roberts(img)
3、scharr算子
功能同sobel,調(diào)用格式:
edges = filters.scharr(img)
4、prewitt算子
功能同sobel,調(diào)用格式:
edges = filters.prewitt(img)
5、canny算子
canny算子也是用于提取邊緣特征,但它不是放在filters模塊,而是放在feature模塊
函數(shù)格式:skimage.feature.canny(image,sigma=1.0)
可以修改sigma的值來(lái)調(diào)整效果
from skimage import data,filters,feature
import matplotlib.pyplot as plt
img = data.camera()
edges1 = feature.canny(img) #sigma=1
edges2 = feature.canny(img,sigma=3) #sigma=3
plt.figure('canny',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
從結(jié)果可以看出,sigma越小,邊緣線條越細(xì)小。
6、gabor濾波
gabor濾波可用來(lái)進(jìn)行邊緣檢測(cè)和紋理特征提取。
函數(shù)調(diào)用格式:skimage.filters.gabor_filter(image, frequency)
通過(guò)修改frequency值來(lái)調(diào)整濾波效果,返回一對(duì)邊緣結(jié)果,一個(gè)是用真實(shí)濾波核的濾波結(jié)果,一個(gè)是想象的濾波核的濾波結(jié)果。
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
filt_real, filt_imag = filters.gabor_filter(img,frequency=0.6)
plt.figure('gabor',figsize=(8,8))
plt.subplot(121)
plt.title('filt_real')
plt.imshow(filt_real,plt.cm.gray)
plt.subplot(122)
plt.title('filt-imag')
plt.imshow(filt_imag,plt.cm.gray)
plt.show()
以上為frequency=0.6的結(jié)果圖。

以上為frequency=0.1的結(jié)果圖
7、gaussian濾波
多維的濾波器,是一種平滑濾波,可以消除高斯噪聲。
調(diào)用函數(shù)為:skimage.filters.gaussian_filter(image, sigma)
通過(guò)調(diào)節(jié)sigma的值來(lái)調(diào)整濾波效果
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.astronaut()
edges1 = filters.gaussian_filter(img,sigma=0.4) #sigma=0.4
edges2 = filters.gaussian_filter(img,sigma=5) #sigma=5
plt.figure('gaussian',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
可見(jiàn)sigma越大,過(guò)濾后的圖像越模糊
8、median
中值濾波,一種平滑濾波,可以消除噪聲。
需要用skimage.morphology模塊來(lái)設(shè)置濾波器的形狀。
from skimage import data,filters
import matplotlib.pyplot as plt
from skimage.morphology import disk
img = data.camera()
edges1 = filters.median(img,disk(5))
edges2= filters.median(img,disk(9))
plt.figure('median',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
從結(jié)果可以看出,濾波器越大,圖像越模糊。
9、水平、垂直邊緣檢測(cè)
上邊所舉的例子都是進(jìn)行全部邊緣檢測(cè),有些時(shí)候我們只需要檢測(cè)水平邊緣,或垂直邊緣,就可用下面的方法。
水平邊緣檢測(cè):sobel_h, prewitt_h, scharr_h
垂直邊緣檢測(cè): sobel_v, prewitt_v, scharr_v
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
edges1 = filters.sobel_h(img)
edges2 = filters.sobel_v(img)
plt.figure('sobel_v_h',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
上邊左圖為檢測(cè)出的水平邊緣,右圖為檢測(cè)出的垂直邊緣。
10、交叉邊緣檢測(cè)
可使用Roberts的十字交叉核來(lái)進(jìn)行過(guò)濾,以達(dá)到檢測(cè)交叉邊緣的目的。這些交叉邊緣實(shí)際上是梯度在某個(gè)方向上的一個(gè)分量。
其中一個(gè)核:
0 1
-1 0
對(duì)應(yīng)的函數(shù):
roberts_neg_diag(image)
例:
from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_neg_diag(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)
另外一個(gè)核:
1 0
0 -1
對(duì)應(yīng)函數(shù)為:
roberts_pos_diag(image)
from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_pos_diag(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)
以上就是python數(shù)字圖像處理之圖像簡(jiǎn)單濾波實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于python數(shù)字圖像處理簡(jiǎn)單濾波的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 實(shí)現(xiàn)將小圖片放到另一個(gè)較大的白色或黑色背景圖片中
今天小編就為大家分享一篇python 實(shí)現(xiàn)將小圖片放到另一個(gè)較大的白色或黑色背景圖片中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python PyWebIO提升團(tuán)隊(duì)效率使用介紹
這篇文章主要為大家介紹了Python PyWebIO提升團(tuán)隊(duì)效率使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Python編寫(xiě)春聯(lián)的示例代碼(支持行書(shū)隸書(shū)楷書(shū))
這篇文章主要介紹了如何通過(guò)Python代碼編寫(xiě)春聯(lián),其中春聯(lián)字體支持行書(shū)隸書(shū)楷書(shū)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-01-01
Python使用Mechanize模塊編寫(xiě)爬蟲(chóng)的要點(diǎn)解析
這篇文章主要介紹了Python使用Mechanize模塊編寫(xiě)爬蟲(chóng)的要點(diǎn)解析,作者還講解了Mechanize程序占用內(nèi)存過(guò)高問(wèn)題的相關(guān)解決方法,需要的朋友可以參考下2016-03-03
python+webdriver自動(dòng)化環(huán)境搭建步驟詳解
在本篇文章里小編給大家分享了關(guān)于python+webdriver自動(dòng)化環(huán)境搭建的詳細(xì)步驟以及注意點(diǎn),需要的朋友們參考下。2019-06-06
解決Python httpx 運(yùn)行過(guò)程中無(wú)限阻塞的問(wèn)題
這篇文章主要介紹了解決Python httpx 運(yùn)行過(guò)程中無(wú)限阻塞的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python爬蟲(chóng)實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息
這篇文章主要介紹了Python爬蟲(chóng)實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

