數字圖像處理之matlab實驗(三):空間濾波器

慈雲數據 8個月前 (04-08) 技術支持 73 0

1、空間濾波原理

空間濾波,就是在原圖像上,用一個固定尺寸的模闆去做卷積運算,得到的新圖像就是濾波結果。濾波,就是過濾某種信号的意思。過濾哪種信号取決于模闆設計,如果是銳化模闆,處理後就保留高頻信号,如果是平滑模闆,處理後就保留低頻信号。

(1)模闆運算

圖像處理中模闆能夠看作是n*n(n通常是奇數)的窗體。模闆連續地運動于整個圖像中,對模闆窗體範圍内的像素做相應處理。

模闆運算主要分爲:

①模闆卷積。模闆卷積是把模闆内像素的灰度值和模闆中相應的灰度值相乘,求平均值賦給當前模闆窗體的中心像素。作爲它的灰度值;

②模闆排序。模闆排序是把模版内像素的灰度值排序,取某個順序統計量作爲模闆中心像素灰度值。

Matlab中做模版卷積十分高效,取出模版内子矩陣和模版權重點乘求平均就可以,已圖示爲例,3X3的模闆在圖像上滑動,原圖像f(x,y) 經過模闆處理後變成了g(x,y)。

(2)邊界處理

處理邊界有非常多種做法:

①重複圖像邊緣上的行和列。

②卷繞輸入圖像(假設第一列緊接着最後一列)。

③在輸入圖像外部填充常數(例如零)。

④去掉不能計算的行列。僅對可計算的像素計算卷積。

(3)空間域濾波

把模闆運算運用于圖像的空間域增強的技術稱爲空間域濾波。依據濾波頻率空間域濾波分爲平滑濾波(減弱和去除高頻分量)和銳化濾波(減弱和去除低頻分量),依據濾波計算特點又分爲線性濾波和非線性濾波。

因此空間域濾波可分爲:

分類     線性       非線性

平滑   線性平滑   非線性平滑

銳化   線性銳化   非線性銳化

2、平滑濾波器

(1)添加噪聲

噪聲主要分類爲兩類,高斯噪聲和椒鹽噪聲。

高斯噪聲在每個像素上都會出現,賦值服從高斯分布。

椒鹽噪聲出現位置随機,所以可以控制椒鹽噪聲的密度,椒鹽噪聲的幅度确定,椒噪聲偏暗,鹽噪聲偏亮。

Python
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'); %添加高斯噪聲,默認均值爲0,方差爲0.01
figure;imshow(noiseIg,[0 1]); title('高斯噪聲圖像');  

(2)平滑濾波器

 平滑濾波器可以去除圖像的噪聲,使圖像變得模糊。包括:中值濾波、均值濾波、高斯濾波。

高斯濾波、均值濾波去除高斯噪聲。

(3)均值濾波

Python
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)中值濾波

 

Python
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)自編程實現高斯濾波

Python
Image=imread('Letters-a.jpg');
sigma1=0.6; sigma2=10; r=3;  % 高斯模闆的參數
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');
%編寫高斯濾波函數實現
[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)梯度算子

Python
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算子 

Python
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(221),imshow(Image),title('原圖像');
BW= edge(Image,'roberts');
subplot(222),imshow(BW),title('邊緣檢測');
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算子 

Python
Image=im2double(rgb2gray(imread('lotus.jpg')));
subplot(221),imshow(Image),title('原圖像');
BW= edge(Image,'sobel');
subplot(222),imshow(BW),title('邊緣檢測');
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)多個模闆邊緣檢測 

 

Python
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('兩個模闆邊緣檢測');
subplot(222),imshow(edgeImage2),title('八個模闆邊緣檢測');
subplot(223),imshow(sharpImage1),title('兩個模闆邊緣銳化');
subplot(224),imshow(sharpImage2),title('八個模闆邊緣銳化');

(5)Laplacian算子

Python
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銳化圖像');
微信掃一掃加客服

微信掃一掃加客服

點擊啓動AI問答
Draggable Icon