圖像的頻域變換**
Matlab圖像顯示方法
圖像的讀寫 %matlab自帶圖像在安裝路徑下 \toolbox\images\imdemos
1:圖像讀
RGB = imread('ngc6543a.jpg');
figure,imshow(RGB);
2:圖像寫
%先從一個(gè).mat 文件中載入一幅圖像激蹲,然后利用圖像寫函數(shù)imwrite驾荣,創(chuàng)建一個(gè).bmp文件燕刻,并將圖像存入其中檐蚜。
load clown
whos
imwrite(X,map,'clown.bmp');
3:圖像文件格式轉(zhuǎn)換
bitmap = imread('clown.bmp','bmp');
imwrite(bitmap,'clown.png','png');
圖像顯示
1:二進(jìn)制圖像的顯示
BW1=zeros(20,20); %創(chuàng)建僅包含0/1的雙精度圖像
BW1(2:2:18,2:2:18)=1;
imshow(BW1,'InitialMagnification','fit'); %double類型[0,1]
BW2=uint8(BW1);
figure,imshow(BW2,'InitialMagnification','fit');
figure,imshow(BW2,[],'InitialMagnification','fit'); %uint8類型[0,255]
BW3=BW2~=0; %邏輯標(biāo)志置為on
figure,imshow(BW3,'InitialMagnification','fit');
2:灰度圖像的顯示
I=imread('spine.tif');
J=filter2([1 2;-1 -2],I); % filters the data in X with the 2D FIR filter in the matrix h.
imshow(I,[]);
figure,imshow(J,[]);
3:索引圖像的顯示
load clown %裝載一幅圖像
imwrite(X,map,'clown.bmp'); %保存為bmp文件
imshow(X);
imshow(X,map);
4:RGB圖像的顯示
RGB=imread('ngc6543a.jpg');
figure,imshow(RGB);
imshow(RGB(:,:,3)); % 顯示第3個(gè)顏色分量
5:多幀圖像的顯示
mri=uint8(zeros(128,128,1,27)); % 27幀文件mri.tif初始化
for frame=1:27
[mri(:,:,:,frame),map]=imread('mri.tif',frame); % 讀入每一幀
end
figure;imshow(mri(:,:,:,3),map); % 顯示第3幀
figure,imshow(mri(:,:,:,6),map); % 顯示第6幀
figure,imshow(mri(:,:,:,10),map); % 顯示第10幀
figure,imshow(mri(:,:,:,20),map); % 顯示第20幀
figure;
hold on;
for frame=1:27
imshow(mri(:,:,:,frame),map); % 讀入每一幀
pause(0.1)
end
hold off
6:顯示多幅圖像
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
figure;
subplot(1,2,1),imshow(X1,map1);
subplot(1,2,2),imshow(X2,map2);
圖像的頻域變換
傅立葉變換
1:繪制一個(gè)二值圖像矩陣,并將其傅立葉函數(shù)可視化魄懂。
f = zeros(30,30);
f(5:24,13:17) = 1;
figure,imshow(f,'InitialMagnification','fit');
F = fft2(f);
F2 = log(abs(F));
figure,imshow(F2,[-1 5],'InitialMagnification','fit');
F=fft2(f,256,256); %零填充為256×256矩陣
figure,imshow(log(abs(F)),[-1 5],'InitialMagnification','fit');
F2=fftshift(F); %將圖像頻譜中心由矩陣原點(diǎn)移至矩陣中心
figure,imshow(log(abs(F2)),[-1 5],'InitialMagnification','fit');
2:利用傅里葉變換恢復(fù)圖像
I=imread('cameraman.tif');
figure,subplot(1,3,1),imshow(I);
F = fft2(I);
I2 = ifft2(F);
subplot(1,3,2),imshow(I2, []);
I3 = ifft2(F./abs(F)); % 幅度譜變?yōu)?
subplot(1,3,3),imshow(I3, []);
離散余弦變換(DCT)
1:使用dct2對(duì)圖像‘a(chǎn)utumn.tif’進(jìn)行DCT變換溶耘。
RGB=imread('autumn.tif');
figure;imshow(RGB);
I=rgb2gray(RGB); %轉(zhuǎn)換為灰度圖像
figure,imshow(I);
J=dct2(I);
figure,imshow(log(abs(J)),[]),colormap(jet(64));colorbar;
2:將上述DCT變換結(jié)果中絕對(duì)值小于10的系數(shù)舍棄谐区,使用idct2重構(gòu)圖像并與原圖像比較叉瘩。
RGB=imread('autumn.tif');
figure,subplot(2,2,1),imshow(RGB);title('原始彩色圖像');
I=rgb2gray(RGB); %轉(zhuǎn)換為灰度圖像
subplot(2,2,2),imshow(I);title('灰度圖像');
J=dct2(I);
K=idct2(J);
subplot(2,2,3),imshow(K,[0 255]);title('離散余弦反變換恢復(fù)圖像');
J(abs(J)<20)=0; %舍棄系數(shù)
K2=idct2(J);
subplot(2,2,4),imshow(K2,[0 255]);title('舍棄系數(shù)后離散余弦反變換恢復(fù)圖像');
3:利用DCT變換進(jìn)行圖像壓縮。
I=imread('cameraman.tif');
I=im2double(I);
T=dctmtx(8); %DCT變換矩陣
fun1 = @(block_struct) T*block_struct.data*T';
B=blockproc(I,[8,8],fun1); %分塊DCT變換
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];
fun2 = @(block_struct) mask.*block_struct.data;
B2=blockproc(B,[8 8],fun2); %每小塊取低頻系數(shù)
fun3 = @(block_struct) T'*block_struct.data*T;
I2=blockproc(B2,[8 8],fun3);
figure,subplot(1,2,1),imshow(I);title('原始圖像');
subplot(1,2,2),imshow(I2);title('離散余弦變換壓縮后恢復(fù)圖像');