function [RESULT_CLA,iter] = SVMAL_FNCan(hyperdata,Groundtruth,sampleindex,ppara)
% [RESULT_CLA,iter] = SVMAL_FNCan(hyperdata,DataTruth,sampleindex,ppara)
% 主动学习辅助支持向量机分类
%Input:
%   hyperdata:              输入特征
%   DataTruth:              真值图
%   sampleindex:  {trainindex,validindex,testindex}训练，验证，测试样本索引
%   ppara:
%       ppara.a,ppara.b:    图像大小
%       ppara.select_per:   每次从候选样本集中加入的标记样本数
%       ppara.Maxiter:      最大迭代次数
%       ppara.AL_method:    从候选样本集中加入标记样本的方法
%Ouput:
%   RESULT_CLA: struct cells
%       RESULT_CLA{j}.ConfusionMatrix; RESULT_CLA{j}.OA; RESULT_CLA{j}.Kappa
%        RESULT_CLA{j}.producercc; RESULT_CLA{j}.usercc;
%       RESULT_CLA{j}.numofUnlabeled:  每次实际加入的标记样本数
%       RESULT_CLA{j}.indexoferr:  每次加入的错误标签样本数
%       RESULT_CLA{iter+1}: 最终的分类图
%   iter:   实际迭代次数
if ~isfield(ppara,'Maxiter'),   ppara.Maxiter = 20; end
if ~isfield(ppara,'StopCond'),  ppara.StopCond = 0;end
RESULT_CLA = cell(1,ppara.Maxiter);
[Last2Sw,Last1Sw,ThisSw] = deal(zeros(1,size(hyperdata,2)));

%初始标记样本
markedindex = sampleindex.trainindex;
markedlabel = Groundtruth(sampleindex.trainindex);

for iter = 1:ppara.Maxiter
    % 已标记样本的四邻域
    fn_index = [markedindex-1;markedindex+ppara.a;markedindex+1;markedindex-ppara.a];
    fn_label = repmat(markedlabel,4,1);
    [~,indexofdiff] = setdiff(fn_index,markedindex);
    fn_index = fn_index(indexofdiff,:);
    fn_label = fn_label(indexofdiff,:);
    Index = zeros(size(fn_index));
    Index(fn_index >= 1 & fn_index <= ppara.a*ppara.b) = 1;
    fn_index = fn_index((Index == 1));
    fn_label = fn_label((Index == 1));

    [bestc,bestg] = GetSVMcg(hyperdata(markedindex,:),markedlabel,hyperdata(sampleindex.validindex,:),Groundtruth(sampleindex.validindex));
    model = svmtrain(markedlabel,hyperdata(markedindex,:),['-c ',num2str(bestc),' -g ',num2str(bestg),' -b 1 -q']);   
    % 计算测试精度
    [test_prelabel,~,~] = svmpredict(Groundtruth(sampleindex.testindex),hyperdata(sampleindex.testindex,:),model,'-b 1');
    RESULT_CLA{iter} = Confusmat(Groundtruth(sampleindex.testindex),test_prelabel);

    % 对四邻域样本分类
    [fn_prelabel,~,fn_preprob] = svmpredict(fn_label,hyperdata(fn_index,:),model,'-b 1');
    % 确定候选样本集
    correctindex = (fn_prelabel==fn_label);
    candidateindex = fn_index(correctindex);
    candidatelabel = fn_label(correctindex);
    
    %加入的未标记样本个数
    RESULT_CLA{iter}.candidatenumber = length(candidateindex);
    % 利用不确定性选择标记样本
    [newmarkindex,newmarklabel] = ALSelectfromCandidate(candidateindex,candidatelabel,...
        ppara.select_per,fn_preprob(correctindex,:),ppara.AL_method);
    
    % 终止条件:所有测试样本的类内方差不发生改变
    if iter>2
        Sw = SumofSW(hyperdata(sampleindex.testindex,:),test_prelabel);
        ThisSw = Sw;
        if norm(ThisSw - Last1Sw)<=ppara.StopCond && norm(Last1Sw - Last2Sw)<=ppara.StopCond
            break;
        end
    end
    Last2Sw = Last1Sw;   Last1Sw = ThisSw;
    %% 加入标记样本
    markedindex = [markedindex;newmarkindex];
    markedlabel = [markedlabel;newmarklabel];
end
for j = iter+1:20
    RESULT_CLA{j} = RESULT_CLA{iter};
end
% 迭代完成后对图像进行分类
% [hyper_Prelabels,~,~] = svmpredict(zeros(size(Groundtruth)),hyperdata,model,'-b 1');
% RESULT_CLA{iter+1}.hyper_Prelabels = hyper_Prelabels;
