一、简介

二、源代码
%
%
% 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);