MATLAB濾波代碼

來源:互聯網
上載者:User

clear all;clc;
%FFT變換
I=imread('cameraman.tif');
A1=fft2(I);
A2=ifft2(A1);
A3=uint8(A2);
figure,subplot(221),imshow(I);
subplot(222);imshow(A3);

%DCT變換
I=imread('cameraman.tif');
figure,subplot(223);imshow(I);
I=im2double(I);
T=dctmtx(8);
B=blkproc(I,[8 8], 'P1*x*P2',T,T');
Mask=[1 1 1 1 0 0 0 0
       1 1 1 0 0 0 0 0
       1 1 0 0 0 0 0 0
       1 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0];
B2=blkproc(B,[8 8],'P1.*x',Mask);    % 此處為點乘(.*)
I2=blkproc(B2,[8 8], 'P1*x*P2',T',T);
subplot(224);imshow(I2);

%長條圖均衡
I=imread('pout.tif');
figure,subplot(321);imshow(I);
subplot(322);imhist(I);    
[J,T]=histeq(I,64);      % 映像灰階擴充到0~255,但是只有64個灰階級
subplot(323);imshow(J);
subplot(324);imhist(J);
J=histeq(I,32);
subplot(325);imshow(J);   % 映像灰階擴充到0~255,但是只有32個灰階級
subplot(326);imhist(J);
figure,plot((0:255)/255,T); % 轉移函數的變換曲線

%灰階展開
I=imread('pout.tif');
figure,subplot(221);imshow(I);
subplot(222);imhist(I);
[X,map]=imread('pout.tif');
Y=imadjust(X,[0.1 0.99],[0 1]);
subplot(223);imshow(Y,map);
subplot(224);imhist(Y);

% 映像空間域處理
[I,map] = imread('pout.tif');
J1=imnoise(I,'gaussian',0,0.02);
% 疊加均值為0,方差為0.02的高斯雜訊,可以用localvar代替
%J2=imnoise(I,'salt & pepper',0.04); % 疊加密度為0.04的椒鹽雜訊
M4=[0 1 0; 1 0 1; 0 1 0];
M4=M4/4;                 % 4鄰域平均濾波
I_filter1=filter2(M4,J1);
figure,subplot(321);imshow(I);
subplot(322);imshow(J1);
subplot(323);imshow(I_filter1,map);
M8=[1 1 1; 1 0 1; 1 1 1];      % 8鄰域平均濾波
M8=M8/8;
I_filter2=filter2(M8,J1);
subplot(324);imshow(I_filter2,map);
K1 = medfilt2(J1,[3,3]);
subplot(325);imshow(K1);
K2=medfilt2(J2,[7 7]);
subplot(326);imshow(K2);

%頻域處理
I=imread('pout.tif');
figure,subplot(221);imshow(I);
J1=imnoise(I,'salt & pepper');   % 疊加椒鹽雜訊
subplot(222);imshow(J1);
f=double(J1);     % 資料類型轉換,MATLAB不支援映像的無符號整型的計算
g=fft2(f);        % 傅立葉變換
g=fftshift(g);     % 轉換資料矩陣
[M,N]=size(g);
nn=2;           % 二階巴特沃斯(Butterworth)低通濾波器
d0=50;
m=fix(M/2); n=fix(N/2);
for i=1:M
       for j=1:N
           d=sqrt((i-m)^2+(j-n)^2);
           h=1/(1+0.414*(d/d0)^(2*nn));  % 計算低通濾波器傳遞函數
           result(i,j)=h*g(i,j);
       end
end
result=ifftshift(result);
J2=ifft2(result);
J3=uint8(real(J2));
subplot(223);imshow(J3);                      % 顯示濾波處理後的映像

[I,map]=imread('pout.tif');
figure,subplot(221);imshow(I,map);
H2=[-1 -1 -1;-1 -9 -1;-1 -1 -1];
J1=filter2(H2,I);             % 高通濾波
subplot(222);imshow(J1,map);

I=imread('pout.tif');
figure,subplot(221);imshow(I);
f=double(I);     % 資料類型轉換,MATLAB不支援映像的無符號整型的計算
g=fft2(f);        % 傅立葉變換
g=fftshift(g);     % 轉換資料矩陣
[M,N]=size(g);
nn=2;           % 二階巴特沃斯(Butterworth)高通濾波器
d0=5;
m=fix(M/2);
n=fix(N/2);
for i=1:M
       for j=1:N
           d=sqrt((i-m)^2+(j-n)^2);
           if (d==0)
              h=0;
           else
              h=1/(1+0.414*(d0/d)^(2*nn));% 計算行程函數
           end
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
J2=ifft2(result);
J3=uint8(real(J2));
subplot(222);imshow(J3);  % 濾波後映像顯示
end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.