SPLINEFIT EXAMPLES
Contents
EXAMPLE 1: Breaks and pieces
+x = 2*pi*rand(1,200);
+y = sin(x) + sin(2*x) + 0.2*randn(size(x));
+
+
+breaks = linspace(0,2*pi,41);
+pp1 = splinefit(x,y,breaks);
+
+
+pp2 = splinefit(x,y,10);
+
+
+figure(1)
+xx = linspace(0,2*pi,400);
+y1 = ppval(pp1,xx);
+y2 = ppval(pp2,xx);
+plot(x,y,'.',xx,[y1;y2])
+axis([0,2*pi,-2.5,2.5]), grid on
+legend('data','41 breaks, 40 pieces','11 breaks, 10 pieces')
+title('EXAMPLE 1: Breaks and pieces')
+
EXAMPLE 2: Spline orders
+x = 2*pi*rand(1,200);
+y = sin(x) + sin(2*x) + 0.1*randn(size(x));
+
+
+pp1 = splinefit(x,y,8,1);
+pp2 = splinefit(x,y,8,2);
+pp3 = splinefit(x,y,8,3);
+pp4 = splinefit(x,y,8,4);
+pp5 = splinefit(x,y,8,5);
+
+
+figure(2)
+xx = linspace(0,2*pi,400);
+y1 = ppval(pp1,xx);
+y2 = ppval(pp2,xx);
+y3 = ppval(pp3,xx);
+y4 = ppval(pp4,xx);
+y5 = ppval(pp5,xx);
+plot(x,y,'.',xx,[y1;y2;y3;y4;y5]), grid on
+legend('data','order 1','order 2','order 3','order 4','order 5')
+title('EXAMPLE 2: Spline orders')
+
EXAMPLE 3: Periodic boundary conditions
+x = 2*pi*[0,rand(1,98),1];
+y = sin(x) - cos(2*x) + 0.2*randn(size(x));
+
+
+pp1 = splinefit(x,y,10,5);
+
+pp2 = splinefit(x,y,10,5,'p');
+
+
+figure(3)
+xx = linspace(0,2*pi,400);
+y1 = ppval(pp1,xx);
+y2 = ppval(pp2,xx);
+plot(x,y,'.',xx,[y1;y2]), grid on
+legend('data','no constraints','periodic')
+title('EXAMPLE 3: Periodic boundary conditions')
+
+
+y0 = ppval(pp2,[0,2*pi]);
+y1 = ppval(ppdiff(pp2,1),[0,2*pi]);
+y2 = ppval(ppdiff(pp2,2),[0,2*pi]);
+y3 = ppval(ppdiff(pp2,3),[0,2*pi]);
+disp('Endpoint derivatives:')
+disp([y0;y1;y2;y3])
+Endpoint derivatives:
+ -0.9885 -0.9885
+ 0.7848 0.7848
+ 3.2802 3.2802
+ 5.6253 5.6253
+
+
EXAMPLE 4: Endpoint conditions
+x = 2*pi*rand(1,200);
+y = sin(2*x) + 0.1*randn(size(x));
+
+
+breaks = linspace(0,2*pi,10);
+
+
+xc = [0,0,2*pi,2*pi];
+cc = [eye(2),eye(2)];
+con = struct('xc',xc,'cc',cc);
+pp1 = splinefit(x,y,breaks,con);
+
+
+con = struct('xc',0);
+pp2 = splinefit(x,y,breaks,con,'p');
+
+
+figure(4)
+xx = linspace(0,2*pi,400);
+y1 = ppval(pp1,xx);
+y2 = ppval(pp2,xx);
+plot(x,y,'.',xx,[y1;y2]), grid on
+legend('data','clamped','hinged periodic')
+title('EXAMPLE 4: Endpoint conditions')
+
EXAMPLE 5: Airfoil data
+x = [0,1,2,4,8,16,24,40,56,72,80]/80;
+y = [0,28,39,53,70,86,90,79,55,22,2]/1000;
+xy = [x;y];
+
+
+ds = sqrt(diff(x).^2 + diff(y).^2);
+s = [0, cumsum(ds)];
+
+
+con = struct('xc',[0 0],'yc',[0 0; 0 1],'cc',eye(2));
+
+
+pp = splinefit(s,xy,4,con);
+
+
+figure(5)
+ss = linspace(0,s(end),400);
+xyfit = ppval(pp,ss);
+xyb = ppval(pp,pp.breaks);
+plot(x,y,'.',xyfit(1,:),xyfit(2,:),'r',xyb(1,:),xyb(2,:),'ro')
+legend('data','spline','breaks')
+grid on, axis equal
+title('EXAMPLE 5: Airfoil data')
+
EXAMPLE 6: Robust fitting
+x = linspace(0,2*pi,200);
+y = sin(x) + sin(2*x) + 0.05*randn(size(x));
+
+
+x = [x, linspace(0,2*pi,60)];
+y = [y, -ones(1,60)];
+
+
+con = struct('xc',[0,2*pi]);
+pp1 = splinefit(x,y,8,con,0.25);
+pp2 = splinefit(x,y,8,con,0.75);
+pp3 = splinefit(x,y,8,con);
+
+
+figure(6)
+xx = linspace(0,2*pi,400);
+y1 = ppval(pp1,xx);
+y2 = ppval(pp2,xx);
+y3 = ppval(pp3,xx);
+plot(x,y,'.',xx,[y1;y2;y3]), grid on
+legend('data with outliers','robust, beta = 0.25','robust, beta = 0.75',...
+ 'no robust fitting')
+title('EXAMPLE 6: Robust fitting')
+
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples.png
new file mode 100644
index 0000000000000000000000000000000000000000..3a8bb5070926c40f52631f8e3a0e526b98377acf
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:8c9a67ecc0caa48403485e7d708737deb1bf1acfe2765959784916d2f672d2ad
+size 4004
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_01.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_01.png
new file mode 100644
index 0000000000000000000000000000000000000000..9e9db855de2aa8b76348a14f3ca3e356ba939bcd
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_01.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:c1b3a9dca6c4e580d642bc434ac8c18b10a7b63937116df6a82895c9b4371d09
+size 11655
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_02.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_02.png
new file mode 100644
index 0000000000000000000000000000000000000000..a143df3ab6c13ed2cde5730122c205812463066d
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_02.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:344dd0b4956c3dc0da57c2a92b354e85766343ce49b62e298d9b2f615405e032
+size 12602
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_03.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_03.png
new file mode 100644
index 0000000000000000000000000000000000000000..dc25cd3e30f4ed9a2d209efac924d68901cea86a
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_03.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:4086d1625fcef96884ad09c293b365a69bbd3924e8a1bdf5134d0717be2902e0
+size 9926
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_04.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_04.png
new file mode 100644
index 0000000000000000000000000000000000000000..6156466c694455e84c2e36dbe2bb7f66e9bf9bbb
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_04.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:e43c3a2a1ac2eae55d6f6e1d503739f750cce46cfdcdc9aaeaea8f132cdece27
+size 11077
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_05.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_05.png
new file mode 100644
index 0000000000000000000000000000000000000000..9b829bf838d1ff09f0858cee4933490d8d5737db
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_05.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:c83d997a51bcbc4519e21baf90456e540a69fa1c089d4b1ddfe3aeb9adf86952
+size 7194
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_06.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_06.png
new file mode 100644
index 0000000000000000000000000000000000000000..02469bc5f7fc27a5ce9c926b5b11ce436a596671
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/html/examples_06.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:c14e0db99e65a16d61ffbe4e3f595fd88a953ffe7f6ee44ae8c724089acfe89b
+size 12048
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/license.txt b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/license.txt
new file mode 100644
index 0000000000000000000000000000000000000000..1be63976f4e8261887b25451f09ac4702f1445f4
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/license.txt
@@ -0,0 +1,24 @@
+Copyright (c) 2010, Jonas Lundgren
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the distribution
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppdiff.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppdiff.m
new file mode 100644
index 0000000000000000000000000000000000000000..8a338f5fb62a1af993b5bab25cb3418fbb4f5f17
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppdiff.m
@@ -0,0 +1,52 @@
+function qq = ppdiff(pp,j)
+%PPDIFF Differentiate piecewise polynomial.
+% QQ = PPDIFF(PP,J) returns the J:th derivative of a piecewise
+% polynomial PP. PP must be on the form evaluated by PPVAL. QQ is a
+% piecewise polynomial on the same form. Default value for J is 1.
+%
+% Example:
+% x = linspace(-pi,pi,9);
+% y = sin(x);
+% pp = spline(x,y);
+% qq = ppdiff(pp);
+% xx = linspace(-pi,pi,201);
+% plot(xx,cos(xx),'b',xx,ppval(qq,xx),'r')
+%
+% See also PPVAL, SPLINE, SPLINEFIT, PPINT
+
+% Author: jonas.lundgren@saabgroup.com, 2009.
+
+if nargin < 1, help ppdiff, return, end
+if nargin < 2, j = 1; end
+
+% Check diff order
+if ~isreal(j) || mod(j,1) || j < 0
+ msgid = 'PPDIFF:DiffOrder';
+ message = 'Order of derivative must be a non-negative integer!';
+ error(msgid,message)
+end
+
+% Get coefficients
+coefs = pp.coefs;
+[m n] = size(coefs);
+
+if j == 0
+ % Do nothing
+elseif j < n
+ % Derivative of order J
+ D = [n-j:-1:1; ones(j-1,n-j)];
+ D = cumsum(D,1);
+ D = prod(D,1);
+ coefs = coefs(:,1:n-j);
+ for k = 1:n-j
+ coefs(:,k) = D(k)*coefs(:,k);
+ end
+else
+ % Derivative kills PP
+ coefs = zeros(m,1);
+end
+
+% Set output
+qq = pp;
+qq.coefs = coefs;
+qq.order = size(coefs,2);
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppint.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppint.m
new file mode 100644
index 0000000000000000000000000000000000000000..b1b0b7d5cef2babf5accd57aa8fd92273a63b6ed
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/ppint.m
@@ -0,0 +1,69 @@
+function output = ppint(pp,a,b)
+%PPINT Integrate piecewise polynomial.
+% QQ = PPINT(PP,A) returns the indefinite integral from A to X of a
+% piecewise polynomial PP. PP must be on the form evaluated by PPVAL.
+% QQ is a piecewise polynomial on the same form. Default value for A is
+% the leftmost break of PP.
+%
+% I = PPINT(PP,A,B) returns the definite integral from A to B.
+%
+% Example:
+% x = linspace(-pi,pi,7);
+% y = sin(x);
+% pp = spline(x,y);
+% I = ppint(pp,0,pi)
+%
+% qq = ppint(pp,pi/2);
+% xx = linspace(-pi,pi,201);
+% plot(xx,-cos(xx),xx,ppval(qq,xx),'r')
+%
+% See also PPVAL, SPLINE, SPLINEFIT, PPDIFF
+
+% Author: jonas.lundgren@saabgroup.com, 2009.
+
+if nargin < 1, help ppint, return, end
+if nargin < 2, a = pp.breaks(1); end
+
+% Get coefficients and breaks
+coefs = pp.coefs;
+[m n] = size(coefs);
+xb = pp.breaks;
+pdim = prod(pp.dim);
+
+% Interval lengths
+hb = diff(xb);
+hb = repmat(hb,pdim,1);
+hb = hb(:);
+
+% Integration
+coefs(:,1) = coefs(:,1)/n;
+y = coefs(:,1).*hb;
+for k = 2:n
+ coefs(:,k) = coefs(:,k)/(n-k+1);
+ y = (y + coefs(:,k)).*hb;
+end
+y = reshape(y,pdim,[]);
+I = cumsum(y,2);
+I = I(:);
+coefs(:,n+1) = [zeros(pdim,1); I(1:m-pdim)];
+
+% Set preliminary indefinite integral
+qq = pp;
+qq.coefs = coefs;
+qq.order = n+1;
+
+% Set output
+if nargin < 3
+ % Indefinite integral from a to x
+ if a ~= xb(1)
+ I0 = ppval(qq,a);
+ I0 = I0(:);
+ I0 = repmat(I0,m/pdim,1);
+ qq.coefs(:,n+1) = qq.coefs(:,n+1) - I0;
+ end
+ output = qq;
+else
+ % Definite integral from a to b
+ output = ppval(qq,b) - ppval(qq,a);
+end
+
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.m
new file mode 100644
index 0000000000000000000000000000000000000000..c81027bcb9a764c0a6cab575e4f8625b8aa10ee0
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.m
@@ -0,0 +1,611 @@
+function pp = splinefit(varargin)
+%SPLINEFIT Fit a spline to noisy data.
+% PP = SPLINEFIT(X,Y,BREAKS) fits a piecewise cubic spline with breaks
+% (knots) BREAKS to the noisy data (X,Y). X is a vector and Y is a vector
+% or an ND array. If Y is an ND array, then X(j) and Y(:,...,:,j) are
+% matched. Use PPVAL to evaluate PP.
+%
+% PP = SPLINEFIT(X,Y,P) where P is a positive integer interpolates the
+% breaks linearly from the sorted locations of X. P is the number of
+% spline pieces and P+1 is the number of breaks.
+%
+% OPTIONAL INPUT
+% Argument places 4 to 8 are reserved for optional input.
+% These optional arguments can be given in any order:
+%
+% PP = SPLINEFIT(...,'p') applies periodic boundary conditions to
+% the spline. The period length is MAX(BREAKS)-MIN(BREAKS).
+%
+% PP = SPLINEFIT(...,'r') uses robust fitting to reduce the influence
+% from outlying data points. Three iterations of weighted least squares
+% are performed. Weights are computed from previous residuals.
+%
+% PP = SPLINEFIT(...,BETA), where 0 < BETA < 1, sets the robust fitting
+% parameter BETA and activates robust fitting ('r' can be omitted).
+% Default is BETA = 1/2. BETA close 0 gives all data equal weighting.
+% Increase BETA to reduce the influence from outlying data. BETA close
+% to 1 may cause instability or rank deficiency.
+%
+% PP = SPLINEFIT(...,N) sets the spline order to N. Default is a cubic
+% spline with order N = 4. A spline with P pieces has P+N-1 degrees of
+% freedom. With periodic boundary conditions the degrees of freedom are
+% reduced to P.
+%
+% PP = SPLINEFIT(...,CON) applies linear constraints to the spline.
+% CON is a structure with fields 'xc', 'yc' and 'cc':
+% 'xc', x-locations (vector)
+% 'yc', y-values (vector or ND array)
+% 'cc', coefficients (matrix).
+%
+% Constraints are linear combinations of derivatives of order 0 to N-2
+% according to
+%
+% cc(1,j)*y(x) + cc(2,j)*y'(x) + ... = yc(:,...,:,j), x = xc(j).
+%
+% The maximum number of rows for 'cc' is N-1. If omitted or empty 'cc'
+% defaults to a single row of ones. Default for 'yc' is a zero array.
+%
+% EXAMPLES
+%
+% % Noisy data
+% x = linspace(0,2*pi,100);
+% y = sin(x) + 0.1*randn(size(x));
+% % Breaks
+% breaks = [0:5,2*pi];
+%
+% % Fit a spline of order 5
+% pp = splinefit(x,y,breaks,5);
+%
+% % Fit a spline of order 3 with periodic boundary conditions
+% pp = splinefit(x,y,breaks,3,'p');
+%
+% % Constraints: y(0) = 0, y'(0) = 1 and y(3) + y"(3) = 0
+% xc = [0 0 3];
+% yc = [0 1 0];
+% cc = [1 0 1; 0 1 0; 0 0 1];
+% con = struct('xc',xc,'yc',yc,'cc',cc);
+%
+% % Fit a cubic spline with 8 pieces and constraints
+% pp = splinefit(x,y,8,con);
+%
+% % Fit a spline of order 6 with constraints and periodicity
+% pp = splinefit(x,y,breaks,con,6,'p');
+%
+% See also SPLINE, PPVAL, PPDIFF, PPINT
+
+% Author: jonas.lundgren@saabgroup.com, 2010.
+
+% 2009-05-06 Original SPLINEFIT.
+% 2010-06-23 New version of SPLINEFIT based on B-splines.
+% 2010-09-01 Robust fitting scheme added.
+% 2010-09-01 Support for data containing NaNs.
+% 2011-07-01 Robust fitting parameter added.
+
+% Check number of arguments
+error(nargchk(3,8,nargin));
+
+% Check arguments
+[x,y,dim,breaks,n,periodic,beta,constr,weights] = arguments(varargin{:});
+
+% Evaluate B-splines
+base = splinebase(breaks,n);
+pieces = base.pieces;
+A = ppval(base,x);
+
+% Bin data
+[junk,ibin] = histc(x,[-inf,breaks(2:end-1),inf]); %#ok
+
+% Sparse system matrix
+mx = numel(x);
+ii = [ibin; ones(n-1,mx)];
+ii = cumsum(ii,1);
+jj = repmat(1:mx,n,1);
+if periodic
+ ii = mod(ii-1,pieces) + 1;
+ A = sparse(ii,jj,A,pieces,mx);
+else
+ A = sparse(ii,jj,A,pieces+n-1,mx);
+end
+
+% Don't use the sparse solver for small problems
+if pieces < 20*n/log(1.7*n)
+ A = full(A);
+end
+
+% global randw;
+% Solve
+if isempty(constr)
+ % Solve Min norm(u*A-y)
+% weights=ones(size(x));
+ normweights=weights/size(A,1);
+ weightsmat=repmat(normweights,size(A,1),1);
+% weightsmat
+% pause
+% randw
+% A
+% y
+ u = lsqsolve(A.*weightsmat,y.*weightsmat(1:dim,:),beta);
+% u=lsqsolve(A,y,beta);
+% u
+% pause
+ else
+ % Evaluate constraints
+ B = evalcon(base,constr,periodic);
+ % Solve constraints
+ [Z,u0] = solvecon(B,constr);
+ % Solve Min norm(u*A-y), subject to u*B = yc
+ y = y - u0*A;
+ A = Z*A;
+ v = lsqsolve(A,y,beta);
+ u = u0 + v*Z;
+end
+
+% Periodic expansion of solution
+if periodic
+ jj = mod(0:pieces+n-2,pieces) + 1;
+ u = u(:,jj);
+end
+
+% Compute polynomial coefficients
+ii = [repmat(1:pieces,1,n); ones(n-1,n*pieces)];
+ii = cumsum(ii,1);
+jj = repmat(1:n*pieces,n,1);
+C = sparse(ii,jj,base.coefs,pieces+n-1,n*pieces);
+coefs = u*C;
+coefs = reshape(coefs,[],n);
+
+% Make piecewise polynomial
+pp = mkpp(breaks,coefs,dim);
+
+%--------------------------------------------------------------------------
+function [x,y,dim,breaks,n,periodic,beta,constr,weights] = arguments(varargin)
+%ARGUMENTS Lengthy input checking
+% x Noisy data x-locations (1 x mx)
+% y Noisy data y-values (prod(dim) x mx)
+% dim Leading dimensions of y
+% breaks Breaks (1 x (pieces+1))
+% n Spline order
+% periodic True if periodic boundary conditions
+% beta Robust fitting parameter, no robust fitting if beta = 0
+% constr Constraint structure
+% constr.xc x-locations (1 x nx)
+% constr.yc y-values (prod(dim) x nx)
+% constr.cc Coefficients (?? x nx)
+% weights weight vector for distances (Anton)
+
+% Reshape x-data
+x = varargin{1};
+mx = numel(x);
+x = reshape(x,1,mx);
+
+% Remove trailing singleton dimensions from y
+y = varargin{2};
+dim = size(y);
+while numel(dim) > 1 && dim(end) == 1
+ dim(end) = [];
+end
+my = dim(end);
+
+% Leading dimensions of y
+if numel(dim) > 1
+ dim(end) = [];
+else
+ dim = 1;
+end
+
+% Reshape y-data
+pdim = prod(dim);
+y = reshape(y,pdim,my);
+
+% Check data size
+if mx ~= my
+ mess = 'Last dimension of array y must equal length of vector x.';
+ error('arguments:datasize',mess)
+end
+
+% Treat NaNs in x-data
+inan = find(isnan(x));
+if ~isempty(inan)
+ x(inan) = [];
+ y(:,inan) = [];
+ mess = 'All data points with NaN as x-location will be ignored.';
+ warning('arguments:nanx',mess)
+end
+
+% Treat NaNs in y-data
+inan = find(any(isnan(y),1));
+if ~isempty(inan)
+ x(inan) = [];
+ y(:,inan) = [];
+ mess = 'All data points with NaN in their y-value will be ignored.';
+ warning('arguments:nany',mess)
+end
+
+% Check number of data points
+mx = numel(x);
+if mx == 0
+ error('arguments:nodata','There must be at least one data point.')
+end
+
+% Sort data
+if any(diff(x) < 0)
+ [x,isort] = sort(x);
+ y = y(:,isort);
+end
+
+% Breaks
+if isscalar(varargin{3})
+ % Number of pieces
+ p = varargin{3};
+ if ~isreal(p) || ~isfinite(p) || p < 1 || fix(p) < p
+ mess = 'Argument #3 must be a vector or a positive integer.';
+ error('arguments:breaks1',mess)
+ end
+ if x(1) < x(end)
+ % Interpolate breaks linearly from x-data
+ dx = diff(x);
+ ibreaks = linspace(1,mx,p+1);
+ [junk,ibin] = histc(ibreaks,[0,2:mx-1,mx+1]); %#ok
+ breaks = x(ibin) + dx(ibin).*(ibreaks-ibin);
+ else
+ breaks = x(1) + linspace(0,1,p+1);
+ end
+else
+ % Vector of breaks
+ breaks = reshape(varargin{3},1,[]);
+ if isempty(breaks) || min(breaks) == max(breaks)
+ mess = 'At least two unique breaks are required.';
+ error('arguments:breaks2',mess);
+ end
+end
+
+% Unique breaks
+if any(diff(breaks) <= 0)
+ breaks = unique(breaks);
+end
+
+% Optional input defaults
+n = 4; % Cubic splines
+periodic = false; % No periodic boundaries
+robust = false; % No robust fitting scheme
+beta = 0.5; % Robust fitting parameter
+constr = []; % No constraints
+weights = ones(1,length(x)); % all ones weights
+
+% Loop over optional arguments
+for k = 4:nargin
+ a = varargin{k};
+ if ischar(a) && isscalar(a) && lower(a) == 'p'
+ % Periodic conditions
+ periodic = true;
+ elseif ischar(a) && isscalar(a) && lower(a) == 'r'
+ % Robust fitting scheme
+ robust = true;
+ elseif isreal(a) && isscalar(a) && isfinite(a) && a > 0 && a < 1
+ % Robust fitting parameter
+ beta = a;
+ robust = true;
+ elseif isreal(a) && isscalar(a) && isfinite(a) && a > 0 && fix(a) == a
+ % Spline order
+ n = a;
+ elseif isstruct(a) && isscalar(a)
+ % Constraint structure
+ constr = a;
+ elseif all(size(x)==size(a))
+ % weight vector
+ weights=a;
+ else
+ error('arguments:nonsense','Failed to interpret argument #%d.',k)
+ end
+end
+
+% No robust fitting
+if ~robust
+ beta = 0;
+end
+
+% Check exterior data
+h = diff(breaks);
+xlim1 = breaks(1) - 0.01*h(1);
+xlim2 = breaks(end) + 0.01*h(end);
+if x(1) < xlim1 || x(end) > xlim2
+ if periodic
+ % Move data inside domain
+ P = breaks(end) - breaks(1);
+ x = mod(x-breaks(1),P) + breaks(1);
+ % Sort
+ [x,isort] = sort(x);
+ y = y(:,isort);
+ else
+ mess = 'Some data points are outside the spline domain.';
+ warning('arguments:exteriordata',mess)
+ end
+end
+
+% Return
+if isempty(constr)
+ return
+end
+
+% Unpack constraints
+xc = [];
+yc = [];
+cc = [];
+names = fieldnames(constr);
+for k = 1:numel(names)
+ switch names{k}
+ case {'xc'}
+ xc = constr.xc;
+ case {'yc'}
+ yc = constr.yc;
+ case {'cc'}
+ cc = constr.cc;
+ otherwise
+ mess = 'Unknown field ''%s'' in constraint structure.';
+ warning('arguments:unknownfield',mess,names{k})
+ end
+end
+
+% Check xc
+if isempty(xc)
+ mess = 'Constraints contains no x-locations.';
+ error('arguments:emptyxc',mess)
+else
+ nx = numel(xc);
+ xc = reshape(xc,1,nx);
+end
+
+% Check yc
+if isempty(yc)
+ % Zero array
+ yc = zeros(pdim,nx);
+elseif numel(yc) == 1
+ % Constant array
+ yc = zeros(pdim,nx) + yc;
+elseif numel(yc) ~= pdim*nx
+ % Malformed array
+ error('arguments:ycsize','Cannot reshape yc to size %dx%d.',pdim,nx)
+else
+ % Reshape array
+ yc = reshape(yc,pdim,nx);
+end
+
+% Check cc
+if isempty(cc)
+ cc = ones(size(xc));
+elseif numel(size(cc)) ~= 2
+ error('arguments:ccsize1','Constraint coefficients cc must be 2D.')
+elseif size(cc,2) ~= nx
+ mess = 'Last dimension of cc must equal length of xc.';
+ error('arguments:ccsize2',mess)
+end
+
+% Check high order derivatives
+if size(cc,1) >= n
+ if any(any(cc(n:end,:)))
+ mess = 'Constraints involve derivatives of order %d or larger.';
+ error('arguments:difforder',mess,n-1)
+ end
+ cc = cc(1:n-1,:);
+end
+
+% Check exterior constraints
+if min(xc) < xlim1 || max(xc) > xlim2
+ if periodic
+ % Move constraints inside domain
+ P = breaks(end) - breaks(1);
+ xc = mod(xc-breaks(1),P) + breaks(1);
+ else
+ mess = 'Some constraints are outside the spline domain.';
+ warning('arguments:exteriorconstr',mess)
+ end
+end
+
+% Pack constraints
+constr = struct('xc',xc,'yc',yc,'cc',cc);
+
+
+%--------------------------------------------------------------------------
+function pp = splinebase(breaks,n)
+%SPLINEBASE Generate B-spline base PP of order N for breaks BREAKS
+
+breaks = breaks(:); % Breaks
+breaks0 = breaks'; % Initial breaks
+h = diff(breaks); % Spacing
+pieces = numel(h); % Number of pieces
+deg = n - 1; % Polynomial degree
+
+% Extend breaks periodically
+if deg > 0
+ if deg <= pieces
+ hcopy = h;
+ else
+ hcopy = repmat(h,ceil(deg/pieces),1);
+ end
+ % to the left
+ hl = hcopy(end:-1:end-deg+1);
+ bl = breaks(1) - cumsum(hl);
+ % and to the right
+ hr = hcopy(1:deg);
+ br = breaks(end) + cumsum(hr);
+ % Add breaks
+ breaks = [bl(deg:-1:1); breaks; br];
+ h = diff(breaks);
+ pieces = numel(h);
+end
+
+% Initiate polynomial coefficients
+coefs = zeros(n*pieces,n);
+coefs(1:n:end,1) = 1;
+
+% Expand h
+ii = [1:pieces; ones(deg,pieces)];
+ii = cumsum(ii,1);
+ii = min(ii,pieces);
+H = h(ii(:));
+
+% Recursive generation of B-splines
+for k = 2:n
+ % Antiderivatives of splines
+ for j = 1:k-1
+ coefs(:,j) = coefs(:,j).*H/(k-j);
+ end
+ Q = sum(coefs,2);
+ Q = reshape(Q,n,pieces);
+ Q = cumsum(Q,1);
+ c0 = [zeros(1,pieces); Q(1:deg,:)];
+ coefs(:,k) = c0(:);
+ % Normalize antiderivatives by max value
+ fmax = repmat(Q(n,:),n,1);
+ fmax = fmax(:);
+ for j = 1:k
+ coefs(:,j) = coefs(:,j)./fmax;
+ end
+ % Diff of adjacent antiderivatives
+ coefs(1:end-deg,1:k) = coefs(1:end-deg,1:k) - coefs(n:end,1:k);
+ coefs(1:n:end,k) = 0;
+end
+
+% Scale coefficients
+scale = ones(size(H));
+for k = 1:n-1
+ scale = scale./H;
+ coefs(:,n-k) = scale.*coefs(:,n-k);
+end
+
+% Reduce number of pieces
+pieces = pieces - 2*deg;
+
+% Sort coefficients by interval number
+ii = [n*(1:pieces); deg*ones(deg,pieces)];
+ii = cumsum(ii,1);
+coefs = coefs(ii(:),:);
+
+% Make piecewise polynomial
+pp = mkpp(breaks0,coefs,n);
+
+
+%--------------------------------------------------------------------------
+function B = evalcon(base,constr,periodic)
+%EVALCON Evaluate linear constraints
+
+% Unpack structures
+breaks = base.breaks;
+pieces = base.pieces;
+n = base.order;
+xc = constr.xc;
+cc = constr.cc;
+
+% Bin data
+[junk,ibin] = histc(xc,[-inf,breaks(2:end-1),inf]); %#ok
+
+% Evaluate constraints
+nx = numel(xc);
+B0 = zeros(n,nx);
+for k = 1:size(cc,1)
+ if any(cc(k,:))
+ B0 = B0 + repmat(cc(k,:),n,1).*ppval(base,xc);
+ end
+ % Differentiate base
+ coefs = base.coefs(:,1:n-k);
+ for j = 1:n-k-1
+ coefs(:,j) = (n-k-j+1)*coefs(:,j);
+ end
+ base.coefs = coefs;
+ base.order = n-k;
+end
+
+% Sparse output
+ii = [ibin; ones(n-1,nx)];
+ii = cumsum(ii,1);
+jj = repmat(1:nx,n,1);
+if periodic
+ ii = mod(ii-1,pieces) + 1;
+ B = sparse(ii,jj,B0,pieces,nx);
+else
+ B = sparse(ii,jj,B0,pieces+n-1,nx);
+end
+
+
+%--------------------------------------------------------------------------
+function [Z,u0] = solvecon(B,constr)
+%SOLVECON Find a particular solution u0 and null space Z (Z*B = 0)
+% for constraint equation u*B = yc.
+
+yc = constr.yc;
+tol = 1000*eps;
+
+% Remove blank rows
+ii = any(B,2);
+B2 = full(B(ii,:));
+
+% Null space of B2
+if isempty(B2)
+ Z2 = [];
+else
+ % QR decomposition with column permutation
+ [Q,R,dummy] = qr(B2); %#ok
+ R = abs(R);
+ jj = all(R < R(1)*tol, 2);
+ Z2 = Q(:,jj)';
+end
+
+% Sizes
+[m,ncon] = size(B);
+m2 = size(B2,1);
+nz = size(Z2,1);
+
+% Sparse null space of B
+Z = sparse(nz+1:nz+m-m2,find(~ii),1,nz+m-m2,m);
+Z(1:nz,ii) = Z2;
+
+% Warning rank deficient
+if nz + ncon > m2
+ mess = 'Rank deficient constraints, rank = %d.';
+ warning('solvecon:deficient',mess,m2-nz);
+end
+
+% Particular solution
+u0 = zeros(size(yc,1),m);
+if any(yc(:))
+ % Non-homogeneous case
+ u0(:,ii) = yc/B2;
+ % Check solution
+ if norm(u0*B - yc,'fro') > norm(yc,'fro')*tol
+ mess = 'Inconsistent constraints. No solution within tolerance.';
+ error('solvecon:inconsistent',mess)
+ end
+end
+
+
+%--------------------------------------------------------------------------
+function u = lsqsolve(A,y,beta)
+%LSQSOLVE Solve Min norm(u*A-y)
+
+% Avoid sparse-complex limitations
+if issparse(A) && ~isreal(y)
+ A = full(A);
+end
+
+% Solution
+u = y/A;
+
+% Robust fitting
+if beta > 0
+ [m,n] = size(y);
+ alpha = 0.5*beta/(1-beta)/m;
+ for k = 1:3
+ % Residual
+ r = u*A - y;
+ rr = r.*conj(r);
+ rrmean = sum(rr,2)/n;
+ rrmean(~rrmean) = 1;
+ rrhat = (alpha./rrmean)'*rr;
+ % Weights
+ w = exp(-rrhat);
+ spw = spdiags(w',0,n,n);
+ % Solve weighted problem
+ u = (y*spw)/(A*spw);
+ end
+end
+
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.png b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.png
new file mode 100644
index 0000000000000000000000000000000000000000..398cddfdb34347c910993867cef8be1981be856c
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/splinefit/splinefit.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:259845451b08c6369401f0da31bc8e1a0ae2988924355c6177cc503b57fd2fea
+size 83106
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/vectorToMatrices.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/vectorToMatrices.m
new file mode 100644
index 0000000000000000000000000000000000000000..e55ee05fcf2009ca50793f343d17431c14c3451d
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/utils/vectorToMatrices.m
@@ -0,0 +1,38 @@
+function [X Y]=vectorToMatrices(x,stateInfo)
+% transforms the state vector X
+% to matrix representation X and Y
+% X and Y are FxN matrices
+%
+% (C) Anton Andriyenko, 2012
+%
+% The code may be used free of charge for non-commercial and
+% educational purposes, the only requirement is that this text is
+% preserved within the derivative work. For any other purpose you
+% must contact the authors for permission. This code may not be
+% redistributed without written permission from the authors.
+
+F=stateInfo.F; N=stateInfo.N;
+%
+X=zeros(F,N); % x-positions
+Y=zeros(F,N); % y-positions
+
+% naive implementation
+% targetsExist=stateInfo.targetsExist;
+%
+% xind=1;
+% for i=1:N
+% frames=targetsExist(i,1):targetsExist(i,2);
+% nFrames=numel(frames);
+% X(frames,i)=(x(xind:2:xind+2*nFrames-1));
+%
+% Y(frames,i)=(x(xind+1:2:xind+2*nFrames));
+% xind=xind+nFrames*2;
+% end
+
+
+% vectorized implementation
+tiInd=find(stateInfo.tiToInd);
+X(tiInd)=x(1:2:end);
+Y(tiInd)=x(2:2:end);
+
+end
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/vectorToMatrices.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/vectorToMatrices.m
new file mode 100644
index 0000000000000000000000000000000000000000..1e26e1bd859159e241b1c4736834c0258b1e0ed2
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CEM/vectorToMatrices.m
@@ -0,0 +1,41 @@
+function [X Y]=vectorToMatrices(x,stateInfo)
+
+% transforms the state vector x
+% to matrix representation X and Y
+% X and Y are FxN matrices
+%
+% (C) Anton Andriyenko, 2012
+%
+% The code may be used free of charge for non-commercial and
+% educational purposes, the only requirement is that this text is
+% preserved within the derivative work. For any other purpose you
+% must contact the authors for permission. This code may not be
+% redistributed without written permission from the authors.
+
+
+
+F=stateInfo.F; N=stateInfo.N;
+%
+X=zeros(F,N); % x-positions
+Y=zeros(F,N); % y-positions
+
+% naive implementation
+% targetsExist=stateInfo.targetsExist;
+%
+% xind=1;
+% for i=1:N
+% frames=targetsExist(i,1):targetsExist(i,2);
+% nFrames=numel(frames);
+% X(frames,i)=(x(xind:2:xind+2*nFrames-1));
+%
+% Y(frames,i)=(x(xind+1:2:xind+2*nFrames));
+% xind=xind+nFrames*2;
+% end
+
+
+% vectorized implementation
+tiInd=find(stateInfo.tiToInd);
+X(tiInd)=x(1:2:end);
+Y(tiInd)=x(2:2:end);
+
+end
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/CenterToLeft.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/CenterToLeft.m
new file mode 100644
index 0000000000000000000000000000000000000000..ffaef7cb88525a973a1ae64ae535c96f11d540e2
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/CenterToLeft.m
@@ -0,0 +1,19 @@
+function [L_x L_y] = CenterToLeft(x,y,height,width)
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+% (x,y): Center position
+
+h_height = height./2;
+h_width = width./2;
+
+L_x = x - round(h_width);
+L_y = y- round(h_height);
+
+
+
+
+
+
+
+end
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Idx2Types.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Idx2Types.m
new file mode 100644
index 0000000000000000000000000000000000000000..dcaefc36bfa7c4e063b6c3c4826576e2351a998e
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Idx2Types.m
@@ -0,0 +1,20 @@
+function [indx,reliable,new] = Idx2Types(Trk,type)
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+indx = [];
+reliable = [];
+new = [];
+for i=1:length(Trk)
+ if strcmp(Trk(i).type, type)
+ indx = [indx,i];
+ if Trk(i).reliable == 1
+ reliable =[reliable,i];
+ end
+ if Trk(i).isnew == 1
+ new =[new,i];
+ end
+ end
+end
+
+end
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Labelling.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Labelling.m
new file mode 100644
index 0000000000000000000000000000000000000000..73447029c5ba746556a439192f51d18289a21cfb
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/Labelling.m
@@ -0,0 +1,7 @@
+function [param,idx] = Labelling(param)
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+label = param.label;
+idx = min(find(label==0));
+param.label(idx) = 1;
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/LeftToCenter.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/LeftToCenter.m
new file mode 100644
index 0000000000000000000000000000000000000000..a4ead1ff6cf58b1a6f9922f6b14f3565a522a2f2
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/LeftToCenter.m
@@ -0,0 +1,18 @@
+function [C_x,C_y]=LeftToCenter(x,y,height,width)
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+
+h_height = height/2;
+h_width = width/2;
+
+C_x = x + round(h_width);
+C_y = y+ round(h_height);
+
+
+
+
+
+
+
+end
\ No newline at end of file
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/affparam2mat.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/affparam2mat.m
new file mode 100644
index 0000000000000000000000000000000000000000..c61e0716238d63699151dd080b750b153b40844c
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/affparam2mat.m
@@ -0,0 +1,30 @@
+function q = affparam2mat(p)
+% function q = affparam2mat(p)
+%
+% The functions affparam2geom and affparam2mat convert a 'geometric'
+% affine parameter to/from a matrix form (2x3 matrix).
+%
+% affparam2geom converts a 2x3 matrix to 6 affine parameters
+% (x, y, th, scale, aspect, skew), and affparam2mat does the inverse.
+%
+% p(6,n) : [dx dy sc th sr phi]'
+% q(6,n) : [q(1) q(3) q(4); q(2) q(5) q(6)]
+%
+% Reference "Multiple View Geometry in Computer Vision" by Richard
+% Hartley and Andrew Zisserman.
+
+% Copyright (C) Jongwoo Lim and David Ross. All rights reserved.
+
+
+sz = size(p);
+if (length(p(:)) == 6)
+ p = p(:);
+end
+s = p(3,:); th = p(4,:); r = p(5,:); phi = p(6,:);
+cth = cos(th); sth = sin(th); cph = cos(phi); sph = sin(phi);
+ccc = cth.*cph.*cph; ccs = cth.*cph.*sph; css = cth.*sph.*sph;
+scc = sth.*cph.*cph; scs = sth.*cph.*sph; sss = sth.*sph.*sph;
+q(1,:) = p(1,:); q(2,:) = p(2,:);
+q(3,:) = s.*(ccc +scs +r.*(css -scs)); q(4,:) = s.*(r.*(ccs -scc) -ccs -sss);
+q(5,:) = s.*(scc -ccs +r.*(ccs +sss)); q(6,:) = s.*(r.*(ccc +scs) -scs +css);
+q = reshape(q, sz);
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/calc_overlap2.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/calc_overlap2.m
new file mode 100644
index 0000000000000000000000000000000000000000..42253eaa59114fd6981ad43733177ea81602b3fd
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/calc_overlap2.m
@@ -0,0 +1,44 @@
+function [ov, ov_n1, ov_n2] = calc_overlap2(cur_det,prev_det,fr)
+%%f2 can be an array and f1 should be a scalar.
+%%% this will find the overlap between dres1(f1) (only one) and all detection windows in dres2(f2(:))
+
+ %% Calculate overlap
+ n2 = length(prev_det.x);
+
+ cx1 = cur_det.x(fr);
+ cx2 = cur_det.x(fr) + cur_det.w(fr)-1;
+ cy1 = cur_det.y(fr);
+ cy2 = cur_det.y(fr) + cur_det.h(fr)-1;
+
+ gx1 = prev_det.x;
+ gx2 = prev_det.x + prev_det.w-1;
+ gy1 = prev_det.y;
+ gy2 = prev_det.y + prev_det.h-1;
+
+ ca = (cur_det.w(fr)).* (cur_det.h(fr)); % area
+ ga = (prev_det.w).* (prev_det.h);
+
+
+
+ xx1 = max(cx1, gx1);
+ yy1 = max(cy1, gy1);
+ xx2 = min(cx2, gx2);
+ yy2 = min(cy2, gy2);
+ w = xx2 - xx1 + 1;
+ h = yy2 - yy1 + 1;
+
+ inds = find((w>0).*(h>0));
+ ov = zeros(1,n2);
+ ov_n1 = zeros(1,n2);
+ ov_n2 = zeros(1,n2);
+ inter = w(inds).*h(inds); %% area of overlap
+ u = ca + ga(inds) - w(inds).*h(inds); %% area of union
+ ov(inds) = inter./u; % intersection/union
+ ov_n1(inds) = inter ./ ca;
+ ov_n2(inds) = inter ./ga(inds);
+end
+
+
+
+
+
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/estimation_size.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/estimation_size.m
new file mode 100644
index 0000000000000000000000000000000000000000..4cdfd47b7a9298ef821053bd422baea1cfc77169
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/estimation_size.m
@@ -0,0 +1,28 @@
+function [size_state] = estimation_size(Trk,ystate,fr)
+
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+init_time = Trk.ifr;
+nof_s = 4;
+
+sum_size = Trk.state{end}(3:4);
+if fr == Trk.last_update;
+ size_state = ystate(3:4);
+else
+ if fr > init_time + 3
+ for j=1:nof_s
+ sum_size = sum_size + Trk.state{end-j}(3:4);
+ end
+ size_state = sum_size/(nof_s+1);
+ else
+ all_est = cell2mat(Trk.state);
+ sum_size = sum_size + sum(all_est(3:4,:),2);
+ size_state = sum_size/ (size(all_est,2)+1);
+ end
+
+end
+
+
+
+end
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/motion_affinity.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/motion_affinity.m
new file mode 100644
index 0000000000000000000000000000000000000000..0a6f8308cef15d92360bef6b5d923b0bc5c89e31
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/motion_affinity.m
@@ -0,0 +1,5 @@
+function prob_pdf = motion_affinity(x,mean,var)
+%% Copyright (C) 2014 Seung-Hwan Bae
+%% All rights reserved.
+
+prob_pdf = exp(-0.5*(x-mean)'*inv(var)*(x-mean));
diff --git a/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/munkres.m b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/munkres.m
new file mode 100644
index 0000000000000000000000000000000000000000..c150740ddc41c87a35fbe636f346db0953c75555
--- /dev/null
+++ b/DETRAC/DETRAC-MOT-toolkit/DETRAC-MOT-toolkit/trackers/CMOT/Common/munkres.m
@@ -0,0 +1,154 @@
+function [assignment,cost] = munkres(costMat)
+% MUNKRES Munkres Assign Algorithm
+%
+% [ASSIGN,COST] = munkres(COSTMAT) returns the optimal assignment in ASSIGN
+% with the minimum COST based on the assignment problem represented by the
+% COSTMAT, where the (i,j)th element represents the cost to assign the jth
+% job to the ith worker.
+%
+
+% This is vectorized implementation of the algorithm. It is the fastest
+% among all Matlab implementations of the algorithm.
+
+% Examples
+% Example 1: a 5 x 5 example
+%{
+[assignment,cost] = munkres(magic(5));
+[assignedrows,dum]=find(assignment);
+disp(assignedrows'); % 3 2 1 5 4
+disp(cost); %15
+%}
+% Example 2: 400 x 400 random data
+%{
+n=400;
+A=rand(n);
+tic
+[a,b]=munkres(A);
+toc % about 6 seconds
+%}
+
+% Reference:
+% "Munkres' Assignment Algorithm, Modified for Rectangular Matrices",
+% http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html
+
+% version 1.0 by Yi Cao at Cranfield University on 17th June 2008
+
+assignment = false(size(costMat));
+cost = 0;
+
+costMat(costMat~=costMat)=Inf;
+validMat = costMat