本文提供許多的濾波方法,這些方法放在filters.rank子模塊內。
這些方法需要用戶自己設定濾波器的形狀和大小,因此需要導入morphology模塊來設定。
1、autolevel
這個詞在photoshop里面翻譯成自動色階,用局部直方圖來對圖片進行濾波分級。
該濾波器局部地拉伸灰度像素值的直方圖,以覆蓋整個像素值范圍。
格式:skimage.filters.rank.autolevel(image, selem)
selem表示結構化元素,用于設定濾波器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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濾波的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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表示結構化元素,用于設定濾波器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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表示結構化元素,用于設定濾波器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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表示結構化元素,用于設定濾波器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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表示結構化元素,用于設定濾波器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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))
總結
以上就是本文關于python數字圖像處理之高級濾波代碼詳解的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。
原文鏈接:http://www.cnblogs.com/denny402/p/5133086.html