%% 基于PLS子空间模型的算法
%% Multiple templates
%% Copyright (C)Qing Wang.
%% All rights reserved.
%% 准备工作
clear all;
close all;
clear;
clc;
iptsetpref('ImshowBorder', 'tight'); %去掉图像与窗口之间的边框
% iptsetpref('ImshowBorder', 'loose'); %去掉图像与窗口之间的边框

%读入图像地址
% prefix = 'I:\test video\dudek\';
% prefix = 'O:\test video\PETS2009\S2_L1\Crowd_PETS09\S2\L1\Time_12-34\View_001\';
% prefix = 'I:\test video\MIL\faceocc2\imgs\';
prefix = 'D:\Projects\Sequences\girl\'; 


%图像集合中图像的数目
D = dir(prefix);
fNum = size(D,1)-4; %实际是图像数目-1，因为图像的索引从0开始

%设置存储跟踪结果的地址
prefix_result = 'result\';
% start frame
fstart = 1;  %wq15-259开始， wCAVIAR-ThreePastShop2cor-393，wq9从112帧开始, liquor_468, faceocc2_8
tmpSZ = 32; %初始标准模板的大小是32*32
% first frame的地址索引
fname = sprintf('%s%04d.jpeg',prefix,fstart);  %自己的视频序列
% fname = sprintf('%s%03d.jpeg',prefix,fstart);  %david
% fname = sprintf('%s%s%05d.png',prefix, 'img', fstart); %faceocc
% fname = sprintf('%s%05d.jpg',prefix,fstart);  %prost
% fname = sprintf('%s%s%04d.jpg',prefix, 'frame_', fstart); %pets2009, VTD

%读入第一帧图像
im = imread(fname);
f_frame = sprintf('%d',1);
fprintf('tracking frame: 第%s帧\n', f_frame); 

figure(1);
imshow(im);
frame = rgb2gray(im);
% frame = im;
mapping=getmapping(8,'u2'); 
% frame = lbp(frame, 1, 8, mapping);
frame =  double(frame)/256;


% imshow(frame);
hwin=gcf;
% 窗口左上角显示第#帧
Current_title = sprintf('第%d帧',fstart);
set(hwin, 'Name', Current_title);  
% 图像上显示帧数
Current_title = sprintf('# %d',fstart);
text(5, 18, Current_title, 'Color','y', 'FontWeight','bold', 'FontSize',12);

%% 手动选择目标的位置
% % 优点-自由灵活；缺点：只能选择矩形区域的目标
% %get 4 vertices of object
% fprintf('Please click 4 corners of the object to track:');
% [xbox,ybox] = ginput(4);  %user inputs corners of bounding box。取多少个点都可以的，记录的是他们的相应的横纵坐标
% %get object bounding box获取目标的边框，选取最边界的框.
% xbox = round(xbox);   %get integer value
% ybox = round(ybox);
% left = min(xbox);    
% right = max(xbox); 
% top = min(ybox);     
% bottom = max(ybox);
% % objBox = [left,right,top,bottom];      %objBox是由左上角和右下角的坐标构成的向量
% %计算目标区域的中心和宽高，并且设定mu的剩余参数
% objCenterx = round((left+right)/2);
% objCentery = round((top+bottom)/2);
% width = right-left;
% height = bottom-top;
% %参数初始化
% p = [objCenterx, objCentery, width, height, 0];  %wq9 中心，宽、高，旋转角度。

%% 预设目标的初始位置
% p = [188,192,110,130,-0.08];
% p = [206, 95, 40, 48, 0];  %wq9 112帧
% p = [84,94,48,65,-0.08];  %wq10 
% p = [366,187,83,281,0];  %singer2 
% p = [160,106,75,97,0];    %David
% p = [149,91,119,154,0]; %faceocc2
% p = [158.21, 107.506, 92, 116, 0];%faceocc2
% p = [652,283.5,44,87,0]; %PETS2009
% p = [200 100 45 49 0]; %Trellis
% p = [146, 82, 57, 61, -0.2]; %sylvester
% p = [145 81 53 53 -0.2]; %sylvester
% p = [269, 86, 42, 56, -0.1];  %wq9 
% p = [265, 86, 62, 82, 0];  %wq9 
% p = [108,88,94,76,0];  %IVT_car
% p = [129, 163, 43, 51, 0];  %CAVIAR-ThreePastShop2cor
% p = [156, 110, 26, 48, 0];  %NBA 
% p = [150,96,26,68,0]; %caviar2
% p = [201, 112, 65, 30, 0.2]; %MB_CAR3
% p = [60,100,27,49,0];  %surf 
% p = [286,152.5,32,35, 0]; %MIL_surf
% p =  [89 140 30 25 0]; %IVT_car11
% p = [109, 91, 60, 88, 0.10];  %wq15 
% p = [291,255,63,211,0];  %board 
% p = [178,106,91,113,0];  %nv-Birchfield-seq_mb
p =[180,109.5,104,127,0];% girl_MILT
%  p = [108,103,74,58,0];  %car2

% p = [287,280,64,127,0]; %liquor_468
% p = [50, 147, 48, 20, -0.16];  %wq24 
% p = [255, 57, 66, 81, -0.3];  %WallE
% p = [246, 180, 33, 33, 0];  %WallE2
% p = [91,70,24,61,0]; %square

%%
% 选择negative samples的内外环
alpha=0:pi/2000:2*pi;%角度[0,2*pi]
num = size(alpha,2);
% R=sqrt(p(3)^2 + p(4)^2)/2;%半径
R=sqrt(p(3)^2+p(4)^2)/2+sqrt(tmpSZ^2+tmpSZ^2)/2;
x=R*cos(alpha) + p(1)*ones(1,num);
y=R*sin(alpha) + p(2)*ones(1,num);
R2 = 1.5*R;
x2=R2*cos(alpha) + p(1)*ones(1,num);
y2=R2*sin(alpha) + p(2)*ones(1,num);


%% 仿射运动参数转换
param0 = [p(1), p(2), p(3)/tmpSZ, p(5), p(4)/p(3), 0];
aTest = param0;
param0 = affparam2mat(param0);

bTest = affparam2geom(param0);

%% 其他参数设置
%粒子滤波的相关参数设置
% opt = struct('numsample',600, 'condenssig',0.25, 'ff',1, ...
%     'batchsize',5,'maxbasis',16,'tmplsize', [32,32],'affsig',[4, 4, .01, .05, .0025, .001],'errfunc','L2');
opt = struct('numsample', 600, 'condenssig',1, 'posNum',1, 'negNum',30, ...
    'tmplsize', [32,32], 'batchsize',15,'projNum',10, 'num_tmpl',1, 'r',1.5, 'posSig',[2,2,.01,.02,.002,.001]);

%condenssig的设置成协方差形式的话，貌似就转化成马氏距离度量

%高斯运动模型的协标准差均值对角线元素单独选择
% 6个参数的意义分别为：中心X的标准差，中心Y的方差,宽/tmpSZ的标准差，旋转角度的方差，高/宽的方差，剪切角的方差
% opt.affsig = [4, 4, .01, .05, .0025, .001]; %wq10
% opt.affsig = [6, 6, .01, .01, .001, .001];  %wq10
% opt.affsig = [2,2,.005,0,0,0]; %singer1-2
% opt.affsig = [4, 2, .01, .0, .0, .0]; %David_indoor
% opt.affsig = [4, 2, .005, 0, 0, 0]; %David_indoor
% opt.affsig = [6,6,0.005,.05,0,0]; %faceocc2
% opt.affsig = [6, 6, .005, .05, .01, 0]; %sylvester
% opt.affsig = [7,7,.01,.02,.002,.001]; %sylvester
% opt.affsig = [4, 4, .005, 0, .001, .0]; %wq9'
% opt.affsig = [4, 3, .05, .0, .0, .0]; %IVT_car
% opt.affsig = [2, 2, .01, .0, .0, .0]; %IVT_car
% opt.affsig = [8, 4, .01, .0, .001, .0];   %NBA
% opt.affsig = [4, 2, 0, 0, 0, 0]; %Caviar2
% opt.affsig = [8,6,.05,.05,0,0]; %MB_CAR3
% opt.affsig = [6,6,.005,0,.001,0]; %surfing
% opt.affsig = [6,6,.01,0,.001,0]; %MIL_surfing
% opt.affsig = [5,5,.01,0, 0, 0];  %IVT_car11
% opt.affsig = [7, 4, .008, .00, .000, .000]; %CAVIAR-ThreePastShop2cor
% opt.affsig = [7, 10, .001, .0, .001, 0]; %pets2009
% opt.affsig = [6, 6, .001, .0, 0, 0]; %pets2009
% opt.affsig = [4,4,.01,.01,.002,.001]; %Trellis
% opt.affsig = [6, 6, .02, .001, .001, .001];   %wq15
% opt.affsig = [6, 6, .01, .001, .001, .001];   %wq15
% opt.affsig = [17, 12, .01, .01, .001, .0];   %prost
% opt.affsig = [4, 6, .05, .01, .001, 0]; %nv-Birchfield-seq_mb
opt.affsig = [4, 4, .01, .0, .0, .0]; %car2
% opt.affsig = [12, 12, .01, .01, .001, .0];   %liquor
% opt.affsig = [6, 6, .01, .01, .001, .0];   %liquor
% opt.affsig = [6, 6, .01, .05, .0025, .001];  %wq24
% opt.affsig = [8, 8, .02, .02, .01, .001];  %WallE
% opt.affsig = [8, 8, .05, .05, .1, 0];  %WallE
% opt.affsig = [8, 8, .1, .005, .001, .0];  %WallE2
% opt.affsig = [8, 6, .1, 0, .005, .0];  %WallE2
% opt.affsig = [5,5,.01,0, 0, 0];  %IVT_car11
% opt.affsig = [6,6,.02, 0, 0, 0];  %square


step = 1; %跟踪的帧率
% wimgs = []; %用来临时存储用于更新模型的目标图像

% frame = rgb2gray(im);
% frame =  double(frame)/256;
%% 选择一个正样本
% posSample = warpimg(frame, param0, opt.tmplsize);%正样本图像
% figure(2);
% imshow(posSample);
%% 选择多个正样本

% [posSample, posPosition] = SelectPos(frame, opt.tmplsize, param0, opt.posNum, posSig);
[posSample, posPosition] = SelectPos(frame, param0, opt);
%显示正样本
for i = 1:opt.posNum
    figure(1);
    %SD.position分别是上，左，下，右，要那左、右、上、下的顺序排列。    
    drawbox(opt.tmplsize, posPosition(:,i), 'Color','g', 'LineWidth',2.5);
end
pause;
% 显示正样本
mri = zeros(opt.tmplsize(1),opt.tmplsize(2),1,opt.posNum);
mri(:,:,1,:)=posSample(:,:,:);
figure(2);
montage(mri);

%% 选择负样本
% [negSample,position] = SelectNeg(frame,p(1),p(2),p(3),p(4),opt.tmplsize,opt.negNum,opt.r);
% [negSample,position] = SelectNeg(frame, param0, tmpSZ, opt.tmplsize, opt.negNum, opt.r);
[negSample, position] = SelectNeg(frame, param0, opt);

%% 对正样本和负样本进行特征选择
% FFF = FeatureExtract(posSample,7);

%% 绘制初始的跟踪窗口，保存第一帧的跟踪结果
figure(1);
drawbox(opt.tmplsize, param0, 'Color','r', 'LineWidth',2.5);

%画出内外环
% figure(1);
% hold on; 
% % h=plot(xfoo,yfoo,color); set(h,); 
% plot(x,y, '--b', 'LineWidth',2); 
% yy = y2;
% ind = find(y2<0);
% yy(ind) = zeros(1,579);
% plot(x2,yy, '--b', 'LineWidth',2); 
% hold off; drawnow;
% axis equal

%保存第一帧图像的标定结果
wname = sprintf('%s%04d.jpeg', prefix_result, fstart); %保存实验结果
imwrite(frame2im(getframe(gcf)),wname);
% 画出所有采集的负样本的位置和大小
% for i = 1:opt.negNum
%     figure(1);
%     %SD.position分别是上，左，下，右，要那左、右、上、下的顺序排列。    
%     drawboxmm(position(1,i),position(2,i),position(3,i),position(4,i),'-b',2);
% end
for i = 1:opt.negNum
    figure(1);
    %SD.position分别是上，左，下，右，要那左、右、上、下的顺序排列。    
    drawbox(opt.tmplsize, position(:,i), 'Color','b', 'LineWidth',2.5);
end
% 显示负样本
% mri = zeros(opt.tmplsize(1),opt.tmplsize(2),1,opt.negNum);
% mri(:,:,1,:)=negSample(:,:,:);
% figure(3);
% montage(mri, 'Size',[1 30]);

%% 计算目标模型
% [W, ObjRef, muX, sigmaX, muY, sigmaY, mri_neg] = PLS_new(frame, p, 31, 31);
% [tmpl, projNeg] = PLS_sub(posSample, negSample, opt.projNum);
tmp = PLS_sub(posSample, negSample, opt);
tmpl0 = tmp;
tmpl.mean = zeros(opt.tmplsize(1),opt.tmplsize(2),opt.num_tmpl);
tmpl.mean_pos = zeros(opt.tmplsize(1),opt.tmplsize(2),opt.num_tmpl);
tmpl.template = zeros(opt.projNum,opt.num_tmpl);
tmpl.W = zeros(opt.tmplsize(1)*opt.tmplsize(2), opt.projNum,opt.num_tmpl);
tmpl.sigma = zeros(opt.tmplsize(1)*opt.tmplsize(2),opt.num_tmpl);
tmpl.mean(:,:,1) = tmp.mean;
tmpl.mean_pos(:,:,1) = posSample(:,:,opt.posNum);
tmpl.template(:,1) = tmp.template;
tmpl.W(:,:,1) = tmp.W;
tmpl.sigma(:,1) = tmp.sigma;


param = [];
param.est =  param0'; %估计的参数，矩阵形式
paramRes = []; % 用于存储每帧图像的跟踪结果，6个参数，为矩阵形式
paramRes = [paramRes param.est];


%% 绘制第一主轴方向对应的权重图
weight = tmpl.W(:,1);
%wq-0724
% nor = sqrt(weight'*weight);
% weight = weight/nor;  %归一化
%wq-0726
%查找正值的个数，看其所占weight总数的比例
sz = size(weight);
sz = sz(1)*sz(2);
numP = find(weight>0);
numP = size(numP, 1);
if numP/sz < 0.5
    weight = -weight;
end

%将其变换到[0,1]区间。
weight = weight/max(max(weight));
weight = reshape(weight,opt.tmplsize);
% figure(4);
% subplot(1,2,1), subimage(posSample(:,:,1));
% title('Warped target image');
% subplot(1,2,2), subimage(weight*1);
% colormap(jet);
title('w1');
figure(10);
image(im2uint8(weight));
axis equal
% image(weight*256);
colormap(jet);
% pause;

posSampleTotal = zeros(opt.tmplsize(1), opt.tmplsize(2),  opt.batchsize);
posSampleTotal(:,:,1) = posSample(:,:,opt.posNum);
% mean = posSample(:,:,opt.posNum);
% mean0 = mean;
% wimgs = [];
f = 0.9;% forgetting factor
mean = zeros(opt.tmplsize(1), opt.tmplsize(2));
% tmplo = tmpl;
count_update = 0;

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                             %跟踪算法
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fNum = 600;
%从第2帧开始进行跟踪
for frameIndex = fstart+1:step:fNum
    frameIndex1 = mod(frameIndex,opt.batchsize);  %用来索引ImStack中的元素，取值1～9，然后是0。当mode为1的时候进行更新
    % read the next frame
    fname = sprintf('%s%04d.jpeg',prefix,frameIndex);  %自己的视频序列
%     fname = sprintf('%s%03d.jpeg',prefix,frameIndex);  %David
%     fname = sprintf('%s%s%05d.png',prefix, 'img', frameIndex); %faceocc
%     fname = sprintf('%s%05d.jpg',prefix,frameIndex);  %prost
%     fname = sprintf('%s%s%04d.jpg',prefix, 'frame_', frameIndex);
%     fname = sprintf('%s%s%04d.jpg',prefix, 'frame_', frameIndex); %pets2009, VTD


    wname = sprintf('%s%04d.jpeg', prefix_result, frameIndex); %保存实验结果
    im = imread(fname);
    figure(1);
    imshow(im);
    %在命令窗口输出帧数的显示
    f_frame = sprintf('%d',frameIndex);
    fprintf('tracking frame: 第%s帧\n', f_frame); 
    % 在跟踪窗口显示帧数
    Current_title = sprintf('# %d',frameIndex);
    set(hwin, 'Name', Current_title);
    % 在图像上显示帧数
    text(5, 18, Current_title, 'Color','y', 'FontWeight','bold', 'FontSize',12);    

    %转换成灰度图像    
    frame = rgb2gray(im);     
%     frame = im;

    %都进行unit8到double的转化
%     frame = lbp(frame, 1, 8, mapping);
    frame =  double(frame)/256;
    
    % 使用粒子滤波算法实现跟踪
    % no pls
%     param = estwarp_condens(frame, tmpl, param, opt);  %在此函数中需要更改使用的观测模型


    param = est_condens_PLS_Multi(frame, tmpl, param, opt);
%     tmp = estwarp_condens_PLS(frame, tmpl0, param, opt);  %在此函数中需要更改使
%     param.est = tmp.est;
%     param.wimg = tmp.wimg;

 
    tmp = tmpl.template;
    tmp(:,sum(abs(tmpl.template),1)==0) = [];
    num_tmpl = size(tmp,2);
    min_tmplate = tmpl.template(:,param.mini_t_idx);
%     %% update only when the distance keeps large for a few frames
%     if param.mini_t_dis < sum(min_tmplate.^2)/5 % update the  param.mini_t_idx template      
%         count_update = count_update + 1;
%         mean = mean*0.5 + (1-0.5)*param.wimg;
%     end
%     if count_update == opt.batchsize %if the distance is larger than the threshold for 10 frames.        
% %     if param.mini_t_dis < sum(min_tmplate.^2)/5 % update the  param.mini_t_idx template      
%         tmpl.mean_pos(:,:,param.mini_t_idx) = tmpl.mean_pos(:,:,param.mini_t_idx)*f + (1-f)*mean;
%         [negSample,position] = SelectNeg(frame, param.est, opt);
%         tmp_t = PLS_sub(tmpl.mean_pos(:,:,param.mini_t_idx), negSample, opt);
%         tmpl.mean(:,:,param.mini_t_idx) = tmp_t.mean;
%         tmpl.template(:,param.mini_t_idx) = tmp_t.template;
%         tmpl.W(:,:,param.mini_t_idx) = tmp_t.W;
%         tmpl.sigma(:,param.mini_t_idx) = tmp_t.sigma;                    
%     else  %add a new subspace and remove the subspace with largest reconstruction error
%         if num_tmpl < opt.num_tmpl % if the number of subspace is less than the setting
%             tmpl.mean_pos(:,:,num_tmpl+1) = mean;
%             [negSample,position] = SelectNeg(frame, param.est, opt);
%             tmp_t = PLS_sub(mean, negSample, opt);
%             tmpl.mean(:,:,num_tmpl+1) = tmp_t.mean;
%             tmpl.template(:,num_tmpl+1) = tmp_t.template;
%             tmpl.W(:,:,num_tmpl+1) = tmp_t.W;
%             tmpl.sigma(:,num_tmpl+1) = tmp_t.sigma;
%         else
%             if param.max_t_idx ~= 1  % test_0521
%                 tmpl.mean_pos(:,:,param.max_t_idx) = mean;
%                 [negSample,position] = SelectNeg(frame, param.est, opt);
%                 tmp_t = PLS_sub(mean, negSample, opt);
%                 tmpl.mean(:,:,param.max_t_idx) = tmp_t.mean;
%                 tmpl.template(:,param.max_t_idx) = tmp_t.template;
%                 tmpl.W(:,:,param.max_t_idx) = tmp_t.W;
%                 tmpl.sigma(:,param.max_t_idx) = tmp_t.sigma;
%             end
%         end     
%         count_update = 0;    
%         mean = zeros(opt.tmplsize(1), opt.tmplsize(2));
%     end

%% Update in each frame
    if param.mini_t_dis < sum(min_tmplate.^2)/5 % update the  param.mini_t_idx template      
        tmpl.mean_pos(:,:,param.mini_t_idx) = tmpl.mean_pos(:,:,param.mini_t_idx)*f + (1-f)*param.wimg;
        [negSample,position] = SelectNeg(frame, param.est, opt);
        tmp_t = PLS_sub(tmpl.mean_pos(:,:,param.mini_t_idx), negSample, opt);
        tmpl.mean(:,:,param.mini_t_idx) = tmp_t.mean;
        tmpl.template(:,param.mini_t_idx) = tmp_t.template;
        tmpl.W(:,:,param.mini_t_idx) = tmp_t.W;
        tmpl.sigma(:,param.mini_t_idx) = tmp_t.sigma;                    
    else  %add a new subspace and remove the subspace with largest reconstruction error
        if num_tmpl < opt.num_tmpl % if the number of subspace is less than the setting
            tmpl.mean_pos(:,:,num_tmpl+1) = param.wimg;
            [negSample,position] = SelectNeg(frame, param.est, opt);
            tmp_t = PLS_sub(param.wimg, negSample, opt);
            tmpl.mean(:,:,num_tmpl+1) = tmp_t.mean;
            tmpl.template(:,num_tmpl+1) = tmp_t.template;
            tmpl.W(:,:,num_tmpl+1) = tmp_t.W;
            tmpl.sigma(:,num_tmpl+1) = tmp_t.sigma;
        else
            if param.max_t_idx ~= 1  % test_0521
                tmpl.mean_pos(:,:,param.max_t_idx) = param.wimg;
                [negSample,position] = SelectNeg(frame, param.est, opt);
                tmp_t = PLS_sub(param.wimg, negSample, opt);
                tmpl.mean(:,:,param.max_t_idx) = tmp_t.mean;
                tmpl.template(:,param.max_t_idx) = tmp_t.template;
                tmpl.W(:,:,param.max_t_idx) = tmp_t.W;
                tmpl.sigma(:,param.max_t_idx) = tmp_t.sigma;
            end
        end       
    end



%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%目标模板更新%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%     if (frameIndex1 == 0)    %从第20帧开始进行第一次坐标基的更新
%         %正样本的选择
%         posSampleTotal(:,:,opt.batchsize) = param.wimg;  %暂时正样本的个数和更新的频率保持一致；最初的这个目标样本一定要留着，所以注释掉这条代码
%         mean = mean*f + (1-f)*param.wimg;
%         %只使用当前的跟踪结果作为正样本
% %         posSample = warpimg(frame, param.est, opt.tmplsize);%正样本图像 
% %         posSample2(:,:,2) = posSample;
% 
%         %负样本在当前帧中选择
%         [negSample,position] = SelectNeg(frame, param.est, opt);
%         % 显示负样本
%         mri = zeros(opt.tmplsize(1),opt.tmplsize(2),1,opt.negNum);
%         mri(:,:,1,:)=negSample(:,:,:);
%         figure(3);
%         montage(mri);
%         %更新模型-使用近段时间的样本集合
% %         [tmpl, projNeg] = PLS_sub(posSampleTotal, negSample, opt.projNum);
%         %更新模型-使用当前时刻的正样本
% %         [tmpl, projNeg] = PLS_sub(posSample, negSample, opt.projNum);
%         %更新模型-使用mean
%         tmpl = PLS_sub(mean, negSample, opt);
%         
% %         X = cat(2, mean(:), reshape(negSample, size(negSample,1)*size(negSample,2), size(negSample,3)));
% % %         X = reshape(X, size(X,1)*size(X,2), size(X,3));
% %         % [u s d] = svd(X);
% %         X = X -  repmat(tmpl.mean(:),[1, size(X,2)]); %中心化，只不过与自己的相反而已
% %         %中心化完了，还要进一步的标准化
% %         X = X./repmat(tmpl.sigma(:),[1,size(X,2)]);
% %         % coefdiff = 0;
% %         X = tmpl.W'*X;
% %         model = train( L', sparse(X), '-s 0 -B 0 -c 100', 'col' );  %2/lamda = 10,lamda = 0.2
% %         
% %         % model = train( L', sparse(X), '-s 2 -B 0 -c 10', 'col' );  %2/lamda = 10,lamda = 0.2
% %         % [predicted_label, accuracy, aa] = predict(L', sparse(F), model, '-s 0 -B 0 -c 10', 'col');
% %         w = model.w(1:size(X,1));
% %         w = w';
% %         b = model.w(size(X,1)+1);
% %         
% %         %store the initial model
% %         % the classifier used in our algorithm; used as a fixed detector ;-)
% %         tmpl.w = w;
% %         tmpl.b = b;
%         
%     else
% %          posSampleTotal(:,:,frameIndex1) = param.wimg;
%         mean = mean*f + (1-f)*param.wimg;
%     end

   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%结果显示%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%     paramRes(frameIndex,:) = param.est; %存储跟踪结果，可以用来重新绘图。
    paramRes = [paramRes param.est];
    % draw result    
    figure(1);
    drawbox(opt.tmplsize, param.est, 'Color','r', 'LineWidth',2.5);
    imwrite(frame2im(getframe(gcf)),wname);
    %显示所有的采样样本
%     figure(1);
%     for i=1:opt.numsample
%         tmp = affparam2mat(param.param(:,i));
%         drawbox(tmpSZ,tmpSZ,tmp, 'Color','g', 'LineWidth',2.5);
%     end

    
%     figure(6);
%     imshow(param.wimg); 
    %打印出跟踪结果在pls子空间的投影前两维特征的情况（不管是保存了多少维，都可以显示前两维）
    
%     figure(5);
%     plot(param.proj(1),param.proj(2),'g.','LineWidth',2);
%     hold on;
%     plot(projNeg(:,1),projNeg(:,2),'b+','LineWidth',2);
%     hold off;


end
paraSaveresult = sprintf('%s%s.mat',prefix_result,'result'); 
save(paraSaveresult, 'paramRes','opt');





