1. 程式人生 > 其它 >【人臉識別】基於matlab ksvd字典學習人臉表情識別【含Matlab原始碼 460期】

【人臉識別】基於matlab ksvd字典學習人臉表情識別【含Matlab原始碼 460期】

一、簡介

二、原始碼

%
%
% Main Program of KSVD-NN based facial expression recognition.
%
% Ziyang Zhang
 
clear
datadim='37x30';
%datadim='50x40';
%datadim='gabor_all';
testmethod='unfamiliar';
 
 
% prepare image data
[data,label] = PrepareData(datadim,testmethod,9);
 
% direct nearest neighbor classification
testresult = nearestNeighbor( data.train , label.train , data.test );
rate = length( find( ( testresult - label.test ) == 0 ) ) / length(label.test);
clear testresult;
fprintf('\n Direct nearest neighbot on pixel values: rec rate: %f \n',rate);
 
 
% training process using KSVD
param.L = 12;
param.K = 90;
param.numIteration = 20;
param.errorFlag = 0;
param.preserveDCAtom = 0;
param.InitializationMethod = 'DataElements';
param.displayProgress = 1;
disp('Starting to  train the dictionary');
tt=cputime;
[Dictionary,KSVDout]  = KSVD(data.train,param);
fprintf('\ntime of K-SVD: %f\n\n' , cputime - tt);
%I=showdict(Dictionary,[37,30],10,8,'lines') ;
%imshow(I);
%KSVDout.CoefMatrix = full( KSVDout.CoefMatrix );
tt=cputime;
% Using OMP to find the sparse coefficients for test samples
coeftest = OMP(Dictionary,data.test,param.L);
%coeftest = full( coeftest );
 
% nearest neighbor classification
testresult = nearestNeighbor( KSVDout.CoefMatrix , label.train , coeftest );
fprintf('\ntime of testing: %f\n\n' , cputime - tt);
rate = length( find( ( testresult - label.test ) == 0 ) ) / length(label.test);
 
fprintf('\n The result when image dimension: %s    test-method: %s \n' , datadim, testmethod );
fprintf(' L(sparsity of coef) = %d, K(number of atoms) = %d : recognition rate: %f \n\n\n',param.L , param.K , rate);
 
% direct pixal values
testresult = nearestNeighbor( data.train , label.train , data.test );
rate = length( find( ( testresult - label.test ) == 0 ) ) / length(label.test);

三、備註

版本:2014a

完整程式碼或代寫加1564658423