wrapperDSKCF

PURPOSE ^

WRAPPERDSKCF.m is the wrapper function for the DS-KCF tracker [1]

SYNOPSIS ^

function [dsKCFoutputSr,dsKCFoutputSq,dsKCFsegmentationOut, avTime,totalTime,timeMatrix] =wrapperDSKCF(video_path, depth_path, img_files, depth_files, pos, target_sz,DSKCFparameters, show_visualization,save_Images,dest_path,noBitShift)

DESCRIPTION ^

 WRAPPERDSKCF.m is the wrapper function for the DS-KCF tracker [1]

   WRAPPERDSKCF is the wrapper function of the DS-KCF tracker [1]. This
   function, given the tracker parameters and the information about the
   data that will be processed, initializes the tracker's data structures
   and starts the frame by frame tracking processing. WRAPPERDSKCF
   eventually save/visualize the tracker image output (images with
   overlayed tracker bounding box) in the specified folder. Please note
   that  this function was partially built extending the KCF tracker code
   presented by Joao F. Henriques, in http://www.isr.uc.pt/~henriques/.

   INPUT:
  -video_path absolute path where color data is stored -depth_path
  absolute path where depth data is stored 
  -img_files the list of Color data files (N is the number of frame to be
  processed) -depth_files  the list of Depth data files (N is the number
  of frame to be processed)
  -pos initial DS-KCF tracker position pos=[y x] where x is the column
  index and y is the row index of the image
  -target_sz initial target size target_sz=[height,width]
  -DSKCFparameters structure containing DSKCF parameters, see the test
  script testDS-KCFScripts\runDSKCF.m
  -show_visualization flag to show the tracking results live in a matlab
  figure
  -save_Images save tracker's output as images with overlayed tracker
   bounding box) in the specified folder dest_path
  -dest_path absolute path of the destination folder where tracker's
  output is saved
  -noBitShift boolean value to properly load the depth data. In some cases
  the depth is stored in 16bit images where every pixel contains the depth
  data in mm. In this case noBitShift has to be set to false. On the
  contrary for other datasets (i.e. Princeton RGB-D) the data stored need
  to be shifted as shown in this function

   OUTPUT
  -dsKCFoutputSr tracker's output using scale factor Sr in [1]. This a Nx5
  vector containing in each row the tracker results for the corresponding
  frame. In particular the output is formatted as suggested in the
  princetonRGB-D dataset in [3]. For each row the first four columns
  contain the bounding box information in the format [topLeftX, topLeftY,
  bottomRightX, bottomRightY]. Note that in case of lost target the row
  contains NaN values. The fifth column contains a flag to indicate
  occlusions cases.

  -dsKCFoutputSq tracker's output using scale factor Sq in [1]. This a Nx5
  vector containing in each row the tracker results for the corresponding
  frame. In particular the output is formatted as suggested in the
  princetonRGB-D dataset in [3]. For each row the first four columns
  contain the bounding box information in the format [topLeftX, topLeftY,
  bottomRightX, bottomRightY]. Note that in case of lost target the row
  contains NaN values. The fifth column contains a flag to indicate
  occlusions cases.

  -dsKCFsegmentationOut tracker's output using scale factor shape module
  in [1]. This a Nx5 vector containing in each row the tracker results for
  the corresponding frame. In particular the output is formatted as
  suggested in the princetonRGB-D dataset in [3]. For each row the first
  four columns contain the bounding box information in the format
  [topLeftX, topLeftY, bottomRightX, bottomRightY]. Note that in case of
  lost target the row contains NaN values. The fifth column contains a
  flag to indicate occlusions cases.
   
  -avTime this is a Nx1 vector containing the processing time, expressed
  in ms,for each frame. Note that it does not condiser the reading and
  writing time from the disk

  -totalTime is the total time required to process the entire sequence
  
  -timeMatrix NxM matrix containing the processing rate for all the main
  modules of
   the ds-kcf (see [1] for more details)
 
  See also LOAD_VIDEO_INFO_BOBOTRESULTS,LOAD_VIDEO_INFO_DEPTHFROMMAT,
  INITDSKCFPARAM,INITDSKCFTRACKER,FROMCENTRALPOINTTOBB,INITDISTRIBUTIONFAST
  MANUALBBDRAW_OCC, MANUALBBDRAW_OCC_WITHLABELSVISUALIZE,
  INITDSKCFTRACKER_OCCLUDER,SINGLEFRAMEDSKCF


  [1] S. Hannuna, M. Camplani, J. Hall, M. Mirmehdi, D. Damen, T.
  Burghardt, A. Paiement, L. Tao, DS-KCF: A real-time tracker for RGB-D
  data, Journal of Real-Time Image Processing

  [2] J. F. Henriques, R. Caseiro, P. Martins, and J. Batista. High-speed
  tracking with kernelized correlation filters. Pattern Analysis and
  Machine Intelligence, IEEE Transactions on, 2015.

  [3] Shuran Song and Jianxiong Xiao. Tracking Revisited using RGBD
  Camera: Baseline and Benchmark. 2013.

  University of Bristol
  Massimo Camplani and Sion Hannuna

  massimo.camplani@bristol.ac.uk
  hannuna@compsci.bristol.ac.uk

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [dsKCFoutputSr,dsKCFoutputSq,dsKCFsegmentationOut, avTime,totalTime,timeMatrix] = ...
0002     wrapperDSKCF(video_path, depth_path, img_files, depth_files, pos, target_sz, ...
0003     DSKCFparameters, show_visualization,save_Images,dest_path,noBitShift)
0004 % WRAPPERDSKCF.m is the wrapper function for the DS-KCF tracker [1]
0005 %
0006 %   WRAPPERDSKCF is the wrapper function of the DS-KCF tracker [1]. This
0007 %   function, given the tracker parameters and the information about the
0008 %   data that will be processed, initializes the tracker's data structures
0009 %   and starts the frame by frame tracking processing. WRAPPERDSKCF
0010 %   eventually save/visualize the tracker image output (images with
0011 %   overlayed tracker bounding box) in the specified folder. Please note
0012 %   that  this function was partially built extending the KCF tracker code
0013 %   presented by Joao F. Henriques, in http://www.isr.uc.pt/~henriques/.
0014 %
0015 %   INPUT:
0016 %  -video_path absolute path where color data is stored -depth_path
0017 %  absolute path where depth data is stored
0018 %  -img_files the list of Color data files (N is the number of frame to be
0019 %  processed) -depth_files  the list of Depth data files (N is the number
0020 %  of frame to be processed)
0021 %  -pos initial DS-KCF tracker position pos=[y x] where x is the column
0022 %  index and y is the row index of the image
0023 %  -target_sz initial target size target_sz=[height,width]
0024 %  -DSKCFparameters structure containing DSKCF parameters, see the test
0025 %  script testDS-KCFScripts\runDSKCF.m
0026 %  -show_visualization flag to show the tracking results live in a matlab
0027 %  figure
0028 %  -save_Images save tracker's output as images with overlayed tracker
0029 %   bounding box) in the specified folder dest_path
0030 %  -dest_path absolute path of the destination folder where tracker's
0031 %  output is saved
0032 %  -noBitShift boolean value to properly load the depth data. In some cases
0033 %  the depth is stored in 16bit images where every pixel contains the depth
0034 %  data in mm. In this case noBitShift has to be set to false. On the
0035 %  contrary for other datasets (i.e. Princeton RGB-D) the data stored need
0036 %  to be shifted as shown in this function
0037 %
0038 %   OUTPUT
0039 %  -dsKCFoutputSr tracker's output using scale factor Sr in [1]. This a Nx5
0040 %  vector containing in each row the tracker results for the corresponding
0041 %  frame. In particular the output is formatted as suggested in the
0042 %  princetonRGB-D dataset in [3]. For each row the first four columns
0043 %  contain the bounding box information in the format [topLeftX, topLeftY,
0044 %  bottomRightX, bottomRightY]. Note that in case of lost target the row
0045 %  contains NaN values. The fifth column contains a flag to indicate
0046 %  occlusions cases.
0047 %
0048 %  -dsKCFoutputSq tracker's output using scale factor Sq in [1]. This a Nx5
0049 %  vector containing in each row the tracker results for the corresponding
0050 %  frame. In particular the output is formatted as suggested in the
0051 %  princetonRGB-D dataset in [3]. For each row the first four columns
0052 %  contain the bounding box information in the format [topLeftX, topLeftY,
0053 %  bottomRightX, bottomRightY]. Note that in case of lost target the row
0054 %  contains NaN values. The fifth column contains a flag to indicate
0055 %  occlusions cases.
0056 %
0057 %  -dsKCFsegmentationOut tracker's output using scale factor shape module
0058 %  in [1]. This a Nx5 vector containing in each row the tracker results for
0059 %  the corresponding frame. In particular the output is formatted as
0060 %  suggested in the princetonRGB-D dataset in [3]. For each row the first
0061 %  four columns contain the bounding box information in the format
0062 %  [topLeftX, topLeftY, bottomRightX, bottomRightY]. Note that in case of
0063 %  lost target the row contains NaN values. The fifth column contains a
0064 %  flag to indicate occlusions cases.
0065 %
0066 %  -avTime this is a Nx1 vector containing the processing time, expressed
0067 %  in ms,for each frame. Note that it does not condiser the reading and
0068 %  writing time from the disk
0069 %
0070 %  -totalTime is the total time required to process the entire sequence
0071 %
0072 %  -timeMatrix NxM matrix containing the processing rate for all the main
0073 %  modules of
0074 %   the ds-kcf (see [1] for more details)
0075 %
0076 %  See also LOAD_VIDEO_INFO_BOBOTRESULTS,LOAD_VIDEO_INFO_DEPTHFROMMAT,
0077 %  INITDSKCFPARAM,INITDSKCFTRACKER,FROMCENTRALPOINTTOBB,INITDISTRIBUTIONFAST
0078 %  MANUALBBDRAW_OCC, MANUALBBDRAW_OCC_WITHLABELSVISUALIZE,
0079 %  INITDSKCFTRACKER_OCCLUDER,SINGLEFRAMEDSKCF
0080 %
0081 %
0082 %  [1] S. Hannuna, M. Camplani, J. Hall, M. Mirmehdi, D. Damen, T.
0083 %  Burghardt, A. Paiement, L. Tao, DS-KCF: A real-time tracker for RGB-D
0084 %  data, Journal of Real-Time Image Processing
0085 %
0086 %  [2] J. F. Henriques, R. Caseiro, P. Martins, and J. Batista. High-speed
0087 %  tracking with kernelized correlation filters. Pattern Analysis and
0088 %  Machine Intelligence, IEEE Transactions on, 2015.
0089 %
0090 %  [3] Shuran Song and Jianxiong Xiao. Tracking Revisited using RGBD
0091 %  Camera: Baseline and Benchmark. 2013.
0092 %
0093 %  University of Bristol
0094 %  Massimo Camplani and Sion Hannuna
0095 %
0096 %  massimo.camplani@bristol.ac.uk
0097 %  hannuna@compsci.bristol.ac.uk
0098 
0099 
0100 %As suggested in the original code of KCF resize target and image....
0101 resize_image = (sqrt(prod(target_sz)) >= 100);  %diagonal size >= threshold
0102 if resize_image,
0103     pos = floor(pos / 2);
0104     target_sz = floor(target_sz / 2);
0105 end
0106 
0107 
0108 %window size, taking padding into account
0109 DSKCFparameters.window_sz = floor(target_sz * (1 + DSKCFparameters.padding));
0110 
0111 %initialize the scale parameters for the DSKCF algorithm accortind to the
0112 %selected Sq (see [1]) information contained in DSKCFparameters
0113 scaleDSKCF_struct=initDSKCFparam(DSKCFparameters,target_sz,pos);
0114 
0115 %initialize shape struct
0116 shapeDSKCF_struct=initDSKCFshape(5,0);
0117 
0118 %check if the scale is properly initialized....
0119 if(isempty(scaleDSKCF_struct))
0120     disp('Scale structure initialization failed, tracking aborted');
0121     dsKCFoutputSr=[];
0122     dsKCFoutputSq=[];
0123     dsKCFsegmentationOut=[];
0124     avTime=[];
0125     return;
0126 end
0127 
0128 %note: variables ending with 'f' are in the Fourier domain.
0129 
0130 totalTime = 0;  %to calculate FPS
0131 
0132 %init variables
0133 positions = zeros(numel(img_files), 2); % where store tracker centroids
0134 dsKCFoutputSr = zeros(numel(img_files), 2);
0135 dsKCFoutputSq = zeros(numel(img_files), 2);
0136 dsKCFsegmentationOut = zeros(numel(img_files), 2);
0137 
0138 %auxiliary variables to compute the final results of the tracker, in fact
0139 %DS-KCF tracking core returns only the centroid of the target, we need also
0140 %to store the size of it to return proper output parameters
0141 sizeSr = zeros(numel(img_files), 2);
0142 sizeSq = zeros(numel(img_files), 2);
0143 
0144 frameCurr=[]; %contains depth and color data of the current frame depth16Bit
0145 %contains the 16bits depth information in mm. depth contains
0146 %the normalize depth data as a grayscale image coded with 8bits
0147 %depthNoData is the mask to identify missing depth data
0148 %rgb is the color image and gray is the grayscale version of it
0149 
0150 framePrev=[]; %contains the same information of frameCurr but related
0151 %to the previous frame
0152 
0153 
0154 occlusionState=[];% vector containing flags about the detected occlusion state
0155 %%%% TO BE CHECKED
0156 nanPosition=[];
0157 
0158 avTime=[];
0159 
0160 numberTimeIntervals=8;%7 interval plus 1 total
0161 timeMatrix=repmat(-1,numel(img_files),numberTimeIntervals);%no es
0162 
0163 segmentedBB=[];
0164 segmentedSize=[];
0165 
0166 %%FRAME BY FRAME TRACKING.....
0167 for frame = 1:numel(img_files),
0168     %load images
0169     im = imread([video_path img_files{frame}]);
0170     depth = imread([depth_path depth_files{frame}]);
0171     
0172     %% inserting type control for depth image
0173     if(isa(depth,'uint16'))
0174         if(noBitShift==false)
0175             depth = bitor(bitshift(depth,-3), bitshift(depth,16-3));
0176         end
0177         %depth data in mm
0178         depth16Bit = depth;
0179         
0180         %Normalize depth data as a grayscale image [0 255]
0181         depth = double(depth);
0182         depth(depth==0) = 10000;
0183         depth = (depth-500)/8500;%only use the data from 0.5-8m
0184         depth(depth<0) = 0;
0185         depth(depth>1) = 1;
0186         depth = uint8(255*(1 - depth));
0187     end
0188     
0189     %resize images
0190     if size(im,3) > 1,
0191         imRGB=im;
0192         im = rgb2gray(im);
0193     else
0194         imRGB=im;
0195         imRGB(:,:,2)=im;
0196         imRGB(:,:,3)=im;
0197     end
0198     
0199     if resize_image,
0200         im = imresize(im, 0.5);
0201         imRGB = imresize(imRGB, 0.5);
0202         depth = imresize(depth, 0.5);
0203         depth16Bit = depth16Bit((1:2:end),(1:2:end));
0204     end
0205     
0206     
0207     
0208     %start measuring the time!!!!
0209     tTotal=tic();
0210     firstFrame=frame==1;
0211     
0212     %Insert current frame data
0213     frameCurr.rgb   = imRGB;
0214     frameCurr.gray   = im;
0215     frameCurr.depth = double(depth);
0216     frameCurr.depthNoData=depth16Bit==0;
0217     frameCurr.depth16Bit=depth16Bit;
0218     
0219     %for the first frame initialize the structures
0220     if(firstFrame)
0221         segmentedMASK=repmat(0,size(frameCurr.depth));
0222         trackerDSKCF_struct=initDSKCFtracker();
0223         %check if the scale is properly initialized....
0224         if(isempty(trackerDSKCF_struct))
0225             disp('DS-KCF tracker structure initialization failed, tracking aborted');
0226             dsKCFoutputSr=[];
0227             dsKCFoutputSq=[];
0228             avTime=[];
0229             return;
0230         end
0231         %%INITIALIZE HISTOGRAMS....
0232         framePrev.rgb   = imRGB;
0233         framePrev.gray   = im;
0234         framePrev.depth = depth;
0235         framePrev.depthNoData=depth16Bit==0;
0236         framePrev.depth16Bit=depth16Bit;
0237         
0238         trackerDSKCF_struct.previousTarget.posX=pos(2);
0239         trackerDSKCF_struct.previousTarget.posY=pos(1);
0240         trackerDSKCF_struct.previousTarget.h=scaleDSKCF_struct.target_sz(scaleDSKCF_struct.i).target_sz(1);
0241         trackerDSKCF_struct.previousTarget.w=scaleDSKCF_struct.target_sz(scaleDSKCF_struct.i).target_sz(2);
0242         trackerDSKCF_struct.previousTarget.bb=fromCentralPointToBB...
0243             (trackerDSKCF_struct.previousTarget.posX,trackerDSKCF_struct.previousTarget.posY,...
0244             trackerDSKCF_struct.previousTarget.w,trackerDSKCF_struct.previousTarget.h,size(im,2),size(im,1));
0245         trackerDSKCF_struct.currentTarget.meanDepthObj=0;% mean depth of the tracker object
0246         %initialize depth distributions
0247         [trackerDSKCF_struct.previousTarget.meanDepthObj,trackerDSKCF_struct.previousTarget.stdDepthObj,...
0248             trackerDSKCF_struct.previousTarget.LabelRegions,...
0249             trackerDSKCF_struct.previousTarget.regionIndex,...
0250             trackerDSKCF_struct.previousTarget.Centers,...
0251             trackerDSKCF_struct.previousTarget.LUT] = ...
0252             initDistributionFast(trackerDSKCF_struct.previousTarget.bb, ...
0253             framePrev.depth16Bit,framePrev.depthNoData);
0254         
0255         %for the first frame copy everything also in the current target
0256         trackerDSKCF_struct.currentTarget=trackerDSKCF_struct.previousTarget;
0257         
0258         %set the depth of the initial target in the scale data structure
0259         scaleDSKCF_struct.InitialDepth = trackerDSKCF_struct.previousTarget.meanDepthObj;
0260         scaleDSKCF_struct.currDepth = trackerDSKCF_struct.previousTarget.meanDepthObj;
0261         
0262         %initialize structures for the occluder object
0263         trackerDSKCF_structOccluder=initDSKCFtracker_occluder();
0264         DSKCFparameters_Occluder=DSKCFparameters;%these need to be resetted eventually in some parts
0265         
0266         %figure initialization
0267         if(show_visualization)
0268             
0269             myFigColor=figure();
0270             myFigDepth=figure();
0271             set(myFigDepth,'resize','off');
0272             set(myFigColor,'resize','off');
0273         end
0274         
0275         %take segmentation results for the first frame
0276         trackerDSKCF_struct.currentTarget.segmentedBB=...
0277             trackerDSKCF_struct.currentTarget.bb';
0278     end
0279 
0280     %DS-KCF tracker code need as input the position expressed as [y x],
0281     %remember this particular while reading the code!!!!!
0282 
0283     [pos,trackerDSKCF_struct,trackerDSKCF_structOccluder,scaleDSKCF_struct,...
0284         DSKCFparameters_Occluder,segmentedMASK,shapeDSKCF_struct,timeMatrix(frame,1:7)]=...
0285         singleFrameDSKCF(firstFrame,pos,frameCurr,trackerDSKCF_struct,DSKCFparameters,...
0286         scaleDSKCF_struct,trackerDSKCF_structOccluder,DSKCFparameters_Occluder,shapeDSKCF_struct);
0287     
0288     if(isempty(trackerDSKCF_struct.currentTarget.segmentedBB))
0289         segmentedBB=[segmentedBB;segmentedBB(end,:)];
0290     else
0291         segmentedBB=[segmentedBB;trackerDSKCF_struct.currentTarget.segmentedBB];
0292     end
0293     %Compose the Occlusion state vector for results
0294     occlusionState=[occlusionState ;trackerDSKCF_struct.currentTarget.underOcclusion];
0295     
0296     %tracking finished,
0297     avTime=[avTime; toc(tTotal)];
0298     timeMatrix(frame,8)=avTime(end);
0299     totalTime = totalTime + avTime(end);
0300     
0301     %% Just visualize......
0302     if (save_Images==false && show_visualization==true)
0303         
0304         %eventually re-scale the images
0305         if(resize_image)
0306             imRGB = imresize(imRGB, 2);
0307             depth = imresize(depth, 2);
0308         end
0309         
0310         %empty tracking, so mark this frame
0311         if(isempty(pos))
0312             bbToPlot=[];
0313             nanPosition=[nanPosition; 1];
0314         else
0315             nanPosition=[nanPosition; 0];
0316             
0317             %use the Sr scale factor (see [1] for more details)
0318             sr = scaleDSKCF_struct.InitialDepth / scaleDSKCF_struct.currDepth;
0319             
0320             targ_sz = round(scaleDSKCF_struct.InitialTargetSize * sr);
0321             
0322             %calculate the corresponding bounding box for Plotting!!!!
0323             %in this case we need [topLeftX, topLeftY,W,H]
0324             bbToPlot = [pos([2,1]) - targ_sz([2,1])/2, targ_sz([2,1])];
0325             if(resize_image)
0326                 bbToPlot=bbToPlot*2;
0327             end
0328         end
0329         
0330         bbOCCToPlot=[];
0331         if(trackerDSKCF_struct.currentTarget.underOcclusion)
0332             widthOCC=trackerDSKCF_struct.currentTarget.occBB(3)-trackerDSKCF_struct.currentTarget.occBB(1);
0333             heightOCC=trackerDSKCF_struct.currentTarget.occBB(4)-trackerDSKCF_struct.currentTarget.occBB(2);
0334             bbOCCToPlot=[trackerDSKCF_struct.currentTarget.occBB(1:2); widthOCC; heightOCC]';
0335             if(resize_image)
0336                 bbOCCToPlot=bbOCCToPlot*2;
0337             end
0338         end
0339         
0340         if(frame==1)
0341             manualBBdraw_OCC_WithLabelsVisualize(imRGB,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigColor);
0342             positionColor=get(gcf,'OuterPosition');
0343             positionColor(1)=positionColor(1)-floor(positionColor(3)/2) -25;
0344             set(gcf,'OuterPosition',positionColor);
0345             manualBBdraw_OCC_WithLabelsVisualize(depth,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigDepth);
0346             positionDepth=get(gcf,'OuterPosition');
0347             positionDepth(1)=positionDepth(1)+floor(positionDepth(3)/2) +25;
0348             set(gcf,'OuterPosition',positionDepth);
0349         else
0350             %myFigColor=figure();
0351             %myFigDepth=figure();
0352             clf(myFigColor);
0353             manualBBdraw_OCC_WithLabelsVisualize(imRGB,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigColor);
0354             
0355             clf(myFigDepth);
0356             manualBBdraw_OCC_WithLabelsVisualize(depth,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigDepth);
0357             drawnow
0358             pause(0.05)
0359         end
0360         
0361     end
0362     %% Visualize and save.....
0363     if (save_Images==true && show_visualization==true)
0364         
0365         %eventually re-scale the images
0366         if(resize_image)
0367             imRGB = imresize(imRGB, 2);
0368             depth = imresize(depth, 2);
0369         end
0370         
0371         %empty tracking, so mark this frame
0372         if(isempty(pos))
0373             bbToPlot=[];
0374             nanPosition=[nanPosition; 1];
0375         else
0376             nanPosition=[nanPosition; 0];
0377             
0378             %use the Sr scale factor (see [1] for more details)
0379             sr = scaleDSKCF_struct.InitialDepth / scaleDSKCF_struct.currDepth;
0380             
0381             targ_sz = round(scaleDSKCF_struct.InitialTargetSize * sr);
0382             
0383             %calculate the corresponding bounding box for Plotting!!!!
0384             %in this case we need [topLeftX, topLeftY,W,H]
0385             bbToPlot = [pos([2,1]) - targ_sz([2,1])/2, targ_sz([2,1])];
0386             if(resize_image)
0387                 bbToPlot=bbToPlot*2;
0388             end
0389         end
0390         
0391         bbOCCToPlot=[];
0392         if(trackerDSKCF_struct.currentTarget.underOcclusion)
0393             widthOCC=trackerDSKCF_struct.currentTarget.occBB(3)-trackerDSKCF_struct.currentTarget.occBB(1);
0394             heightOCC=trackerDSKCF_struct.currentTarget.occBB(4)-trackerDSKCF_struct.currentTarget.occBB(2);
0395             bbOCCToPlot=[trackerDSKCF_struct.currentTarget.occBB(1:2); widthOCC; heightOCC]';
0396             if(resize_image)
0397                 bbOCCToPlot=bbOCCToPlot*2;
0398             end
0399         end
0400         
0401         if(frame==1)
0402             imRGB_tracked=manualBBdraw_OCC_WithLabelsVisualize(imRGB,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigColor);
0403             positionColor=get(gcf,'OuterPosition');
0404             positionColor(1)=positionColor(1)-floor(positionColor(3)/2) -25;
0405             set(gcf,'OuterPosition',positionColor);
0406             dept_tracked=manualBBdraw_OCC_WithLabelsVisualize(depth,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigDepth);
0407             positionDepth=get(gcf,'OuterPosition');
0408             positionDepth(1)=positionDepth(1)+floor(positionDepth(3)/2) +25;
0409             set(gcf,'OuterPosition',positionDepth);
0410         else
0411             %myFigColor=figure();
0412             %myFigDepth=figure();
0413             clf(myFigColor);
0414             imRGB_tracked=manualBBdraw_OCC_WithLabelsVisualize(imRGB,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigColor);
0415             
0416             clf(myFigDepth);
0417             dept_tracked=manualBBdraw_OCC_WithLabelsVisualize(depth,bbToPlot,bbOCCToPlot,'r','y',4,'DS-KCF','Occluder',myFigDepth);
0418             drawnow
0419             pause(0.05)
0420         end
0421         
0422         tmpColorName=[dest_path '/trackedColor_' num2str(frame) '.jpg'];
0423         if(frame==1)
0424             imRGB_trackedMASK=imRGB_tracked(:,:,1)==204;
0425             imRGB_trackedMASK=imRGB_trackedMASK & imRGB_tracked(:,:,2)==204;
0426             imRGB_trackedMASK=imRGB_trackedMASK & imRGB_tracked(:,:,3)==204;
0427             finalMask=imfill(~imRGB_trackedMASK,'holes');
0428             rp=regionprops(finalMask,'boundingbox','Area');
0429             
0430             areaList= cat(1, rp.Area);
0431             bbList= cat(1, rp.BoundingBox);
0432             [ddd,areaMax]=max(areaList);
0433             rect=floor(bbList(areaMax,:));
0434             
0435         end
0436         %clean the image from gray fig border....and save
0437         imRGB_trackedNEW=imRGB_tracked(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:);
0438         imwrite(imRGB_trackedNEW,tmpColorName);
0439         
0440         %clean the image from gray fig border.... and save
0441         tmpDepthName=[dest_path '/trackedDepth_' num2str(frame) '.jpg'];
0442         dept_trackedNEW=dept_tracked(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:);
0443         imwrite(dept_trackedNEW,tmpDepthName);
0444     end
0445     %% just save images
0446     if (save_Images==true && show_visualization==false)
0447         
0448         %eventually re-scale the images
0449         if(resize_image)
0450             imRGB = imresize(imRGB, 2);
0451             depth = imresize(depth, 2);
0452             segmentedMASK= imresize(segmentedMASK,2);
0453         end
0454         
0455         tmpMaskName=[dest_path '/binaryMask_' num2str(frame) '.jpg'];
0456         imwrite(segmentedMASK>0,tmpMaskName);
0457         
0458         tmpMaskName=[dest_path '/binaryMaskColored_' num2str(frame) '.jpg'];
0459         segmentedMASKColored = imRGB.*(uint8(repmat(segmentedMASK>0,[1,1,3])));
0460         imwrite(segmentedMASKColored,tmpMaskName);
0461         
0462         bbSegToPlot=[1 1 1 1];
0463         if(isempty(trackerDSKCF_struct.currentTarget.segmentedBB)==false)
0464             widthOCC=trackerDSKCF_struct.currentTarget.segmentedBB(3)-trackerDSKCF_struct.currentTarget.segmentedBB(1);
0465             heightOCC=trackerDSKCF_struct.currentTarget.segmentedBB(4)-trackerDSKCF_struct.currentTarget.segmentedBB(2);
0466             bbSegToPlot=[trackerDSKCF_struct.currentTarget.segmentedBB(1:2)'; widthOCC; heightOCC]';
0467             if(resize_image)
0468                 bbSegToPlot=bbSegToPlot*2;
0469             end
0470         end
0471         
0472         
0473         %empty tracking, so mark this frame
0474         if(isempty(pos))
0475             bbToPlot=[1 1 1 1];
0476             nanPosition=[nanPosition; 1];
0477         else
0478             nanPosition=[nanPosition; 0];
0479             
0480             %use the Sr scale factor (see [1] for more details)
0481             sr = scaleDSKCF_struct.InitialDepth / scaleDSKCF_struct.currDepth;
0482             
0483             targ_sz = round(scaleDSKCF_struct.InitialTargetSize * sr);
0484             
0485             %calculate the corresponding bounding box for Plotting!!!!
0486             %in this case we need [topLeftX, topLeftY,W,H]
0487             bbToPlot = [pos([2,1]) - targ_sz([2,1])/2, targ_sz([2,1])];
0488             %bbToPlot=[segmentedBB(end,[1 2]),(segmentedBB(end,[3 4])-segmentedBB(end,[1 2]))];
0489             if(resize_image)
0490                 bbToPlot=bbToPlot*2;
0491             end
0492         end
0493         
0494         bbOCCToPlot=[1 1 1 1];
0495         if(trackerDSKCF_struct.currentTarget.underOcclusion)
0496             widthOCC=trackerDSKCF_struct.currentTarget.occBB(3)-trackerDSKCF_struct.currentTarget.occBB(1);
0497             heightOCC=trackerDSKCF_struct.currentTarget.occBB(4)-trackerDSKCF_struct.currentTarget.occBB(2);
0498             bbOCCToPlot=[trackerDSKCF_struct.currentTarget.occBB(1:2); widthOCC; heightOCC]';
0499             if(resize_image)
0500                 bbOCCToPlot=bbOCCToPlot*2;
0501             end
0502         end
0503         
0504         bbSeparate(1,:)=bbToPlot;
0505         bbSeparate(2,:)=bbOCCToPlot;
0506         bbSeparate(3,:)=bbSegToPlot;
0507         
0508         colorVector='ryb';
0509         myText{1}='DS-KCF';
0510         myText{2}='Occluder';
0511         myText{3}='DS-KCF+SEG';
0512         myText={};
0513         myTextColor='ryb';
0514         
0515         %draw the bb square....
0516         imRGB_tracked=manualBBdraw_MULTIPLE(imRGB,bbSeparate,colorVector,4,myText,myTextColor);
0517         dept_tracked=manualBBdraw_MULTIPLE(depth,bbSeparate,colorVector,4,myText,myTextColor);
0518 
0519         %end
0520         tmpColorName=[dest_path '/trackedColor_' num2str(frame) '.jpg'];
0521         if(frame==1)
0522             imRGB_trackedMASK=imRGB_tracked(:,:,1)==204;
0523             imRGB_trackedMASK=imRGB_trackedMASK & imRGB_tracked(:,:,2)==204;
0524             imRGB_trackedMASK=imRGB_trackedMASK & imRGB_tracked(:,:,3)==204;
0525             finalMask=imfill(~imRGB_trackedMASK,'holes');
0526             rp=regionprops(finalMask,'boundingbox','Area');
0527             
0528             areaList= cat(1, rp.Area);
0529             bbList= cat(1, rp.BoundingBox);
0530             [ddd,areaMax]=max(areaList);
0531             rect=floor(bbList(areaMax,:));
0532             
0533         end
0534         %clean the image from gray fig border....and save
0535         imRGB_trackedNEW=imRGB_tracked(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:);
0536         imwrite(imRGB_trackedNEW,tmpColorName);
0537         
0538         %clean the image from gray fig border.... and save
0539         tmpDepthName=[dest_path '/trackedDepth_' num2str(frame) '.jpg'];
0540         dept_trackedNEW=dept_tracked(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:);
0541         imwrite(dept_trackedNEW,tmpDepthName);
0542     end
0543     
0544     %now generate the results, starting from the tracker output!!!
0545     % the object has being tracked....
0546     if(isempty(pos)==false)
0547         %accumulate the position of the DS-KCF tracker remember format [y x]
0548         positions(frame,:) = pos;
0549         %take the sq size (see [1]) invert the coordinate as you need to
0550         %combine this with positions
0551         sqVector(frame,:)= scaleDSKCF_struct.target_sz(scaleDSKCF_struct.i).target_sz([2,1]);
0552         
0553         %use the Sr scale factor (see [1] for more details)
0554         sr = scaleDSKCF_struct.InitialDepth / scaleDSKCF_struct.currDepth;
0555         targ_sz = round(scaleDSKCF_struct.InitialTargetSize * sr);
0556         %invert the coordinate as you need to combine this with positions
0557         srVector(frame,:) = targ_sz([2,1]);
0558         
0559     else
0560         pos=positions(frame-1,:);
0561         positions(frame,:) = pos;
0562         
0563         tmpSize=sqVector(frame-1,:);
0564         sqVector(frame,:)= tmpSize;
0565         
0566         tmpSize=srVector(frame-1,:);
0567         srVector(frame,:) = tmpSize;
0568         
0569     end
0570     
0571     %Update PAST Target data structure
0572     if(frame>1)
0573         %previous target entries
0574         trackerDSKCF_struct.previousTarget=trackerDSKCF_struct.currentTarget;
0575     end
0576     
0577     
0578 end
0579 
0580 if resize_image,
0581     positions = positions * 2;
0582     sqVector= sqVector*2;
0583     srVector= srVector*2;
0584     segmentedBB=2*segmentedBB;
0585 end
0586 
0587 %now generate the final results, this are in the format requested by the
0588 %Princeton RGB-D dataset in the format [topLeftX, topLeftY, bottomRightX,
0589 %bottomRightY]. Then move from the change from the center + target size
0590 %format to the above mentioned one
0591 
0592 dsKCFoutputSr=[positions(:,[2,1]) - srVector/2, positions(:,[2,1]) + srVector/2];
0593 dsKCFoutputSq=[positions(:,[2,1]) - sqVector/2, positions(:,[2,1]) + sqVector/2];
0594 dsKCFsegmentationOut=segmentedBB;
0595 
0596 %now set to NaN the output for the frames where the tracker was not
0597 %available
0598 dsKCFoutputSr(nanPosition>0,:)=NaN;
0599 dsKCFoutputSq(nanPosition>0,:)=NaN;
0600 dsKCFsegmentationOut(nanPosition>0,:)=NaN;
0601 
0602 %add the occlusion state vector.
0603 dsKCFoutputSr=[dsKCFoutputSr, occlusionState];
0604 dsKCFoutputSq=[dsKCFoutputSq, occlusionState];
0605 dsKCFsegmentationOut=[dsKCFsegmentationOut, occlusionState];
0606 end
0607

Generated on Thu 24-Nov-2016 18:03:21 by m2html © 2005