1、空間濾波原理
空間濾波,就是在原圖像上,用一個(gè)固定尺寸的模板去做卷積運(yùn)算,得到的新圖像就是濾波結(jié)果。濾波,就是過(guò)濾某種信號(hào)的意思。過(guò)濾哪種信號(hào)取決于模板設(shè)計(jì),如果是銳化模板,處理后就保留高頻信號(hào),如果是平滑模板,處理后就保留低頻信號(hào)。
(1)模板運(yùn)算
圖像處理中模板能夠看作是n*n(n通常是奇數(shù))的窗體。模板連續(xù)地運(yùn)動(dòng)于整個(gè)圖像中,對(duì)模板窗體范圍內(nèi)的像素做相應(yīng)處理。
模板運(yùn)算主要分為:
①模板卷積。模板卷積是把模板內(nèi)像素的灰度值和模板中相應(yīng)的灰度值相乘,求平均值賦給當(dāng)前模板窗體的中心像素。作為它的灰度值;
②模板排序。模板排序是把模版內(nèi)像素的灰度值排序,取某個(gè)順序統(tǒng)計(jì)量作為模板中心像素灰度值。
Matlab中做模版卷積十分高效,取出模版內(nèi)子矩陣和模版權(quán)重點(diǎn)乘求平均就可以,已圖示為例,3X3的模板在圖像上滑動(dòng),原圖像f(x,y) 經(jīng)過(guò)模板處理后變成了g(x,y)。
(2)邊界處理
處理邊界有非常多種做法:
①重復(fù)圖像邊緣上的行和列。
②卷繞輸入圖像(假設(shè)第一列緊接著最后一列)。
③在輸入圖像外部填充常數(shù)(例如零)。
④去掉不能計(jì)算的行列。僅對(duì)可計(jì)算的像素計(jì)算卷積。
(3)空間域?yàn)V波
把模板運(yùn)算運(yùn)用于圖像的空間域增強(qiáng)的技術(shù)稱為空間域?yàn)V波。依據(jù)濾波頻率空間域?yàn)V波分為平滑濾波(減弱和去除高頻分量)和銳化濾波(減弱和去除低頻分量),依據(jù)濾波計(jì)算特點(diǎn)又分為線性濾波和非線性濾波。
因此空間域?yàn)V波可分為:
分類(lèi)?? ? 線性 ????? 非線性
平滑?? 線性平滑?? 非線性平滑
銳化?? 線性銳化?? 非線性銳化
2、平滑濾波器
(1)添加噪聲
噪聲主要分類(lèi)為兩類(lèi),高斯噪聲和椒鹽噪聲。
高斯噪聲在每個(gè)像素上都會(huì)出現(xiàn),賦值服從高斯分布。
椒鹽噪聲出現(xiàn)位置隨機(jī),所以可以控制椒鹽噪聲的密度,椒鹽噪聲的幅度確定,椒噪聲偏暗,鹽噪聲偏亮。
Image = mat2gray( imread('original_pattern.jpg') ,[0 255]);
noiseIsp=imnoise(Image,'salt & pepper',0.1); %添加椒鹽噪聲,密度為0.1
imshow(noiseIsp,[0 1]); title('椒鹽噪聲圖像');
noiseIg=imnoise(Image,'gaussian'); %添加高斯噪聲,默認(rèn)均值為0,方差為0.01
figure;imshow(noiseIg,[0 1]); title('高斯噪聲圖像');
(2)平滑濾波器
?平滑濾波器可以去除圖像的噪聲,使圖像變得模糊。包括:中值濾波、均值濾波、高斯濾波。
高斯濾波、均值濾波去除高斯噪聲。
(3)均值濾波
Image=imread('Letters-a.jpg');
noiseI=imnoise(Image,'gaussian'); %添加高斯噪聲
subplot(221),imshow(Image),title('原圖');
subplot(222),imshow(noiseI),title('高斯噪聲圖像');
result1=filter2(fspecial('average',3),noiseI); %3×3均值濾波
result2=filter2(fspecial('average',7),noiseI); % 7×7均值濾波
subplot(223),imshow(uint8(result1)),title('3×3均值濾波');
subplot(224),imshow(uint8(result2)),title('7×7均值濾波');
(4)中值濾波
?
Image=rgb2gray(imread('lotus.bmp'));
noiseI=imnoise(Image,'salt & pepper',0.1);
result=medfilt2(noiseI); %3×3中值濾波
subplot(121),imshow(noiseI),title('椒鹽噪聲圖像');
subplot(122),imshow(uint8(result)),title('3×3中值濾波');
(5)自編程實(shí)現(xiàn)高斯濾波
Image=imread('Letters-a.jpg');
sigma1=0.6; sigma2=10; r=3; % 高斯模板的參數(shù)
NoiseI= imnoise(Image,'gaussian'); %加噪
gausFilter1=fspecial('gaussian',[2*r+1 2*r+1],sigma1);
gausFilter2=fspecial('gaussian',[2*r+1 2*r+1],sigma2);
result1=imfilter(NoiseI,gausFilter1,'conv');
result2=imfilter(NoiseI,gausFilter2,'conv');
subplot(231);imshow(Image);title('原圖');
subplot(232);imshow(NoiseI);title('高斯噪聲圖像');
subplot(233);imshow(result1);title('sigma1 =0.6高斯濾波');
subplot(234);imshow(result2);title('sigma2 =10高斯濾波');
%imwrite(uint8(NoiseI),'gr.bmp');
%imwrite(uint8(result1),'gr1.bmp');
%imwrite(uint8(result2),'gr2.bmp');
%編寫(xiě)高斯濾波函數(shù)實(shí)現(xiàn)
[height,width]=size(NoiseI);
for x=-r:r
for y=-r:r
H(x+r+1,y+r+1)=1/(2*pi*sigma1^2).*exp((-x.^2-y.^2)/(2*sigma1^2));
end
end
H=H/sum(H(:)); %歸一化高斯模板H
result3=zeros(height,width); %濾波后圖像
midimg=zeros(height+2*r,width+2*r); %中間圖像
midimg(r+1:height+r,r+1:width+r)=NoiseI;
for ai=r+1:height+r
for aj=r+1:width+r
temp_row=ai-r;
temp_col=aj-r;
temp=0;
for bi=1:2*r+1
for bj=1:2*r+1
temp= temp+(midimg(temp_row+bi-1,temp_col+bj-1)*H(bi,bj));
end
end
result3(temp_row,temp_col)=temp;
end
end
subplot(235);imshow(uint8(result3));title('myself高斯濾波');
3、銳化濾波器
?(1)梯度算子
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(131),imshow(Image),title('原圖像');
[h,w]=size(Image);
edgeImage=zeros(h,w);
for x=1:w-1
for y=1:h-1
edgeImage(y,x)=abs(Image(y,x+1)-Image(y,x))+abs(Image(y+1,x)-Image(y,x));
end
end
subplot(132),imshow(edgeImage),title('梯度圖像');
sharpImage=Image+edgeImage;
subplot(133),imshow(sharpImage),title('銳化圖像');
(2)Robert算子?
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(221),imshow(Image),title('原圖像');
BW= edge(Image,'roberts');
subplot(222),imshow(BW),title('邊緣檢測(cè)');
H1=[1 0; 0 -1];
H2=[0 1;-1 0];
R1=imfilter(Image,H1);
R2=imfilter(Image,H2);
edgeImage=abs(R1)+abs(R2);
subplot(223),imshow(edgeImage),title('Robert梯度圖像');
sharpImage=Image+edgeImage;
subplot(224),imshow(sharpImage),title('Robert銳化圖像');
(3)Sobel算子?
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(221),imshow(Image),title('原圖像');
BW= edge(Image,'sobel');
subplot(222),imshow(BW),title('邊緣檢測(cè)');
H1=[-1 -2 -1;0 0 0;1 2 1];
H2=[-1 0 1;-2 0 2;-1 0 1];
R1=imfilter(Image,H1);
R2=imfilter(Image,H2);
edgeImage=abs(R1)+abs(R2);
subplot(223),imshow(edgeImage),title('Sobel梯度圖像');
sharpImage=Image+edgeImage;
subplot(224),imshow(sharpImage),title('Sobel銳化圖像');
(4)多個(gè)模板邊緣檢測(cè)?
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-861155.html
clear,clc,close all;
Image=im2double(rgb2gray(imread('lotus.jpg')));
H1=[-1 -1 -1;0 0 0;1 1 1];
H2=[0 -1 -1;1 0 -1; 1 1 0];
H3=[1 0 -1;1 0 -1;1 0 -1];
H4=[1 1 0;1 0 -1;0 -1 -1];
H5=[1 1 1;0 0 0;-1 -1 -1];
H6=[0 1 1;-1 0 1;-1 -1 0];
H7=[-1 0 1;-1 0 1;-1 0 1];
H8=[-1 -1 0;-1 0 1;0 1 1];
R1=imfilter(Image,H1);
R2=imfilter(Image,H2);
R3=imfilter(Image,H3);
R4=imfilter(Image,H4);
R5=imfilter(Image,H5);
R6=imfilter(Image,H6);
R7=imfilter(Image,H7);
R8=imfilter(Image,H8);
edgeImage1=abs(R1)+abs(R7);
sharpImage1=edgeImage1+Image;
f1=max(max(R1,R2),max(R3,R4));
f2=max(max(R5,R6),max(R7,R8));
edgeImage2=max(f1,f2);
sharpImage2=edgeImage2+Image;
subplot(221),imshow(edgeImage1),title('兩個(gè)模板邊緣檢測(cè)');
subplot(222),imshow(edgeImage2),title('八個(gè)模板邊緣檢測(cè)');
subplot(223),imshow(sharpImage1),title('兩個(gè)模板邊緣銳化');
subplot(224),imshow(sharpImage2),title('八個(gè)模板邊緣銳化');
(5)Laplacian算子
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-861155.html
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(131),imshow(Image),title('原圖像');
H=fspecial('laplacian',0);
R=imfilter(Image,H);
edgeImage=abs(R);
subplot(132),imshow(edgeImage),title('Laplacian梯度圖像');
H1=[0 -1 0;-1 5 -1;0 -1 0];
sharpImage=imfilter(Image,H1);
subplot(133),imshow(sharpImage),title('Laplacian銳化圖像');
到了這里,關(guān)于數(shù)字圖像處理之matlab實(shí)驗(yàn)(三):空間濾波器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!