www.gusucode.com > Adaboost算法训练人脸图像和非人脸图像,通过迭代得到由多个弱分类器组合而成的强分类器,实现图片里的人脸检测。 > Adaboost算法训练人脸图像和非人脸图像,通过迭代得到由多个弱分类器组合而成的强分类器,实现图片里的人脸检测。/myfacedet02/demo.m

    %
% DEMONSTRATION OF ADABOOST_tr and ADABOOST_te
%
% Just type "demo" to run the demo.
%
% Using adaboost with linear threshold classifier 
% for a two class classification problem.
%
% Bug Reporting: Please contact the author for bug reporting and comments.
%
% Cuneyt Mertayak
% email: cuneyt.mertayak@gmail.com
% version: 1.0
% date: 21/05/2007
%this is the feature extra
posNO=33
negNO=33;
NOsamples=posNO+negNO;
featureNO=40;
%TempImgaeFeature=zeros(NOsamples,featureNO);
TrainFeature=[];
for i=1:NOsamples
    imageName=strcat('Training\','yale',num2str(i),'.png');
    I=imread(imageName);
    ii=bianli(I);
    ii=buzero(ii,1,1);
    F12=tezhen1(ii); %为矩形特征值数组
    F21=tezhen21(ii);
    F31=tezhen31(ii);
    TrainFeature=[TrainFeature;F12 F21 F31];
end
%<-this is trainfeatures
%->this is testfeatures
    TestFeature=[];
for i=41:80
    %41->80
    %TestFeatures
    imageName=strcat('Test\',num2str(i),'.png');
    I=imread(imageName);
    ii=bianli(I);
    ii=buzero(ii,1,1);
    F12=tezhen1(ii); %为矩形特征值数组
    F21=tezhen21(ii);
    F31=tezhen31(ii);
    TestFeature=[TestFeature;F12 F21 F31];
end
% Creating the training and testing sets
%
tr_n = 66;
te_n = 40;
weak_learner_n =5;

%tr_set = abs(rand(tr_n,2))*100;
tr_set=TrainFeature;
%te_set = abs(rand(te_n,2))*100;
te_set=TestFeature;

%tr_labels = (tr_set(:,1)-tr_set(:,2) > 0) + 1;
tr_labels=[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]';
%te_labels = (te_set(:,1)-te_set(:,2) > 0) + 1;
te_labels=[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]';

% Displaying the training and testing sets

% subplot(2,2,1);
% hold on; axis square;
% indices = tr_labels==1;
% plot(tr_set(indices,1),tr_set(indices,2),'b*');
% indices = ~indices;
% plot(tr_set(indices,1),tr_set(indices,2),'r*');
% title('Training set');

% subplot(2,2,2);
% hold on; axis square;
% indices = te_labels==1;
% plot(te_set(indices,1),te_set(indices,2),'b*');
% indices = ~indices;
% plot(te_set(indices,1),te_set(indices,2),'r*');
% title('Testing set');

% Training and testing error rates
tr_error = zeros(1,weak_learner_n);
te_error = zeros(1,weak_learner_n);

for i=1:weak_learner_n
	adaboost_model = ADABOOST_tr(@threshold_tr,@threshold_te,tr_set,tr_labels,i);
 	[L_tr,hits_tr] = ADABOOST_te(adaboost_model,@threshold_te,tr_set,tr_labels);
	tr_error(i) = (tr_n-hits_tr)/tr_n;
	[L_te,hits_te] = ADABOOST_te(adaboost_model,@threshold_te,te_set,te_labels);
 	te_error(i) = (te_n-hits_te)/te_n;
 %   if i==5
      %   Slide;
 %   end
end
figure;
subplot(1,2,1); 
plot(1:weak_learner_n,tr_error);
axis([1,weak_learner_n,0,1]);
title('Training Error');
xlabel('weak classifier number');
ylabel('error rate');
grid on;

subplot(1,2,2); axis square;
plot(1:weak_learner_n,te_error);
axis([1,weak_learner_n,0,1]);
title('Testing Error');
xlabel('weak classifier number');
ylabel('error rate');
grid on;