python數字圖像處理(14):高級濾波


本文提供更多更強大的濾波方法,這些方法放在filters.rank子模塊內。

這些方法需要用戶自己設定濾波器的形狀和大小,因此需要導入morphology模塊來設定。

1、autolevel

這個詞在photoshop里面翻譯成自動色階,用局部直方圖來對圖片進行濾波分級。

該濾波器局部地拉伸灰度像素值的直方圖,以覆蓋整個像素值范圍。

格式:skimage.filters.rank.autolevel(image, selem)

selem表示結構化元素,用於設定濾波器。

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
auto =sfr.autolevel(img, disk(5))  #半徑為5的圓形濾波器

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(auto,plt.cm.gray)

2、bottomhat 與 tophat

bottomhat: 此濾波器先計算圖像的形態學閉運算,然后用原圖像減去運算的結果值,有點像黑帽操作。

bottomhat: 此濾波器先計算圖像的形態學開運算,然后用原圖像減去運算的結果值,有點像白帽操作。

格式:

skimage.filters.rank.bottomhat(image, selem)

skimage.filters.rank.tophat(image, selem)

selem表示結構化元素,用於設定濾波器。

下面是bottomhat濾波的例子:

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
auto =sfr.bottomhat(img, disk(5))  #半徑為5的圓形濾波器

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(auto,plt.cm.gray)

3、enhance_contrast

對比度增強。求出局部區域的最大值和最小值,然后看當前點像素值最接近最大值還是最小值,然后替換為最大值或最小值。

函數: enhance_contrast(image, selem)

selem表示結構化元素,用於設定濾波器。

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
auto =sfr.enhance_contrast(img, disk(5))  #半徑為5的圓形濾波器

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(auto,plt.cm.gray)

4、entropy

求局部熵,熵是使用基為2的對數運算出來的。該函數將局部區域的灰度值分布進行二進制編碼,返回編碼的最小值。

函數格式:entropy(image, selem)

selem表示結構化元素,用於設定濾波器。

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
dst =sfr.entropy(img, disk(5))  #半徑為5的圓形濾波器

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)

5、equalize

均衡化濾波。利用局部直方圖對圖像進行均衡化濾波。

函數格式:equalize(image, selem)

selem表示結構化元素,用於設定濾波器。

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
dst =sfr.equalize(img, disk(5))  #半徑為5的圓形濾波器

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)

6、gradient

返回圖像的局部梯度值(如:最大值-最小值),用此梯度值代替區域內所有像素值。

函數格式:gradient(image, selem)

selem表示結構化元素,用於設定濾波器。

from skimage import data,color
import matplotlib.pyplot as plt
from skimage.morphology import disk
import skimage.filters.rank as sfr
img =color.rgb2gray(data.lena())
dst =sfr.gradient(img, disk(5))  #半徑為5的圓形濾波器

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)

7、其它濾波器

濾波方式很多,下面不再一一詳細講解,僅給出核心代碼,所有的函數調用方式都是一樣的。

最大值濾波器(maximum):返回圖像局部區域的最大值,用此最大值代替該區域內所有像素值。

dst =sfr.maximum(img, disk(5)) 

最小值濾波器(minimum):返回圖像局部區域內的最小值,用此最小值取代該區域內所有像素值。

dst =sfr.minimum(img, disk(5))

均值濾波器(mean) : 返回圖像局部區域內的均值,用此均值取代該區域內所有像素值。

dst =sfr.mean(img, disk(5)) 

中值濾波器(median): 返回圖像局部區域內的中值,用此中值取代該區域內所有像素值。

dst =sfr.median(img, disk(5))

莫代爾濾波器(modal) : 返回圖像局部區域內的modal值,用此值取代該區域內所有像素值。

dst =sfr.modal(img, disk(5))

otsu閾值濾波(otsu): 返回圖像局部區域內的otsu閾值,用此值取代該區域內所有像素值。

dst =sfr.otsu(img, disk(5))

閾值濾波(threshhold): 將圖像局部區域中的每個像素值與均值比較,大於則賦值為1,小於賦值為0,得到一個二值圖像。

dst =sfr.threshold(img, disk(5)) 

減均值濾波(subtract_mean):  將局部區域中的每一個像素,減去該區域中的均值。

dst =sfr.subtract_mean(img, disk(5))

求和濾波(sum) :求局部區域的像素總和,用此值取代該區域內所有像素值。

dst =sfr.sum(img, disk(5))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM