matlab 二维高斯滤波 傅里叶_光电图像处理 | 傅里叶变换(二)
生活随笔
收集整理的這篇文章主要介紹了
matlab 二维高斯滤波 傅里叶_光电图像处理 | 傅里叶变换(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.頻域濾波與圖像增強
1.1 基本步驟
(1) zero padding
(2) Spectrum centralization
(3) DFT:
(4) Symmetry
(5)
(6) IDFT :
(7) from with size top-left part.
1.2 MATLAB代碼
clc,close?all;I?=?imread('byh.jpg');%?MATLAB庫里面有圖源?cell.tif
I?=?rgb2gray(I);
[m,n]?=?size(I);?%?m為行,n為列
J?=?zeros(2*m,2*n);
%?進行擴展
for?i=1:m
????for?j=1:n
????????J(i,j)?=?I(i,j);
????end
end
%?中心化處理?Spectrum?ccentralization
K?=?J;
for?i=1:m
????for?j=1:n
????????K(i,j)?=?I(i,j)*(-1).^(i+j);
????end
end
%?傅里葉變換
F?=?log(1+fft2(im2double(K)));
%?H?函數
H?=?zeros(2*m,2*n);
square?=?70;?%?濾波函數?H?的邊長?
H(m-square:m+square,n-square:n+square)=1;
%?相乘
FF?=?F?.*?H;
%?反傅里葉變換
ff?=?ifft2(exp(FF)-1);
ff_r?=?real(ff);
ff_final?=?ff_r;
for?i=1:2*m
????for?j=1:2*n
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
subplot(241),imshow(I,[]),xlabel('(a)?原圖');
subplot(242),imshow(J,[]),xlabel('(b)?進行擴展操作后的圖片');
subplot(243),imshow(K,[]),xlabel('(c)?頻域中心化后的圖像');
subplot(244),imshow(F,[]),xlabel('(d)?傅里葉變換后的圖像');
subplot(245),imshow(H,[]),xlabel('(e)?自制的低通濾波器');
subplot(246),imshow(FF,[]),xlabel('(f)?低通濾波處理后的圖像');
subplot(247),imshow(ff_final,[]),xlabel('(g)?反傅里葉變換后的圖像');
subplot(248),imshow(ff_final(1:m,1:n),[]),xlabel('(h)?取(g)圖中的左上部分');
result:
“如果不進行擴展,直接進行(低通)濾波,結果如下:
clc,close?all;I?=?imread('byh.jpg');%?MATLAB庫里面有圖源?cell.tif
I?=?rgb2gray(I);
[m,n]?=?size(I);?%?m為行,n為列
J?=?zeros(m,n);
%?進行擴展
for?i=1:m
????for?j=1:n
????????J(i,j)?=?I(i,j);
????end
end
%?中心化處理?Spectrum?ccentralization
K?=?J;
for?i=1:m
????for?j=1:n
????????K(i,j)?=?I(i,j)*(-1).^(i+j);
????end
end
%?傅里葉變換
F?=?log(1+fft2(im2double(K)));
%?H?函數
H?=?zeros(m,n);
square?=?35;?%?濾波函數?H?的邊長?
H(round(m/2)-square:round(m/2)+square,round(n/2)-square:round(n/2)+square)=1;
%?相乘
FF?=?F?.*?H;
%?反傅里葉變換
ff?=?ifft2(exp(FF)-1);
ff_r?=?real(ff);
ff_final?=?ff_r;
for?i=1:m
????for?j=1:n
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
subplot(241),imshow(I,[]),xlabel('(a)?原圖');
subplot(242),imshow(J,[]),xlabel('(b)?進行擴展操作后的圖片');
subplot(243),imshow(K,[]),xlabel('(c)?頻域中心化后的圖像');
subplot(244),imshow(F,[]),xlabel('(d)?傅里葉變換后的圖像');
subplot(245),imshow(H,[]),xlabel('(e)?自制的低通濾波器');
subplot(246),imshow(FF,[]),xlabel('(f)?低通濾波處理后的圖像');
subplot(247),imshow(ff_final,[]),xlabel('(g)?反傅里葉變換后的圖像');
subplot(248),imshow(ff_final(1:m,1:n),[]),xlabel('(h)?取(g)圖中的左上部分');
“
只修改了部分代碼,表達清楚意思即可hhh ?;-)
result:
2 頻域濾波器分類
2.1 低通濾波器
“理想低通濾波器(ILPF)特點:物理上不可實現;有抖動現象;濾除高頻成分使圖像變模糊
其中,
D0為截止頻率;P = 2M,Q = 2N,M,N為待濾波圖像的尺寸。
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?ILPF_ZH(P,Q,70);?%?濾波矩陣?/?系統傳遞函數
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')???
%%?理想低通濾波器
function?H?=?ILPF_ZH(P,Q,D0)
%?P?=?2551;Q?=?2551;%?P和Q均為奇數
H?=?zeros(P,Q);
%?D0?=?20;
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????if?D?????????????H(i,j)?=?1;
????????end
????end
end
end
RESULT
“巴特沃思(Butterworth)低通濾波器 BLPF
其中,
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?BLPF_ZH(P,Q,70,2);?%?巴特沃思(Butterworth)低通濾波矩陣?/?系統傳遞函數
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')???
%%?巴特沃思(Butterworth)低通濾波器
function?H?=?BLPF_ZH(P,Q,D0,n)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1/(1+(D/D0)^(2*n));
????end
end
end
RESULT
“高斯低通濾波器(GLPF)
其中,
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?GLPF_ZH(P,Q,50);?????%?高斯低通濾波器(GLPF)
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')???
%%?高斯低通濾波器(GLPF)
function?H?=?GLPF_ZH(P,Q,D0)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?exp(-(D)^2?/?(2?*?D0^2));
????end
end
end
RESULT
2.2 高通濾波器
通常,頻域高通濾波器可以看成是頻域低通濾波器的反操作,即
“理想高通濾波器(IHPF)
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?IHPF_ZH(P,Q,15);???%?理想高通濾波矩陣?/?系統傳遞函數
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')??
%%?理想高通濾波器
function?H?=?IHPF_ZH(P,Q,D0)
H?=?ones(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????if?D?????????????H(i,j)?=?0;
????????end
????end
end
end
RESULT
“巴特沃思(Butterworth)高通濾波器
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?BHPF_ZH(P,Q,8,2);?%?巴特沃思(Butterworth)高通濾波矩陣?/?系統傳遞函數
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')???
%%?巴特沃思(Butterworth)高通濾波器
function?H?=?BHPF_ZH(P,Q,D0,n)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1/(1+(D0/D)^(2*n));
????end
end
end
RESULT
“高斯高通濾波器(GLPF)
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
H?=?GHPF_ZH(P,Q,8);???%?高斯高通濾波器(GLPF)
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')?
%%?高斯高通濾波器(GHPF)
function?H?=?GHPF_ZH(P,Q,D0)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1?-?exp(-(D)^2?/?(2?*?D0^2));
????end
end
end
RESULT
匯合在一起的代碼如下:
clc,close?all;I?=?imread('kobi.png');%?MATLAB庫里面有圖源?cell.tif?byh.jpg
I?=?rgb2gray(I);
[P,Q]?=?size(I);?%?m為行,n為列
F?=?log(1+fftshift(fft2(I)));?%?顯示頻譜
%?H?=?ILPF_ZH(P,Q,70);???%?理想低通濾波矩陣?/?系統傳遞函數
%?H?=?BLPF_ZH(P,Q,70,2);?%?巴特沃思(Butterworth)低通濾波矩陣?/?系統傳遞函數
%?H?=?GLPF_ZH(P,Q,50);?????%?高斯低通濾波器(GLPF)
%?H?=?IHPF_ZH(P,Q,15);???%?理想高通濾波矩陣?/?系統傳遞函數
%?H?=?BHPF_ZH(P,Q,8,2);?%?巴特沃思(Butterworth)高通濾波矩陣?/?系統傳遞函數
H?=?GHPF_ZH(P,Q,8);???%?高斯高通濾波器(GLPF)
F_new?=?F?.*?H;?%?濾波后的頻譜
F_ishift?=?ifft2(exp(F_new)-1);
ff_r?=?real(F_ishift);
ff_final?=?ff_r;
for?i=1:P
????for?j=1:Q
????????ff_final(i,j)?=?ff_final(i,j)?*?(-1)^(i+j);
????end
end
imshow(ff_final,[])
subplot(231),imshow(I,[]),xlabel('(a)?原圖');????
subplot(232),imshow(F,[]),xlabel('(b)?原圖的頻譜');???
subplot(233),imshow(H,[]),xlabel('(c)?濾波器');????
subplot(234),x=1:1:P;y=1:1:Q;[X,Y]=meshgrid(x,y);mesh(X,Y,H');xlabel('(d)?濾波器三維展示');???
subplot(235),imshow(F_new,[]),xlabel('(e)?濾波后的頻譜');????
subplot(236),imshow(ff_final,[]),xlabel('(f)?濾波后的圖像')???
%%?理想低通濾波器
function?H?=?ILPF_ZH(P,Q,D0)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????if?D?????????????H(i,j)?=?1;
????????end
????end
end
end
%%?巴特沃思(Butterworth)低通濾波器
function?H?=?BLPF_ZH(P,Q,D0,n)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1/(1+(D/D0)^(2*n));
????end
end
end
%%?高斯低通濾波器(GLPF)
function?H?=?GLPF_ZH(P,Q,D0)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?exp(-(D)^2?/?(2?*?D0^2));
????end
end
end
%%?理想高通濾波器
function?H?=?IHPF_ZH(P,Q,D0)
H?=?ones(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????if?D?????????????H(i,j)?=?0;
????????end
????end
end
end
%%?巴特沃思(Butterworth)高通濾波器
function?H?=?BHPF_ZH(P,Q,D0,n)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1/(1+(D0/D)^(2*n));
????end
end
end
%%?高斯高通濾波器(GHPF)
function?H?=?GHPF_ZH(P,Q,D0)
H?=?zeros(P,Q);
for?i=1:P
????for?j=1:Q
????????D?=?((i?-?round((P+1)/2))^2?+?(j?-?round((Q+1)/2))^2)^0.5;
????????H(i,j)?=?1?-?exp(-(D)^2?/?(2?*?D0^2));
????end
end
end
END
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的matlab 二维高斯滤波 傅里叶_光电图像处理 | 傅里叶变换(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: richedit line insert
- 下一篇: python画误差棒_给妹子讲pytho