39 #include <visp3/core/vpException.h>
40 #include <visp3/tt_mi/vpTemplateTrackerMI.h>
41 #include <visp3/tt_mi/vpTemplateTrackerMIBSpline.h>
74 :
vpTemplateTracker(_warp), hessianComputation(USE_HESSIEN_NORMAL), ApproxHessian(HESSIAN_NEW), lambda(0),
75 temp(NULL), Prt(NULL), dPrt(NULL), Pt(NULL), Pr(NULL), d2Prt(NULL), PrtTout(NULL),
76 dprtemp(NULL), PrtD(NULL), dPrtD(NULL), influBspline(0), bspline(3), Nc(8), Ncb(0),
77 d2Ix(), d2Iy(), d2Ixy(), MI_preEstimation(0), MI_postEstimation(0),
78 NMI_preEstimation(0), NMI_postEstimation(0), covarianceMatrix(), computeCovariance(false)
145 unsigned int Ncb_ = (
unsigned int)
Ncb;
151 Warp->computeCoeff(tp);
158 Warp->computeDenom(
X1,tp);
173 cr=(int)((IW*(
Nc-1))/255.);
174 ct=(int)((Tij*(
Nc-1))/255.);
175 er=(IW*(
Nc-1))/255.-cr;
176 et=((double)Tij*(
Nc-1))/255.-ct;
179 vpTemplateTrackerMIBSpline::PutPVBsplineD(
PrtD, cr, er, ct, et,
Nc, 1.,
bspline);
186 for(
int r=0;r<
Nc;r++)
187 for(
int t=0;t<
Nc;t++)
202 for(
unsigned int r=0;r<Ncb_;r++)
203 for(
unsigned int t=0;t<Ncb_;t++)
205 Prt[r*Ncb_+t]=
Prt[r*Ncb_+t]/Nbpoint;
207 memset(
Pr, 0, Ncb_*
sizeof(
double));
208 for(
unsigned int r=0;r<Ncb_;r++)
210 for(
unsigned int t=0;t<Ncb_;t++)
211 Pr[r]+=
Prt[r*Ncb_+t];
215 memset(
Pt, 0, Ncb_*
sizeof(
double));
216 for(
unsigned int t=0;t<Ncb_;t++)
218 for(
unsigned int r=0;r<Ncb_;r++)
219 Pt[t]+=
Prt[r*Ncb_+t];
221 for(
unsigned int r=0;r<Ncb_;r++)
223 if(std::fabs(
Pr[r]) > std::numeric_limits<double>::epsilon())
224 MI-=
Pr[r]*log(
Pr[r]);
226 for(
unsigned int t=0;t<Ncb_;t++)
228 if(std::fabs(
Pt[t]) > std::numeric_limits<double>::epsilon())
229 MI-=
Pt[t]*log(
Pt[t]);
231 for(
unsigned int r=0;r<Ncb_;r++)
232 for(
unsigned int t=0;t<Ncb_;t++)
234 if(std::fabs(
Prt[r*Ncb_+t]) > std::numeric_limits<double>::epsilon())
235 MI+=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
253 double Prt_[256][256];
255 memset(Pr_, 0, 256*
sizeof(
double));
256 memset(Pt_, 0, 256*
sizeof(
double));
257 memset(Prt_, 0, 256*256*
sizeof(
double));
265 Warp->computeDenom(
X1,tp);
274 IW=I[(int)i2][(
int)j2];
280 Prt_[(int)Tij][(
int)IW]++;
284 for(
int i = 0 ; i < 256 ; i++)
288 for(
int j = 0 ; j < 256 ; j++)
289 Prt_[i][j] /= Nbpoint;
292 for(
int r=0;r<256;r++)
294 if(std::fabs(Pr_[r]) > std::numeric_limits<double>::epsilon())
295 MI-=Pr_[r]*log(Pr_[r]);
297 for(
int t=0;t<256;t++)
299 if(std::fabs(Pt_[t]) > std::numeric_limits<double>::epsilon())
300 MI-=Pt_[t]*log(Pt_[t]);
303 for(
int r=0;r<256;r++)
304 for(
int t=0;t<256;t++)
306 if(std::fabs(Prt_[r][t]) > std::numeric_limits<double>::epsilon())
307 denom-=(Prt_[r][t]*log(Prt_[r][t]));
310 if(std::fabs(denom) > std::numeric_limits<double>::epsilon())
334 unsigned int Nc_ = (
unsigned int)
Nc;
335 unsigned int Ncb_ = (
unsigned int)
Ncb;
336 unsigned int bspline_ = (
unsigned int)
bspline;
338 for(
unsigned int r=0;r<Nc_;r++) {
339 for(
unsigned int t=0;t<Nc_;t++)
341 for(
unsigned int r2=0;r2<bspline_;r2++) {
342 for(
unsigned int t2=0;t2<bspline_;t2++)
344 Prt[((r2+r)*Ncb_+(t2+t))]+=*pt++;
345 for(
unsigned int ip=0;ip<
nbParam;ip++)
347 dPrt[((r2+r)*Ncb_+(t2+t))*nbParam+ip]+=*pt++;
348 for(
unsigned int it=0;it<
nbParam;it++){
349 d2Prt[((r2+r)*Ncb_+(t2+t))*nbParam*nbParam+ip*nbParam+it]+=*pt++;
406 unsigned int indd, indd2;
408 for(
int i=0;i<
Ncb*
Ncb;i++){
410 for(
unsigned int j=0;j<
nbParam;j++){
413 for(
unsigned int k=0;k<
nbParam;k++){
423 unsigned int Ncb_ = (
unsigned int)
Ncb;
426 memset(
Pr, 0, Ncb_*
sizeof(
double));
427 for(
unsigned int r=0;r<Ncb_;r++)
429 for(
unsigned int t=0;t<Ncb_;t++)
430 Pr[r]+=
Prt[r*Ncb_+t];
434 memset(
Pt, 0, Ncb_*
sizeof(
double));
435 for(
unsigned int t=0;t<Ncb_;t++)
437 for(
unsigned int r=0;r<Ncb_;r++)
438 Pt[t]+=
Prt[r*Ncb_+t];
443 for(
unsigned int r=0;r<Ncb_;r++)
446 if(std::fabs(
Pr[r]) > std::numeric_limits<double>::epsilon())
448 entropieI-=
Pr[r]*log(
Pr[r]);
449 MI-=
Pr[r]*log(
Pr[r]);
454 for(
unsigned int t=0;t<Ncb_;t++)
457 if(std::fabs(
Pt[t]) > std::numeric_limits<double>::epsilon())
459 entropieT-=
Pt[t]*log(
Pt[t]);
460 MI-=
Pt[t]*log(
Pt[t]);
464 for(
unsigned int r=0;r<Ncb_;r++)
465 for(
unsigned int t=0;t<Ncb_;t++)
467 if(std::fabs(
Prt[r*Ncb_+t]) > std::numeric_limits<double>::epsilon())
468 MI+=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
473 double seuilevitinf=1e-200;
476 unsigned int Ncb_ = (
unsigned int)
Ncb;
477 for(
unsigned int t=0;t<Ncb_;t++)
480 if(
Pt[t]>seuilevitinf)
482 for(
unsigned int r=0;r<Ncb_;r++)
485 if(
Prt[r*Ncb_+t]>seuilevitinf)
487 for(
unsigned int it=0;it<
nbParam;it++)
490 dtemp=1.+log(
Prt[r*Ncb_+t]/
Pt[t]);
491 for(
unsigned int it=0;it<
nbParam;it++)
492 for(
unsigned int jt=0;jt<
nbParam;jt++)
494 Hessian[it][jt]+=
dprtemp[it]*
dprtemp[jt]*(1./
Prt[r*Ncb_+t]-1./
Pt[t])+
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*dtemp;
496 Hessian[it][jt]+=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*dtemp;
512 double seuilevitinf=1e-200;
515 double *du =
new double[
nbParam];
516 double *dv =
new double[
nbParam];
517 double *A =
new double[
nbParam];
518 double *dB =
new double[
nbParam];
519 double **d2u =
new double *[
nbParam];
520 double **d2v =
new double *[
nbParam];
521 double **dA =
new double *[
nbParam];
522 for (
unsigned int i = 0; i <
nbParam; i++) {
528 memset(du, 0, nbParam*
sizeof(
double));
529 memset(dv, 0, nbParam*
sizeof(
double));
530 memset(A, 0, nbParam*
sizeof(
double));
531 memset(dB, 0, nbParam*
sizeof(
double));
532 memset(
dprtemp, 0, nbParam*
sizeof(
double));
534 for(
unsigned int it=0;it<
nbParam;it++){
535 for(
unsigned int jt=0;jt<
nbParam;jt++){
536 memset(d2u[it], 0, nbParam*
sizeof(
double));
537 memset(d2v[it], 0, nbParam*
sizeof(
double));
538 memset(dA[it], 0, nbParam*
sizeof(
double));
542 unsigned int Ncb_ = (
unsigned int)
Ncb;
544 for(
unsigned int t=0;t<Ncb_;t++)
547 if(
Pt[t]>seuilevitinf)
549 for(
unsigned int r=0;r<Ncb_;r++)
552 if(
Prt[r*Ncb_+t]>seuilevitinf)
554 for(
unsigned int it=0;it<
nbParam;it++){
560 u +=
Prt[r*Ncb_+t]*log(
Pt[t]*
Pr[r]);
562 v +=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
564 for(
unsigned int it=0;it<
nbParam;it++){
571 for(
unsigned int it=0;it<
nbParam;it++){
572 for(
unsigned int jt=0;jt<
nbParam;jt++){
573 d2u[it][jt] +=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*log(
Pt[t]*
Pr[r])
575 d2v[it][jt] +=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*(1+log(
Prt[r*Ncb_+t]))
585 for(
unsigned int it=0;it<
nbParam;it++){
587 A[it] = du[it] * v - u * dv[it];
589 dB[it] = 2 * v * dv[it];
592 for(
unsigned int jt=0;jt<
nbParam;jt++){
594 dA[it][jt] = d2u[it][jt]*v-d2v[it][jt]*u;
596 Hessian[it][jt] = (dA[it][jt] * B -A[it] * dB[it])/(B*B);
604 for (
unsigned int i = 0; i <
nbParam; i++) {
620 double seuilevitinf=1e-200;
622 unsigned int Ncb_ = (
unsigned int)
Ncb;
624 for(
unsigned int t=0;t<Ncb_;t++)
627 if(
Pt[t]>seuilevitinf)
629 for(
unsigned int r=0;r<Ncb_;r++)
631 if(
Prt[r*Ncb_+t]>seuilevitinf)
634 for(
unsigned int it=0;it<
nbParam;it++)
637 dtemp=1.+log(
Prt[r*Ncb_+t]/
Pt[t]);
639 for(
unsigned int it=0;it<
nbParam;it++)
649 unsigned int Ncb_ = (
unsigned int)
Ncb;
650 unsigned int Nc_ = (
unsigned int)
Nc;
651 unsigned int influBspline_ = (
unsigned int)
influBspline;
653 memset(
Prt, 0, Ncb_*Ncb_*
sizeof(
double));
666 int tNcb=nc+bspline_;
667 int tinfluBspline=bspline_*bspline_;
668 double *tPrtD =
new double[nc*nc*tinfluBspline];
669 double *tPrt =
new double[tNcb*tNcb];
670 double *tPr =
new double[tNcb];
671 double *tPt =
new double[tNcb];
685 memset(tPrt, 0, tNcb*tNcb*
sizeof(
double));
686 memset(tPrtD, 0, nc*nc*tinfluBspline*
sizeof(
double));
689 Warp->computeCoeff(tp);
696 Warp->computeDenom(
X1,tp);
711 cr=(int)((IW*(nc-1))/255.);
712 ct=(int)((Tij*(nc-1))/255.);
713 er=(IW*(nc-1))/255.-cr;
714 et=((double)Tij*(nc-1))/255.-ct;
717 vpTemplateTrackerMIBSpline::PutPVBsplineD(tPrtD, cr, er, ct, et, nc, 1.,bspline_);
721 for(
int r=0;r<nc;r++)
722 for(
int t=0;t<nc;t++)
724 for(i=0;i<tinfluBspline;i++)
729 tPrt[r2*tNcb+t2]+=*pt;
745 for(
int r=0;r<tNcb;r++)
746 for(
int t=0;t<tNcb;t++)
748 tPrt[r*tNcb+t]=tPrt[r*tNcb+t]/Nbpoint;
750 memset(tPr, 0, tNcb*
sizeof(
double));
751 for(
int r=0;r<tNcb;r++)
753 for(
int t=0;t<tNcb;t++)
754 tPr[r]+=tPrt[r*tNcb+t];
758 memset(tPt, 0, tNcb*
sizeof(
double));
759 for(
int t=0;t<tNcb;t++)
761 for(
int r=0;r<tNcb;r++)
762 tPt[t]+=tPrt[r*tNcb+t];
764 for(
int r=0;r<tNcb;r++)
766 if(std::fabs(tPr[r]) > std::numeric_limits<double>::epsilon())
767 MI-=tPr[r]*log(tPr[r]);
769 for(
int t=0;t<tNcb;t++)
771 if(std::fabs(tPt[t]) > std::numeric_limits<double>::epsilon())
772 MI-=tPt[t]*log(tPt[t]);
774 for(
int r=0;r<tNcb;r++)
775 for(
int t=0;t<tNcb;t++)
777 if(std::fabs(tPrt[r*tNcb+t]) > std::numeric_limits<double>::epsilon())
778 MI+=tPrt[r*tNcb+t]*log(tPrt[r*tNcb+t]);
804 Warp->computeCoeff(tp);
811 Warp->computeDenom(
X1,tp);
824 IW=(
unsigned int)GaussI.
getValue(i2,j2);
834 "Cannot get MI; number of points = 0"));
836 Prt256=Prt256/Nbpoint;
842 for(
unsigned int t=0;t<256;t++)
844 for(
unsigned int r=0;r<256;r++)
847 if(std::fabs(Prt256[r][t]) > std::numeric_limits<double>::epsilon())
848 MI+=Prt256[r][t]*log(Prt256[r][t]);
851 if(std::fabs(Pt256[t]) > std::numeric_limits<double>::epsilon())
852 MI+=-Pt256[t]*log(Pt256[t]);
854 if(std::fabs(Pr256[t]) > std::numeric_limits<double>::epsilon())
855 MI+=-Pr256[t]*log(Pr256[t]);
vpTemplateTrackerMI()
Default constructor.
Implementation of a matrix and operations on matrices.
void computeHessien(vpMatrix &H)
unsigned int getWidth() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
vpTemplateTrackerPoint * ptTemplate
virtual void warpX(const int &i, const int &j, double &i2, double &j2, const vpColVector &ParamM)=0
error that can be emited by ViSP classes.
Type getValue(double i, double j) const
vpHessienApproximationType ApproxHessian
double getMI256(const vpImage< unsigned char > &I, const vpColVector &tp)
unsigned int templateSize
Error that can be emited by the vpTracker class and its derivates.
double getCost(const vpImage< unsigned char > &I, const vpColVector &tp)
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M)
void computeMI(double &MI)
double getNormalizedCost(const vpImage< unsigned char > &I, const vpColVector &tp)
Implementation of column vector and the associated operations.
unsigned int getHeight() const
void computeHessienNormalized(vpMatrix &H)
void setBspline(const vpBsplineType &newbs)
vpTemplateTrackerWarp * Warp
void computeProba(int &nbpoint)
vpHessienType hessianComputation
void resize(const unsigned int i, const bool flagNullify=true)