25 #include "TrackerBaseWeighted.hh"
30 #define SMALL_DET 1e-8
32 template <
class StorageType>
38 _useGaussianWeightMatrix =
false;
41 template <
class StorageType>
50 if (_useGaussianWeightMatrix)
51 CalcGaussianWeightMatrix_();
55 if ((
int)_weight.GetRows() != _WinSize ||
56 (int)_weight.GetCols() != _WinSize)
58 BIASERR(
"Matrix height and width does not match window size!");
63 BIASCDOUT(D_TRACKERB_KLT,
"p1: " << p1[0] <<
", " << p1[1]
64 <<
"\tp2: " <<p2[0] <<
", " << p2[1]<<endl);
69 register KLT_TYPE dI, ex, ey, dx, dy, det;
71 const int hws=_HalfWinSize;
72 const int winsize=_WinSize;
73 const int maxiter=_MaxIterations;
74 const KLT_TYPE maxerr=_MaxError;
76 if (p1[0]<_minx1 || p1[0]>=_maxx1 ||
77 p1[1]<_miny1 || p1[1]>=_maxy1 ||
78 p2[0]<_minx2 || p2[0]>=_maxx2 ||
79 p2[1]<_miny2 || p2[1]>=_maxy2 ){
80 BIASCDOUT(D_TRACKERB_KLT,
"p1 ("<<p1[0]<<
", "<<p1[1]<<
") p2 ("<<p2[0]
81 <<
", "<<p2[1]<<
")\n");
82 BIASCDOUT(D_TRACKERB_KLT,
"tracking roi im1 : ( "<<_minx1<<
", "<<_miny1
83 <<
") <--> ( "<<_maxx1<<
", "<<_maxy1<<
")\n");
84 BIASCDOUT(D_TRACKERB_KLT,
"tracking roi im2 : ( "<<_minx2<<
", "<<_miny2
85 <<
") <--> ( "<<_maxx2<<
", "<<_maxy2<<
")\n");
86 BIASCDOUT(D_TRACKERB_KLT,
"points do not lie in ROI, returning -4\n");
89 BIASCDOUT(D_TRACKERB_KLT,
"tracking roi im1 : ( "<<_minx1<<
", "<<_miny1
90 <<
") <--> ( "<<_maxx1<<
", "<<_maxy1<<
")\n");
91 BIASCDOUT(D_TRACKERB_KLT,
"tracking roi im2 : ( "<<_minx2<<
", "<<_miny2
92 <<
") <--> ( "<<_maxx2<<
", "<<_maxy2<<
")\n");
100 BilinearRegion1_(p1[0],p1[1],hws);
102 _gxx=_gxy=_gyy=ex=ey=0.0;
105 BilinearRegion2_(result[0],result[1],hws);
107 for (rh=0; rh<winsize; rh++){
108 for (ch=0; ch<winsize; ch++){
110 _weight[rh][ch] * (_gx1[rh][ch] + _gx2[rh][ch]);
112 _weight[rh][ch] * (_gy1[rh][ch] + _gy2[rh][ch]);
113 _gxx += _gsx[rh][ch]*_gsx[rh][ch];
114 _gyy += _gsy[rh][ch]*_gsy[rh][ch];
115 _gxy += _gsx[rh][ch]*_gsy[rh][ch];
116 dI = _weight[rh][ch] * (_bl1[rh][ch] - _bl2[rh][ch]);
117 ex += _gsx[rh][ch]*dI;
118 ey += _gsy[rh][ch]*dI;
121 BIASCDOUT(D_TRACKERB_KLT,
"_gxx: "<<_gxx<<
" _gxy: "<<_gxy
122 <<
" _gyy: "<<_gyy<<
" ex: "<<ex<<
" ey: "<<ey<<endl);
123 det = _gxx*_gyy-_gxy*_gxy;
124 if (fabs(det)<SMALL_DET) {
125 BIASCDOUT(D_TRACKERB_KLT,
126 "unable to invert G, det: "<<det<<endl);
132 dx = (_gyy*ex - _gxy*ey)*_idet;
133 dy = (_gxx*ey - _gxy*ex)*_idet;
135 BIASCDOUT(D_TRACKERB_KLT,
"det: "<<det<<
" _idet: "<<_idet
136 <<
" dx: "<<dx<<
" dy: "<<dy<<endl);
139 error = (fabs(dx)>fabs(dy))?fabs(dx):fabs(dy);
155 BIASCDOUT(D_TRACKERB_KLT,
"disp after "<<iter<<
" iteration: "
156 <<result[0]-p2[0]<<
", "<<result[1]-p2[1]<<
" error: "<< error
157 <<
" ("<<result[0]<<
", "<<result[1]<<
")\n");
161 if (result[0]<_minx2 || result[0]>=_maxx2 ||
162 result[1]<_miny2 || result[1]>=_maxy2){
163 BIASCDOUT(D_TRACKERB_KLT,
164 "feature slid out of image, returning -2\n");
168 }
while (error>maxerr && iter<maxiter);
170 if (res==0 && error>maxerr) {
172 BIASCDOUT(D_TRACKERB_KLT,
"error > maxerror : returning -3\n");
174 BilinearRegion2_(result[0],result[1],hws);
179 cerr <<
"i "<<iter<<
" res "<<res<<
" ";
180 cerr <<
"TrackWeighted_ took "<<timer.
GetRealTime()<<
" us = "
187 template <
class StorageType>
191 _weight.newsize(_WinSize, _WinSize);
193 double sigma = ((double)_WinSize)/6.0;
194 double fac = 1.0 / (2.0 * sigma * sigma);
196 register int i, j, x, y, xp, xm, yp, ym;
197 const int hws=_HalfWinSize;
198 for (i= hws; i >= 0; i--){
199 for (j= hws; j >= 0; j--){
200 tmp=exp(-(
double)(i*i+j*j)*fac);
205 _weight[ym][xm] =_weight[yp][xm] = _weight[ym][xp] = _weight[yp][xp] =
210 for (y=0; y<_WinSize; y++){
211 for (x=0; x<_WinSize; x++){
215 BIASCDOUT(D_TRACKERB_INIT,
"_weight: "<<_weight<<endl);
216 BIASCDOUT(D_TRACKERB_INIT,
"_weight sum before normalization: "<<sum<<endl);
220 for (i= hws; i >= 0; i--){
221 for (j= hws; j >= 0; j--){
227 _weight[ym][xm] = _weight[yp][xm] = _weight[ym][xp] =
228 _weight[yp][xp] = tmp * sum;
231 BIASCDOUT(D_TRACKERB_INIT,
"_weight: "<<_weight<<endl);
234 for (y=0; y<_WinSize; y++){
235 for (x=0; x<_WinSize; x++){
239 BIASCDOUT(D_TRACKERB_INIT,
"_weight sum after normalization: "<<sum<<endl);
252 #ifdef BUILD_IMAGE_INT
255 #ifdef BUILD_IMAGE_CHAR
258 #ifdef BUILD_IMAGE_SHORT
261 #ifdef BUILD_IMAGE_USHORT
264 #ifdef BUILD_IMAGE_UINT
267 #ifdef BUILD_IMAGE_DOUBLE
virtual int Track_(Vector2< KLT_TYPE > &p1, Vector2< KLT_TYPE > &p2, Vector2< KLT_TYPE > &result, KLT_TYPE &error, int &iter, const Matrix2x2< KLT_TYPE > &AffinePred, Matrix2x2< KLT_TYPE > &AffineResult)
Interface of all tracking algorithms implemented in derived classes.
success (error < maxerror)
no spatial structure is present
void CalcGaussianWeightMatrix_()
double GetCycleCount() const
return number of cycles between all subsequent calls to Start() and Stop() since last call to Reset()...
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
a point lies outside of valid region in images
void SetWeightMatrix(const Matrix< KLT_TYPE > &weight)
Overwritten virtual function from TrackerBaseInterface.
maxiter is reached and error is above maxerror