一、简介
二、源代码
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