本文轉(zhuǎn)自 python數(shù)字圖像處理
圖像簡單濾波
對(duì)圖像進(jìn)行濾波捏肢,可以有兩種效果:一種是平滑濾波滋捶,用來抑制噪聲盒粮;另一種是微分算子逆日,可以用來檢測(cè)邊緣和特征提取嵌巷。
skimage庫中通過filters模塊進(jìn)行濾波操作。
1室抽、sobel算子
sobel算子可用來檢測(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的值來調(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濾波可用來進(jìn)行邊緣檢測(cè)和紋理特征提取。
函數(shù)調(diào)用格式:
skimage.filters.gabor_filter(image, frequency)
通過修改frequency值來調(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)
通過調(diào)節(jié)sigma的值來調(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()
可見sigma越大檐晕,過濾后的圖像越模糊
8.median
中值濾波暑诸,一種平滑濾波,可以消除噪聲辟灰。
需要用skimage.morphology模塊來設(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的十字交叉核來進(jìn)行過濾,以達(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)
圖像自動(dòng)閾值分割
圖像閾值分割是一種廣泛應(yīng)用的分割技術(shù),利用圖像中要提取的目標(biāo)區(qū)域與其背景在灰度特性上的差異眶拉,把圖像看作具有不同灰度級(jí)的兩類區(qū)域(目標(biāo)區(qū)域和背景區(qū)域)的組合千埃,選取一個(gè)比較合理的閾值,以確定圖像中每個(gè)像素點(diǎn)應(yīng)該屬于目標(biāo)區(qū)域還是背景區(qū)域忆植,從而產(chǎn)生相應(yīng)的二值圖像放可。
在skimage庫中谒臼,閾值分割的功能是放在filters模塊中。
我們可以手動(dòng)指定一個(gè)閾值耀里,從而來實(shí)現(xiàn)分割蜈缤。也可以讓系統(tǒng)自動(dòng)生成一個(gè)閾值,下面幾種方法就是用來自動(dòng)生成閾值冯挎。
1底哥、threshold_otsu
基于Otsu的閾值分割方法,函數(shù)調(diào)用格式:
skimage.filters.threshold_otsu(image, nbins=256)
參數(shù)image是指灰度圖像房官,返回一個(gè)閾值趾徽。
from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_otsu(image) #返回一個(gè)閾值
dst =(image <= thresh)*1.0 #根據(jù)閾值進(jìn)行分割
plt.figure('thresh',figsize=(8,8))
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
返回閾值為87,根據(jù)87進(jìn)行分割得下圖:
2翰守、threshold_yen
使用方法同上:
thresh = filters.threshold_yen(image)
返回閾值為198孵奶,分割如下圖:
3、threshold_li
使用方法同上:
thresh = filters.threshold_li(image)
返回閾值64.5蜡峰,分割如下圖:
4了袁、threshold_isodata
閾值計(jì)算方法:
threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0
使用方法同上:
thresh = filters.threshold_isodata(image)
返回閾值為87,因此分割效果和threshold_otsu一樣湿颅。
5载绿、threshold_adaptive
調(diào)用函數(shù)為:
skimage.filters.threshold_adaptive(image, block_size, method='gaussian')
block_size: 塊大小,指當(dāng)前像素的相鄰區(qū)域大小肖爵,一般是奇數(shù)(如3,5臀脏,7劝堪。。揉稚。)
method: 用來確定自適應(yīng)閾值的方法秒啦,有'mean', 'generic', 'gaussian' 和 'median'。省略時(shí)默認(rèn)為gaussian
該函數(shù)直接訪問一個(gè)閾值后的圖像搀玖,而不是閾值余境。
from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
dst =filters.threshold_adaptive(image, 15) #返回一個(gè)閾值圖像
plt.figure('thresh',figsize=(8,8))
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
大家可以修改block_size的大小和method值來查看更多的效果。如:
dst1 =filters.threshold_adaptive(image,31,'mean')
dst2 =filters.threshold_adaptive(image,5,'median')
兩種效果如下:
基本圖形的繪制
圖形包括線條灌诅、圓形芳来、橢圓形、多邊形等猜拾。
在skimage包中即舌,繪制圖形用的是draw模塊,不要和繪制圖像搞混了挎袜。
1顽聂、畫線條
函數(shù)調(diào)用格式為:
skimage.draw.line(r1,c1,r2,c2)
r1,r2: 開始點(diǎn)的行數(shù)和結(jié)束點(diǎn)的行數(shù)
c1,c2: 開始點(diǎn)的列數(shù)和結(jié)束點(diǎn)的列數(shù)
返回當(dāng)前繪制圖形上所有點(diǎn)的坐標(biāo)肥惭,如:
rr, cc =draw.line(1, 5, 8, 2)
表示從(1,5)到(8紊搪,2)連一條線蜜葱,返回線上所有的像素點(diǎn)坐標(biāo)[rr,cc]
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc =draw.line(1, 150, 470, 450)
img[rr, cc] =255
plt.imshow(img,plt.cm.gray)
如果想畫其它顏色的線條,則可以使用set_color()函數(shù)耀石,格式為:
skimage.draw.set_color(img, coords, color)
例:
draw.set_color(img,[rr,cc],[255,0,0])
則繪制紅色線條牵囤。
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc =draw.line(1, 150, 270, 250)
draw.set_color(img,[rr,cc],[0,0,255])
plt.imshow(img,plt.cm.gray)
2、畫圓
函數(shù)格式:
skimage.draw.circle(cy, cx, radius)
cy和cx表示圓心點(diǎn)娶牌,radius表示半徑
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc=draw.circle(150,150,50)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
3奔浅、多邊形
函數(shù)格式:
skimage.draw.polygon(Y,X)
Y為多邊形頂點(diǎn)的行集合,X為各頂點(diǎn)的列值集合诗良。
from skimage import draw,data
import matplotlib.pyplot as plt
import numpy as np
img=data.chelsea()
Y=np.array([10,10,60,60])
X=np.array([200,400,400,200])
rr, cc=draw.polygon(Y,X)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
我在此處只設(shè)置了四個(gè)頂點(diǎn)汹桦,因此是個(gè)四邊形。
4鉴裹、橢圓
格式:
skimage.draw.ellipse(cy, cx, yradius, xradius)
cy和cx為中心點(diǎn)坐標(biāo)舞骆,yradius和xradius代表長短軸。
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc=draw.ellipse(150, 150, 30, 80)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
5径荔、貝塞兒曲線
格式:
skimage.draw.bezier_curve(y1,x1,y2,x2,y3,x3,weight)
y1,x1表示第一個(gè)控制點(diǎn)坐標(biāo)
y2,x2表示第二個(gè)控制點(diǎn)坐標(biāo)
y3,x3表示第三個(gè)控制點(diǎn)坐標(biāo)
weight表示中間控制點(diǎn)的權(quán)重督禽,用于控制曲線的彎曲度。
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc=draw.bezier_curve(150,50,50,280,260,400,2)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
6总处、畫空心圓
和前面的畫圓是一樣的狈惫,只是前面是實(shí)心圓,而此處畫空心圓鹦马,只有邊框線胧谈。
格式:
skimage.draw.circle_perimeter(yx,yc,radius)
yx,yc是圓心坐標(biāo),radius是半徑
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc=draw.circle_perimeter(150,150,50)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
7荸频、空心橢圓
格式:
skimage.draw.ellipse_perimeter(cy, cx, yradius, xradius)
cy,cx表示圓心
yradius,xradius表示長短軸
from skimage import draw,data
import matplotlib.pyplot as plt
img=data.chelsea()
rr, cc=draw.ellipse_perimeter(150, 150, 30, 80)
draw.set_color(img,[rr,cc],[255,0,0])
plt.imshow(img,plt.cm.gray)
基本形態(tài)學(xué)濾波
對(duì)圖像進(jìn)行形態(tài)學(xué)變換菱肖。變換對(duì)象一般為灰度圖或二值圖,功能函數(shù)放在morphology子模塊內(nèi)旭从。
1稳强、膨脹(dilation)
原理:一般對(duì)二值圖像進(jìn)行操作。找到像素值為1的點(diǎn)和悦,將它的鄰近像素點(diǎn)都設(shè)置成這個(gè)值退疫。1值表示白,0值表示黑鸽素,因此膨脹操作可以擴(kuò)大白色值范圍蹄咖,壓縮黑色值范圍。一般用來擴(kuò)充邊緣或填充小的孔洞付鹿。
功能函數(shù):
skimage.morphology.dilation(image, selem=None)
selem表示結(jié)構(gòu)元素澜汤,用于設(shè)定局部區(qū)域的形狀和大小蚜迅。
from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.dilation(img,sm.square(5)) #用邊長為5的正方形濾波器進(jìn)行膨脹濾波
dst2=sm.dilation(img,sm.square(15)) #用邊長為15的正方形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)
分別用邊長為5或15的正方形濾波器對(duì)棋盤圖片進(jìn)行膨脹操作,結(jié)果如下:
可見濾波器的大小俊抵,對(duì)操作結(jié)果的影響非常大谁不。一般設(shè)置為奇數(shù)。
除了正方形的濾波器外徽诲,濾波器的形狀還有一些刹帕,現(xiàn)列舉如下:
morphology.square: 正方形
morphology.disk: 平面圓形
morphology.ball: 球形
morphology.cube: 立方體形
morphology.diamond: 鉆石形
morphology.rectangle: 矩形
morphology.star: 星形
morphology.octagon: 八角形
morphology.octahedron: 八面體
注意,如果處理圖像為二值圖像(只有0和1兩個(gè)值)谎替,則可以調(diào)用:
skimage.morphology.binary_dilation(image, selem=None)
用此函數(shù)比處理灰度圖像要快偷溺。
2、腐蝕(erosion)
函數(shù):
skimage.morphology.erosion(image, selem=None)
selem表示結(jié)構(gòu)元素钱贯,用于設(shè)定局部區(qū)域的形狀和大小挫掏。
和膨脹相反的操作,將0值擴(kuò)充到鄰近像素秩命。擴(kuò)大黑色部分尉共,減小白色部分∑瘢可用來提取骨干信息袄友,去掉毛刺,去掉孤立的像素霹菊。
from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.erosion(img,sm.square(5)) #用邊長為5的正方形濾波器進(jìn)行膨脹濾波
dst2=sm.erosion(img,sm.square(25)) #用邊長為25的正方形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)
注意剧蚣,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:
skimage.morphology.binary_erosion(image, selem=None)
用此函數(shù)比處理灰度圖像要快旋廷。
3鸠按、開運(yùn)算(opening)
函數(shù):
skimage.morphology.openning(image, selem=None)
selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小柳洋。
先腐蝕再膨脹待诅,可以消除小物體或小斑塊叹坦。
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.opening(img,sm.disk(9)) #用邊長為9的圓形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
注意熊镣,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:
skimage.morphology.binary_opening(image, selem=None)
用此函數(shù)比處理灰度圖像要快募书。
4绪囱、閉運(yùn)算(closing)
函數(shù):
skimage.morphology.closing(image, selem=None)
selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小莹捡。
先膨脹再腐蝕鬼吵,可用來填充孔洞。
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.closing(img,sm.disk(9)) #用邊長為5的圓形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
注意篮赢,如果處理圖像為二值圖像(只有0和1兩個(gè)值)齿椅,則可以調(diào)用:
skimage.morphology.binary_closing(image, selem=None)
用此函數(shù)比處理灰度圖像要快琉挖。
5、白帽(white-tophat)
函數(shù):
skimage.morphology.white_tophat(image, selem=None)
selem表示結(jié)構(gòu)元素涣脚,用于設(shè)定局部區(qū)域的形狀和大小示辈。
將原圖像減去它的開運(yùn)算值,返回比結(jié)構(gòu)化元素小的白點(diǎn)
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.white_tophat(img,sm.square(21))
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
6遣蚀、黑帽(black-tophat)
函數(shù):
skimage.morphology.black_tophat(image, selem=None)
selem表示結(jié)構(gòu)元素矾麻,用于設(shè)定局部區(qū)域的形狀和大小。
將原圖像減去它的閉運(yùn)算值芭梯,返回比結(jié)構(gòu)化元素小的黑點(diǎn)险耀,且將這些黑點(diǎn)反色。
from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.black_tophat(img,sm.square(21))
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')
高級(jí)濾波
本文提供更多更強(qiáng)大的濾波方法玖喘,這些方法放在filters.rank子模塊內(nèi)甩牺。
這些方法需要用戶自己設(shè)定濾波器的形狀和大小,因此需要導(dǎo)入morphology模塊來設(shè)定芒涡。
1柴灯、autolevel
這個(gè)詞在photoshop里面翻譯成自動(dòng)色階,用局部直方圖來對(duì)圖片進(jìn)行濾波分級(jí)费尽。
該濾波器局部地拉伸灰度像素值的直方圖赠群,以覆蓋整個(gè)像素值范圍。
格式:
skimage.filters.rank.autolevel(image, selem)
selem表示結(jié)構(gòu)化元素旱幼,用于設(shè)定濾波器查描。
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: 此濾波器先計(jì)算圖像的形態(tài)學(xué)閉運(yùn)算柏卤,然后用原圖像減去運(yùn)算的結(jié)果值冬三,有點(diǎn)像黑帽操作。
bophat: 此濾波器先計(jì)算圖像的形態(tài)學(xué)開運(yùn)算缘缚,然后用原圖像減去運(yùn)算的結(jié)果值勾笆,有點(diǎn)像白帽操作。
格式:
skimage.filters.rank.bottomhat(image, selem)
skimage.filters.rank.tophat(image, selem)
selem表示結(jié)構(gòu)化元素桥滨,用于設(shè)定濾波器窝爪。
下面是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
對(duì)比度增強(qiáng)齐媒。求出局部區(qū)域的最大值和最小值蒲每,然后看當(dāng)前點(diǎn)像素值最接近最大值還是最小值,然后替換為最大值或最小值喻括。
函數(shù):
enhance_contrast(image, selem)
selem表示結(jié)構(gòu)化元素邀杏,用于設(shè)定濾波器。
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的對(duì)數(shù)運(yùn)算出來的唤崭。該函數(shù)將局部區(qū)域的灰度值分布進(jìn)行二進(jìn)制編碼,返回編碼的最小值脖律。
函數(shù)格式:
entropy(image, selem)
selem表示結(jié)構(gòu)化元素浩姥,用于設(shè)定濾波器。
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
均衡化濾波勒叠。利用局部直方圖對(duì)圖像進(jìn)行均衡化濾波。
函數(shù)格式:
equalize(image, selem)
selem表示結(jié)構(gòu)化元素膏孟,用于設(shè)定濾波器眯分。
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
返回圖像的局部梯度值(如:最大值-最小值)柒桑,用此梯度值代替區(qū)域內(nèi)所有像素值弊决。
函數(shù)格式:
gradient(image, selem)
selem表示結(jié)構(gòu)化元素,用于設(shè)定濾波器魁淳。
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飘诗、其它濾波器
濾波方式很多,下面不再一一詳細(xì)講解界逛,僅給出核心代碼昆稿,所有的函數(shù)調(diào)用方式都是一樣的。
最大值濾波器(maximum):返回圖像局部區(qū)域的最大值息拜,用此最大值代替該區(qū)域內(nèi)所有像素值溉潭。
dst =sfr.maximum(img, disk(5))
最小值濾波器(minimum):返回圖像局部區(qū)域內(nèi)的最小值,用此最小值取代該區(qū)域內(nèi)所有像素值少欺。
dst =sfr.minimum(img, disk(5))
均值濾波器(mean) : 返回圖像局部區(qū)域內(nèi)的均值喳瓣,用此均值取代該區(qū)域內(nèi)所有像素值。
dst =sfr.mean(img, disk(5))
中值濾波器(median): 返回圖像局部區(qū)域內(nèi)的中值赞别,用此中值取代該區(qū)域內(nèi)所有像素值畏陕。
dst =sfr.median(img, disk(5))
莫代爾濾波器(modal) : 返回圖像局部區(qū)域內(nèi)的modal值,用此值取代該區(qū)域內(nèi)所有像素值仿滔。
dst =sfr.modal(img, disk(5))
otsu閾值濾波(otsu): 返回圖像局部區(qū)域內(nèi)的otsu閾值惠毁,用此值取代該區(qū)域內(nèi)所有像素值。
dst =sfr.otsu(img, disk(5))
閾值濾波(threshhold): 將圖像局部區(qū)域中的每個(gè)像素值與均值比較堤撵,大于則賦值為1仁讨,小于賦值為0羽莺,得到一個(gè)二值圖像实昨。
dst =sfr.threshold(img, disk(5))
減均值濾波(subtract_mean): 將局部區(qū)域中的每一個(gè)像素,減去該區(qū)域中的均值盐固。
dst =sfr.subtract_mean(img, disk(5))
求和濾波(sum) :求局部區(qū)域的像素總和荒给,用此值取代該區(qū)域內(nèi)所有像素值丈挟。
dst =sfr.sum(img, disk(5))
參考文獻(xiàn)
python數(shù)字圖像處理