1. 程式人生 > 其它 >【影象處理】基於matlab影象正交變換【含Matlab原始碼 1010期】

【影象處理】基於matlab影象正交變換【含Matlab原始碼 1010期】

一、簡介








































































































二、原始碼

clear,clc,close all;
Image=rgb2gray(imread('cameraman.jpg'));
subplot(121),imshow(Image),title('原影象');
[ca,ch,cv,cd]=dwt2(Image,'db4');                   %用db4小波對影象進行一級小波分解
result=idwt2(ca*0,ch,cv,cd,'db4')/256;    
 
Image=imread('desert.jpg');%讀取影象
grayI=rgb2gray(Image);%將彩色影象灰度化
DFTI1=fft2(grayI);
ADFTI1=abs(DFTI1);
top=max(ADFTI1(:));
bottom=min(ADFTI1(:));
 
subplot(131),imshow(Image),title('原圖');%顯示原影象
subplot(132),imshow(ADFTI1),title('原頻譜圖');%顯示傅立葉變換頻譜圖
subplot(133),imshow(ADFTI2),title('移位頻譜圖');%顯示傅立葉變換頻譜圖
% imwrite(ADFTI1,'dftpinpu2_1.jpg');
% imwrite(ADFTI2,'dftpinpu2_2.jpg');
clear,clc,close all;
fmt={'*.jpg','JPEG image(*.jpg)';'*.*','All Files(*.*)'};
[FileName,FilePath]=uigetfile(fmt,'匯入資料','face*.jpg','MultiSelect','on');
if ~isequal([FileName,FilePath],[0,0])
    FileFullName=strcat(FilePath,FileName);
else 
    return;
end 
N=length(FileFullName); 
for k=1:N
    Image=im2double(rgb2gray(imread(FileFullName{k})));
    X(:,k) = Image(:);           % 把影象放在矩陣x的第k列
end
[h,w,c]=size(Image);
% %----------- 計算每幅訓練影象的與平均臉的差值 -------%
averagex = mean(X')';             %計算均值影象 
X=X-averagex;                     % 求中心化影象向量

%-----奇異值分解方法計算協方差矩陣的特徵值和特徵向量----%
R = X'*X;                   %協方差矩陣為x*x’,這裡用奇異值分解
[Q,D] = eig(R);         %V為以特徵向量為列的矩陣,D為特徵值組成的對角陣
[D_sort,index] = sort(diag(D),'descend');
D=D(index,index);
Q = Q(:,index);
P = X*Q*(abs(D))^-0.5;

total = 0.0;
count = sum(D_sort);
for r =1:N
    total = total + D_sort(r);
    if total/count > 0.95        %當差異資訊比例達到85%時退出迴圈
        break;
    end
end
%-------------測試樣本在新空間上投影后的座標-----------%
KLCoefR = P'*X;
figure; plot(KLCoefR(1,:),KLCoefR(2,:),'ko'),title('K-L變換行壓縮');
xlabel('第一主成分得分');ylabel('第二主成分得分');
Y= P(:,1:2)*KLCoefR(1:2,:)+averagex;                     %重建
for j=1:N
    outImage=reshape(Y(:,j),h,w);
%     top=max(outImage(:));
%     bottom=min(outImage(:));
%     outImage=(outImage-bottom)/(top-bottom);
%     str=strcat('feaface12_',num2str(j),'.jpg');
%     imwrite(outImage,str);
    figure,imshow(outImage,[]);
end
Z= P(:,1:r)*KLCoefR(1:r,:)+averagex;                     %重建
for j=1:N
    outImage=reshape(Z(:,j),h,w);
%     top=max(outImage(:));
%     bottom=min(outImage(:));
%     outImage=(outImage-bottom)/(top-bottom);
%     str=strcat('feaface1r_',num2str(j),'.jpg');
%     imwrite(outImage,str);
    figure,imshow(outImage,[]);
 
for j =1:N
    outImage=reshape(KLCoefC(:,j),h,w);
%     top=max(outImage(:));
%     bottom=min(outImage(:));
%     outImage=(outImage-bottom)/(top-bottom);
%     str=strcat('feaface',num2str(j),'.jpg');
%     imwrite(outImage,str);
    figure,imshow(outImage,[]);
end
% 
% 


三、執行結果





















四、備註

版本:2014a
完整程式碼或代寫加1564658423