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