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)
140 unsigned int Ncb_ = (
unsigned int)
Ncb;
141 unsigned int Nc_ = (
unsigned int)
Nc;
142 unsigned int influBspline_ = (
unsigned int)
influBspline;
144 memset(
Prt, 0, Ncb_*Ncb_*
sizeof(
double));
145 memset(
PrtD, 0, Nc_*Nc_*influBspline_*
sizeof(
double));
148 Warp->computeCoeff(tp);
155 Warp->computeDenom(
X1,tp);
171 int cr=(int)((IW*(
Nc-1))/255.);
172 int ct=(int)((Tij*(
Nc-1))/255.);
173 double er=(IW*(
Nc-1))/255.-cr;
174 double et=((double)Tij*(
Nc-1))/255.-ct;
177 vpTemplateTrackerMIBSpline::PutPVBsplineD(
PrtD, cr, er, ct, et,
Nc, 1.,
bspline);
184 for(
int r=0;r<
Nc;r++)
185 for(
int t=0;t<
Nc;t++)
200 for(
unsigned int r=0;r<Ncb_;r++)
201 for(
unsigned int t=0;t<Ncb_;t++)
203 Prt[r*Ncb_+t]=
Prt[r*Ncb_+t]/Nbpoint;
205 memset(
Pr, 0, Ncb_*
sizeof(
double));
206 for(
unsigned int r=0;r<Ncb_;r++)
208 for(
unsigned int t=0;t<Ncb_;t++)
209 Pr[r]+=
Prt[r*Ncb_+t];
213 memset(
Pt, 0, Ncb_*
sizeof(
double));
214 for(
unsigned int t=0;t<Ncb_;t++)
216 for(
unsigned int r=0;r<Ncb_;r++)
217 Pt[t]+=
Prt[r*Ncb_+t];
219 for(
unsigned int r=0;r<Ncb_;r++)
221 if(std::fabs(
Pr[r]) > std::numeric_limits<double>::epsilon())
222 MI-=
Pr[r]*log(
Pr[r]);
224 for(
unsigned int t=0;t<Ncb_;t++)
226 if(std::fabs(
Pt[t]) > std::numeric_limits<double>::epsilon())
227 MI-=
Pt[t]*log(
Pt[t]);
229 for(
unsigned int r=0;r<Ncb_;r++)
230 for(
unsigned int t=0;t<Ncb_;t++)
232 if(std::fabs(
Prt[r*Ncb_+t]) > std::numeric_limits<double>::epsilon())
233 MI+=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
249 double Prt_[256][256];
251 memset(Pr_, 0, 256*
sizeof(
double));
252 memset(Pt_, 0, 256*
sizeof(
double));
253 memset(Prt_, 0, 256*256*
sizeof(
double));
261 Warp->computeDenom(
X1,tp);
271 IW=I[(int)i2][(
int)j2];
277 Prt_[(int)Tij][(
int)IW]++;
281 for(
int i = 0 ; i < 256 ; i++)
285 for(
int j = 0 ; j < 256 ; j++)
286 Prt_[i][j] /= Nbpoint;
289 for(
int r=0;r<256;r++)
291 if(std::fabs(Pr_[r]) > std::numeric_limits<double>::epsilon())
292 MI-=Pr_[r]*log(Pr_[r]);
294 for(
int t=0;t<256;t++)
296 if(std::fabs(Pt_[t]) > std::numeric_limits<double>::epsilon())
297 MI-=Pt_[t]*log(Pt_[t]);
300 for(
int r=0;r<256;r++)
301 for(
int t=0;t<256;t++)
303 if(std::fabs(Prt_[r][t]) > std::numeric_limits<double>::epsilon())
304 denom-=(Prt_[r][t]*log(Prt_[r][t]));
307 if(std::fabs(denom) > std::numeric_limits<double>::epsilon())
331 unsigned int Nc_ = (
unsigned int)
Nc;
332 unsigned int Ncb_ = (
unsigned int)
Ncb;
333 unsigned int bspline_ = (
unsigned int)
bspline;
335 for(
unsigned int r=0;r<Nc_;r++) {
336 for(
unsigned int t=0;t<Nc_;t++)
338 for(
unsigned int r2=0;r2<bspline_;r2++) {
339 for(
unsigned int t2=0;t2<bspline_;t2++)
341 Prt[((r2+r)*Ncb_+(t2+t))]+=*pt++;
342 for(
unsigned int ip=0;ip<
nbParam;ip++)
344 dPrt[((r2+r)*Ncb_+(t2+t))*nbParam+ip]+=*pt++;
345 for(
unsigned int it=0;it<
nbParam;it++){
346 d2Prt[((r2+r)*Ncb_+(t2+t))*nbParam*nbParam+ip*nbParam+it]+=*pt++;
403 unsigned int indd, indd2;
405 for(
int i=0;i<
Ncb*
Ncb;i++){
407 for(
unsigned int j=0;j<
nbParam;j++){
410 for(
unsigned int k=0;k<
nbParam;k++){
420 unsigned int Ncb_ = (
unsigned int)
Ncb;
423 memset(
Pr, 0, Ncb_*
sizeof(
double));
424 for(
unsigned int r=0;r<Ncb_;r++)
426 for(
unsigned int t=0;t<Ncb_;t++)
427 Pr[r]+=
Prt[r*Ncb_+t];
431 memset(
Pt, 0, Ncb_*
sizeof(
double));
432 for(
unsigned int t=0;t<Ncb_;t++)
434 for(
unsigned int r=0;r<Ncb_;r++)
435 Pt[t]+=
Prt[r*Ncb_+t];
440 for(
unsigned int r=0;r<Ncb_;r++)
443 if(std::fabs(
Pr[r]) > std::numeric_limits<double>::epsilon())
446 MI-=
Pr[r]*log(
Pr[r]);
451 for(
unsigned int t=0;t<Ncb_;t++)
454 if(std::fabs(
Pt[t]) > std::numeric_limits<double>::epsilon())
457 MI-=
Pt[t]*log(
Pt[t]);
461 for(
unsigned int r=0;r<Ncb_;r++)
462 for(
unsigned int t=0;t<Ncb_;t++)
464 if(std::fabs(
Prt[r*Ncb_+t]) > std::numeric_limits<double>::epsilon())
465 MI+=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
470 double seuilevitinf=1e-200;
473 unsigned int Ncb_ = (
unsigned int)
Ncb;
474 for(
unsigned int t=0;t<Ncb_;t++)
477 if(
Pt[t]>seuilevitinf)
479 for(
unsigned int r=0;r<Ncb_;r++)
482 if(
Prt[r*Ncb_+t]>seuilevitinf)
484 for(
unsigned int it=0;it<
nbParam;it++)
487 dtemp=1.+log(
Prt[r*Ncb_+t]/
Pt[t]);
488 for(
unsigned int it=0;it<
nbParam;it++)
489 for(
unsigned int jt=0;jt<
nbParam;jt++)
491 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;
493 Hessian[it][jt]+=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*dtemp;
509 double seuilevitinf=1e-200;
512 double *du =
new double[
nbParam];
513 double *dv =
new double[
nbParam];
514 double *A =
new double[
nbParam];
515 double *dB =
new double[
nbParam];
516 double **d2u =
new double *[
nbParam];
517 double **d2v =
new double *[
nbParam];
518 double **dA =
new double *[
nbParam];
519 for (
unsigned int i = 0; i <
nbParam; i++) {
525 memset(du, 0, nbParam*
sizeof(
double));
526 memset(dv, 0, nbParam*
sizeof(
double));
527 memset(A, 0, nbParam*
sizeof(
double));
528 memset(dB, 0, nbParam*
sizeof(
double));
529 memset(
dprtemp, 0, nbParam*
sizeof(
double));
531 for(
unsigned int it=0;it<
nbParam;it++){
532 for(
unsigned int jt=0;jt<
nbParam;jt++){
533 memset(d2u[it], 0, nbParam*
sizeof(
double));
534 memset(d2v[it], 0, nbParam*
sizeof(
double));
535 memset(dA[it], 0, nbParam*
sizeof(
double));
539 unsigned int Ncb_ = (
unsigned int)
Ncb;
541 for(
unsigned int t=0;t<Ncb_;t++)
544 if(
Pt[t]>seuilevitinf)
546 for(
unsigned int r=0;r<Ncb_;r++)
549 if(
Prt[r*Ncb_+t]>seuilevitinf)
551 for(
unsigned int it=0;it<
nbParam;it++){
557 u +=
Prt[r*Ncb_+t]*log(
Pt[t]*
Pr[r]);
559 v +=
Prt[r*Ncb_+t]*log(
Prt[r*Ncb_+t]);
561 for(
unsigned int it=0;it<
nbParam;it++){
568 for(
unsigned int it=0;it<
nbParam;it++){
569 for(
unsigned int jt=0;jt<
nbParam;jt++){
570 d2u[it][jt] +=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*log(
Pt[t]*
Pr[r])
572 d2v[it][jt] +=
d2Prt[(r*Ncb_+t)*nbParam*nbParam+it*nbParam+jt]*(1+log(
Prt[r*Ncb_+t]))
582 for(
unsigned int it=0;it<
nbParam;it++){
584 A[it] = du[it] * v - u * dv[it];
586 dB[it] = 2 * v * dv[it];
589 for(
unsigned int jt=0;jt<
nbParam;jt++){
591 dA[it][jt] = d2u[it][jt]*v-d2v[it][jt]*u;
593 Hessian[it][jt] = (dA[it][jt] * B -A[it] * dB[it])/(B*B);
601 for (
unsigned int i = 0; i <
nbParam; i++) {
617 double seuilevitinf=1e-200;
619 unsigned int Ncb_ = (
unsigned int)
Ncb;
621 for(
unsigned int t=0;t<Ncb_;t++)
624 if(
Pt[t]>seuilevitinf)
626 for(
unsigned int r=0;r<Ncb_;r++)
628 if(
Prt[r*Ncb_+t]>seuilevitinf)
631 for(
unsigned int it=0;it<
nbParam;it++)
634 dtemp=1.+log(
Prt[r*Ncb_+t]/
Pt[t]);
636 for(
unsigned int it=0;it<
nbParam;it++)
646 unsigned int Ncb_ = (
unsigned int)
Ncb;
647 unsigned int Nc_ = (
unsigned int)
Nc;
648 unsigned int influBspline_ = (
unsigned int)
influBspline;
650 memset(
Prt, 0, Ncb_*Ncb_*
sizeof(
double));
663 unsigned int tNcb = (
unsigned int)(nc+bspline_);
664 unsigned int tinfluBspline = (
unsigned int)(bspline_*bspline_);
665 unsigned int nc_ = (
unsigned int)nc;
666 double *tPrtD =
new double[nc_*nc_*tinfluBspline];
667 double *tPrt =
new double[tNcb*tNcb];
668 double *tPr =
new double[tNcb];
669 double *tPt =
new double[tNcb];
678 memset(tPrt, 0, tNcb*tNcb*
sizeof(
double));
679 memset(tPrtD, 0, nc_*nc_*tinfluBspline*
sizeof(
double));
682 Warp->computeCoeff(tp);
689 Warp->computeDenom(
X1,tp);
705 int cr=(int)((IW*(nc-1))/255.);
706 int ct=(int)((Tij*(nc-1))/255.);
707 double er=(IW*(nc-1))/255.-cr;
708 double et=((double)Tij*(nc-1))/255.-ct;
711 vpTemplateTrackerMIBSpline::PutPVBsplineD(tPrtD, cr, er, ct, et, nc, 1.,bspline_);
715 int tNcb_ = (int)tNcb;
716 int tinfluBspline_ = (int)tinfluBspline;
717 for(
int r=0;r<nc;r++)
718 for(
int t=0;t<nc;t++)
720 for(
int i=0;i<tinfluBspline_;i++)
725 tPrt[r2*tNcb_+t2]+=*pt;
741 for(
unsigned int r=0;r<tNcb;r++)
742 for(
unsigned int t=0;t<tNcb;t++)
744 tPrt[r*tNcb+t]=tPrt[r*tNcb+t]/Nbpoint;
746 memset(tPr, 0, tNcb*
sizeof(
double));
747 for(
unsigned int r=0;r<tNcb;r++)
749 for(
unsigned int t=0;t<tNcb;t++)
750 tPr[r]+=tPrt[r*tNcb+t];
754 memset(tPt, 0, (
size_t)(tNcb*
sizeof(
double)));
755 for(
unsigned int t=0;t<tNcb;t++)
757 for(
unsigned int r=0;r<tNcb;r++)
758 tPt[t]+=tPrt[r*tNcb+t];
760 for(
unsigned int r=0;r<tNcb;r++)
762 if(std::fabs(tPr[r]) > std::numeric_limits<double>::epsilon())
763 MI-=tPr[r]*log(tPr[r]);
765 for(
unsigned int t=0;t<tNcb;t++)
767 if(std::fabs(tPt[t]) > std::numeric_limits<double>::epsilon())
768 MI-=tPt[t]*log(tPt[t]);
770 for(
unsigned int r=0;r<tNcb;r++)
771 for(
unsigned int t=0;t<tNcb;t++)
773 if(std::fabs(tPrt[r*tNcb+t]) > std::numeric_limits<double>::epsilon())
774 MI+=tPrt[r*tNcb+t]*log(tPrt[r*tNcb+t]);
798 Warp->computeCoeff(tp);
805 Warp->computeDenom(
X1,tp);
819 IW=(
unsigned int)GaussI.
getValue(i2,j2);
829 "Cannot get MI; number of points = 0"));
831 Prt256=Prt256/Nbpoint;
837 for(
unsigned int t=0;t<256;t++)
839 for(
unsigned int r=0;r<256;r++)
842 if(std::fabs(Prt256[r][t]) > std::numeric_limits<double>::epsilon())
843 MI+=Prt256[r][t]*log(Prt256[r][t]);
846 if(std::fabs(Pt256[t]) > std::numeric_limits<double>::epsilon())
847 MI+=-Pt256[t]*log(Pt256[t]);
849 if(std::fabs(Pr256[t]) > std::numeric_limits<double>::epsilon())
850 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)
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
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
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)