matlab 特征值不排序,matlap 代码求解释!从这里开始即可%对特征值进行排序并去掉...
%人臉識別代碼
clear all %? ? //removes all variables from the workspace. This frees up system memory.
close all %? ? //Delete specified figure
clc
% number of images on your training set.
%訓練集數(shù)目
M=16;
%Chosen std and mean.
%It can be any number that it is close to the std and mean of most of the images.
um=100;
ustd=80;
%read and show images(bmp);
%讀入M個訓練圖像并顯示在一個窗口上
S=[];? ?%img matrix
temp=[];
figure(1); %??//figure creates figure graphics objects. Figure objects are the individual windows on the screen in which MATLAB displays graphical output. figure creates a new figure object using default property values.
for i=1:M
str=strcat('D:\民族圖形圖像研究\jpg\',int2str(i),'.jpg');? ? %concatenates two strings that form the name of the image int is nteger to string conversion
eval('img=imread(str);');? ?? ? %??//xecute a string containing a MATLAB expression
img=rgb2gray(img); %rgb 轉(zhuǎn)換成為灰度圖像
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i) %??//divides the current figure into rectangular panes m求根行m求根列 i表示第幾個圖像
imshow(img)
if i==3
title('Training set write by 李鋒平','fontsize',18)? ?%??//Add title to current axes 名字 字體尺寸 字體大小
end
drawnow;??%??//Complete pending drawing events??flushes the event queue and updates the figure window.完成待繪圖事件刷新事件隊列和更新的數(shù)字窗口
[irow icol]=size(img);? ?% get the number of rows (N1) and columns (N2)獲得圖像的大小,所有圖像的大小要一致
m=irow;n=icol;
for i=1:m
for j=1:n
a(i,j)=img(i,j);
end
end
temp=reshape(a,irow*icol,1);? ???%creates a (N1*N2)x1 matrix一幅圖像構(gòu)造一個向量 向量的大小和圖像大小有關(guān)
S=[S temp];? ?? ?? ?%X is a N1*N2xM matrix after finishing the sequence??生成一個向量矩陣,M個圖像有M列
%this is our S
end
%Here we change the mean and std of all images. We normalize all images.
%This is done to reduce the error due to lighting conditions.
%下面是對圖像規(guī)范化,更具所有圖像的的平均值和方差
for i=1:size(S,2)
temp=double(S(:,i));
m=mean(temp);
st=std(temp);
S(:,i)=(temp-m)*ustd/st+um; %%%%%
end
%show normalized images 顯示規(guī)范化后的圖像
figure(2);
for i=1:M
str=strcat(int2str(i),'.bmp');??%重新調(diào)整矩陣的行數(shù)、列數(shù)、維數(shù)
img=reshape(S(:,i),irow,icol);
eval('imwrite(img,str)');? ?% 將圖像數(shù)據(jù)寫入到圖像文件中,
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)? ?? ?? ?? ?? ?%subplot是將多個圖畫到一個平面上的工具。其中,m表示是圖排成m行,n表示圖排成n列,也就是整個figure中有n個圖是排成一行的,一共m行,如果第一個數(shù)字是2就是表示2行圖。p是指你現(xiàn)在要把曲線畫到figure中哪個圖上,最后一個如果是1表示是從左到右第一個位置。
imshow(img)
drawnow;
if i==3
title('Normalized Training Set write by 李鋒平','fontsize',18)
end
end
%mean image;顯示平均圖像,所有圖像疊加在一起
m=mean(S,2);? ?%obtains the mean of each row instead of each column
tmimg=uint8(m);? ?%converts to unsigned 8-bit integer. Values range from 0 to 255
img=reshape(tmimg,irow,icol);? ? %takes the N1*N2x1 vector and creates a N2xN1 matrix
figure(3);
imshow(img);
title('Mean Image write by 李鋒平','fontsize',18)
% Change image for manipulation
%對圖像變換便于處理
dbx=[];? ?% A matrix
for i=1:M
temp=double(S(:,i));
dbx=[dbx temp];
end
%Covariance matrix C=A'A, L=AA'
%求協(xié)方差矩陣
%對于PCA做人臉識別:中心化(也就是你說的去均值)后的矩陣再乘以它的轉(zhuǎn)置矩陣,結(jié)果就是協(xié)方差矩陣,之后求它的特征值和特征向量了就可以了...
A=dbx';
L=A*A';
% vv are the eigenvector for L
% dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx';
[vv dd]=eig(L);
% Sort and eliminate those whose eigenvalue is zero
%對特征值進行排序并去掉0? ? 實數(shù)是按照降序排列的,復數(shù)是按照虛部的降序排列的
v=[];
d=[];
for i=1:size(vv,2)
if(dd(i,i)>1e-4)
v=[v vv(:,i)];
d=[d dd(i,i)];
end
end
%sort,??will return an ascending sequence
%排序并返回降序的
[B index]=sort(d);
ind=zeros(size(index));
dtemp=zeros(size(index));
vtemp=zeros(size(v));
len=length(index);
for i=1:len
dtemp(i)=B(len+1-i);
ind(i)=len+1-index(i);
vtemp(:,ind(i))=v(:,i);
end
d=dtemp;
v=vtemp;
%Normalization of eigenvectors
%對特征向量進行規(guī)范化
for i=1:size(v,2)? ?? ? %access each column
kk=v(:,i);
temp=sqrt(sum(kk.^2));
v(:,i)=v(:,i)./temp;
end
%Eigenvectors of C matrix
%得到C的特征向量矩陣
u=[];
for i=1:size(v,2)
temp=sqrt(d(i));
u=[u (dbx*v(:,i))./temp];
end
%Normalization of eigenvectors
for i=1:size(u,2)
kk=u(:,i);
temp=sqrt(sum(kk.^2));
u(:,i)=u(:,i)./temp;
end
% show eigenfaces;
%顯示特征臉
figure(4);
for i=1:size(u,2)
cmax=max(u(:,i));
cmin=min(u(:,i));
img=(reshape(u(:,i),irow,icol)-cmin)*255/(cmax-cmin);
%img=histeq(img,255);
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(uint8(img))
drawnow;
if i==3
title('Eigenfaces write by 李鋒平','fontsize',18)
end
end
% Find the weight of each face in the training set.
%找出訓練集中每張臉的權(quán)重
omega = [];
for h=1:size(dbx,2)
WW=[];
for i=1:size(u,2)
t = u(:,i)';
WeightOfImage = dot(t,dbx(:,h)');
WW = [WW; WeightOfImage];
end
omega = [omega WW];
end
% Acquire new image
% Note: the input image must have a bmp or jpg extension.
%? ?? ? It should have the same size as the ones in your training set.
%? ?? ? It should be placed on your desktop
%獲取一張新的臉
%注意:圖像的大小和訓練集中圖像大小一樣
%
InputImage = input('Please enter the name of the image and its extension \n','s');
InputImage = imread(strcat('D:\民族圖形圖像研究\jpg\',InputImage));
InputImage=rgb2gray(InputImage);
figure(5)
subplot(1,2,1)
imshow(InputImage); colormap('gray');title('Input image','fontsize',18)
InImage=reshape(double(InputImage)',irow*icol,1);
temp=InImage;
me=mean(temp);
st=std(temp);
temp=(temp-me)*ustd/st+um;
NormImage = temp;
Difference = temp-m;
NormImage = Difference;
p = [];
aa=size(u,2);
for i = 1:aa
pare = dot(NormImage,u(:,i));
p = [p; pare];
end
ReshapedImage = m + u(:,1:aa)*p;? ? %m is the mean image, u is the eigenvector
ReshapedImage = reshape(ReshapedImage,irow,icol);
%ReshapedImage = ReshapedImage';
%show the reconstructed image. 顯示重構(gòu)的圖像
subplot(1,2,2)
imshow(uint8(ReshapedImage)); colormap('gray');
title('Reconstructed image write by 李鋒平','fontsize',18)
InImWeight = [];
for i=1:size(u,2)
t = u(:,i)';
WeightOfInputImage = dot(t,Difference');
InImWeight = [InImWeight; WeightOfInputImage];
end
ll = 1:M;
figure(68)
subplot(1,2,1)
stem(ll,InImWeight)
title('Weight of Input Face write by 李鋒平','fontsize',14)
% Find Euclidean distance 查找Euclidean距離
e=[];
for i=1:size(omega,2)
q = omega(:,i);
DiffWeight = InImWeight-q;
mag = norm(DiffWeight);
e = [e mag];
end
kk = 1:size(e,2);
subplot(1,2,2)
stem(kk,e)
title('Eucledian distance of input image,write by 李鋒平','fontsize',14)
MaximumValue=max(e)
MinimumValue=min(e)
總結(jié)
以上是生活随笔為你收集整理的matlab 特征值不排序,matlap 代码求解释!从这里开始即可%对特征值进行排序并去掉...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sscanf取固定长度的int_ssca
- 下一篇: left join 效率_人力资源HR的