4、空間濾波器


1、線性空間濾波

相關(guān):
w(x,y)☆f(x,y)=\sum^a_{s=-a}\sum^b_{t=-b}w(s,t)f(x+s,y+t)
卷積(w旋轉(zhuǎn)180°):
w(x,y)★f(x,y)=\sum^a_{s=-a}\sum^b_{t=-b}w(s,t)f(x-s,y-t)

工具箱使用 imfilter 來實(shí)現(xiàn)線性空間濾波

g = imfilter(f, w, filtering_mode, boundary_options, size_options)

濾波模式: 'corr' ,相關(guān),默認(rèn)值;'conv',卷積。
邊界選項(xiàng):
P,使用P值來填充擴(kuò)展,默認(rèn)選項(xiàng),值為0;
'replicate',圖像的大小通過復(fù)制圖像邊界外的值來擴(kuò)展;
'symmetric',圖像的大小通過邊界鏡像反射來擴(kuò)展;
'circular',圖像的大小通過將圖像處理為二維周期函數(shù)的一個(gè)周期來擴(kuò)展;
大小選項(xiàng):
'full',輸出與填充后的圖像大小相同;
'same',輸出圖像的大小與輸入圖像的大小相同。

w = ones(31); %31 x 31的濾波器
f8 = im2uint8(f);
g8 = imfilter(f8, w, 'replicate');
imshow(g8, [])



2、非線性空間濾波

實(shí)現(xiàn)非線性空間濾波時(shí),常用 colfilt 函數(shù)

g = colfilt(f, [m n], 'sliding', fun)

使用 colfilt 時(shí),濾波前需要顯示的填充輸入圖像,使用二維函數(shù) padarray 可以實(shí)現(xiàn)

fp = padarray(f, [r c], method, direction)

[r c]: 表示填充 f 的行和列數(shù)
method: symmetric, repliacte, circular
direction: pre, post, both(默認(rèn)值)

f = [1 2; 3 4]
fp = padarray(f, [1 1], 'replicate', 'both')
%幾何平均濾波
gmean = @(A) prod(A, 1)^(1/size(A, 1));
f = padarray(f, [m n], 'replicate');
g = colfit(f, [m n], 'sliding', @gmean)
[M N] = size(f);
g = g((1:M) + m, (1:N) + n);



3、標(biāo)準(zhǔn)線性空間濾波器

工具箱中預(yù)定義了一些二維線性空間濾波器,可以通過函數(shù) fspecial 生成

w = fspecial('type', parameters)

'average': 矩形平均濾波器
fspecial('average', [r c])
'disk': 圓形平均濾波器
fspecial('disk', r)
'gaussian': 高斯低通濾波器
fspecial('gaussian', [r c], sig)
'laplacian': 拉普拉斯濾波器
fspecial('laplacian', alpha)
'log': 高斯拉普拉斯濾波器
fspecial('log', [r c], sig)
'motion' 近似計(jì)算 len 個(gè)像素的線性運(yùn)動(dòng)
fspecial('motion', len, theta)
'prewitt:' prewitt 濾波器,近似計(jì)算垂直梯度
fspecial('prewitt')
'sobel': sobel濾波器,近似計(jì)算垂直梯度
fspecial('sobel')
'unsharp': 非尖銳濾波器
fspecial('unsharp', alpha)

<拉普拉斯算子>

\nabla^2f(x,y)=\frac{\partial^2f(x,y)}{\partial x^2}+\frac{\partial^2f(x,y)}{\partial y^2}

近似為

\nabla^2f(x,y)=[f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)]-4f(x,y)

空間模板

w = fspecial('laplacian', 0)
% 0  1  0
% 1 -4  1
% 0  1  0

拉普拉斯濾波器實(shí)現(xiàn)如下

w = fspecial('laplacian', 0)
f2 = tofloat(f);
g1 = imfilter(f2, w, 'replicate');
g = f2 - g1;



4、標(biāo)準(zhǔn)非線性空間濾波器

  • 排序?yàn)V波器
g = ordfilt2(f, order, domain)

order:排序中的第 order 個(gè)元素代替 f 中的每個(gè)元素
domain: 一個(gè)由 0 和 1 組成的大小為 m x n 的矩陣,計(jì)算時(shí),不使用對(duì)應(yīng)為 0 的像素。

%最小濾波器
g = ordfilt2(f, 1, ones(m, n))
%最大濾波器
g = ordfilt2(f, m*n, ones(m, n))
%中值濾波器
g = ordfilt2(f, (m*n + 1)/2, ones(m, n))

工具箱中提供了一個(gè)專門的二維終止濾波器函數(shù) medfilt2

g = medfilt2(f, [m n], padopt)

padopt: 指定邊界填充選項(xiàng)
'zeros:'零填充,(默認(rèn)值)
'symmetric': 鏡像方式
'indexed':若 f 是 double 類的則用 1 填充,否則用 0 填充

fn = imnoise(f, 'salt & pepper', 0.2);
gm = medfilt2(fn, 'symmetric');
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容