ba.cpp 36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                         License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2009, PhaseSpace Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's 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.
//
//   * The names of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// 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 Intel Corporation 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.
//
//M*/

#include "precomp.hpp"
#include "opencv2/calib3d/calib3d.hpp"
44
#include <iostream>
45

46
using namespace cv;
47

48 49 50 51 52 53
LevMarqSparse::LevMarqSparse() {
  Vis_index = X = prevP = P = deltaP = err = JtJ_diag = S = hX = NULL;
  U = ea = V = inv_V_star = eb = Yj = NULL;
  num_cams = 0,   num_points = 0,   num_err_param = 0;
  num_cam_param = 0,  num_point_param = 0;
  A = B = W = NULL;
54 55
}

56 57
LevMarqSparse::~LevMarqSparse() {
  clear();
58
}
59 60

LevMarqSparse::LevMarqSparse(int npoints, // number of points
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
           int ncameras, // number of cameras
           int nPointParams, // number of params per one point  (3 in case of 3D points)
           int nCameraParams, // number of parameters per one camera
           int nErrParams, // number of parameters in measurement vector
           // for 1 point at one camera (2 in case of 2D projections)
           Mat& visibility, // visibility matrix. rows correspond to points, columns correspond to cameras
           // 1 - point is visible for the camera, 0 - invisible
           Mat& P0, // starting vector of parameters, first cameras then points
           Mat& X_, // measurements, in order of visibility. non visible cases are skipped
           TermCriteria _criteria, // termination criteria

           // callback for estimation of Jacobian matrices
           void (CV_CDECL * _fjac)(int i, int j, Mat& point_params,
                Mat& cam_params, Mat& A, Mat& B, void* data),
           // callback for estimation of backprojection errors
           void (CV_CDECL * _func)(int i, int j, Mat& point_params,
                Mat& cam_params, Mat& estim, void* data),
           void* _data, // user-specific data passed to the callbacks
           BundleAdjustCallback _cb, void* _user_data
           ) {
81 82 83 84 85 86
  Vis_index = X = prevP = P = deltaP = err = JtJ_diag = S = hX = NULL;
  U = ea = V = inv_V_star = eb = Yj = NULL;
  A = B = W = NULL;

  cb = _cb;
  user_data = _user_data;
87

88
  run(npoints, ncameras, nPointParams, nCameraParams, nErrParams, visibility,
89
      P0, X_, _criteria, _fjac, _func, _data);
90 91
}

92 93 94 95 96 97
void LevMarqSparse::clear() {
  for( int i = 0; i < num_points; i++ ) {
    for(int j = 0; j < num_cams; j++ ) {
      //CvMat* tmp = ((CvMat**)(A->data.ptr + i * A->step))[j];
      CvMat* tmp = A[j+i*num_cams];
      if (tmp)
98
  cvReleaseMat( &tmp );
99 100 101 102

      //tmp = ((CvMat**)(B->data.ptr + i * B->step))[j];
      tmp  = B[j+i*num_cams];
      if (tmp)
103 104
  cvReleaseMat( &tmp );

105 106 107
      //tmp = ((CvMat**)(W->data.ptr + j * W->step))[i];
      tmp  = W[j+i*num_cams];
      if (tmp)
108
  cvReleaseMat( &tmp );
109
    }
110
  }
111 112 113 114 115 116 117 118 119 120 121 122 123 124
  delete A; //cvReleaseMat(&A);
  delete B;//cvReleaseMat(&B);
  delete W;//cvReleaseMat(&W);
  cvReleaseMat( &Vis_index);

  for( int j = 0; j < num_cams; j++ ) {
    cvReleaseMat( &U[j] );
  }
  delete U;

  for( int j = 0; j < num_cams; j++ ) {
    cvReleaseMat( &ea[j] );
  }
  delete ea;
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  //allocate V and inv_V_star
  for( int i = 0; i < num_points; i++ ) {
    cvReleaseMat(&V[i]);
    cvReleaseMat(&inv_V_star[i]);
  }
  delete V;
  delete inv_V_star;

  for( int i = 0; i < num_points; i++ ) {
    cvReleaseMat(&eb[i]);
  }
  delete eb;

  for( int i = 0; i < num_points; i++ ) {
    cvReleaseMat(&Yj[i]);
141
  }
142
  delete Yj;
143

144 145 146 147
  cvReleaseMat(&X);
  cvReleaseMat(&prevP);
  cvReleaseMat(&P);
  cvReleaseMat(&deltaP);
148

149 150
  cvReleaseMat(&err);

151 152 153
  cvReleaseMat(&JtJ_diag);
  cvReleaseMat(&S);
  cvReleaseMat(&hX);
154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

//A params correspond to  Cameras
//B params correspont to  Points

//num_cameras  - total number of cameras
//num_points   - total number of points

//num_par_per_camera - number of parameters per camera
//num_par_per_point - number of parameters per point

//num_errors - number of measurements.

void LevMarqSparse::run( int num_points_, //number of points
168 169 170 171 172 173 174 175 176 177 178 179 180
       int num_cams_, //number of cameras
       int num_point_param_, //number of params per one point  (3 in case of 3D points)
       int num_cam_param_, //number of parameters per one camera
       int num_err_param_, //number of parameters in measurement vector for 1 point at one camera (2 in case of 2D projections)
       Mat& visibility,   //visibility matrix . rows correspond to points, columns correspond to cameras
       // 0 - point is visible for the camera, 0 - invisible
       Mat& P0, //starting vector of parameters, first cameras then points
       Mat& X_init, //measurements, in order of visibility. non visible cases are skipped
       TermCriteria criteria_init,
       void (*fjac_)(int i, int j, Mat& point_params, Mat& cam_params, Mat& A, Mat& B, void* data),
       void (*func_)(int i, int j, Mat& point_params, Mat& cam_params, Mat& estim, void* data),
       void* data_
       ) { //termination criteria
181
  //clear();
182

183 184 185
  func = func_; //assign evaluation function
  fjac = fjac_; //assign jacobian
  data = data_;
186

187 188
  num_cams = num_cams_;
  num_points = num_points_;
189
  num_err_param = num_err_param_;
190 191
  num_cam_param = num_cam_param_;
  num_point_param = num_point_param_;
192

193 194 195
  //compute all sizes
  int Aij_width = num_cam_param;
  int Aij_height = num_err_param;
196

197 198
  int Bij_width = num_point_param;
  int Bij_height = num_err_param;
199

200 201
  int U_size = Aij_width;
  int V_size = Bij_width;
202

203 204
  int Wij_height = Aij_width;
  int Wij_width = Bij_width;
205

206
  //allocate memory for all Aij, Bij, U, V, W
207

208
  //allocate num_points*num_cams matrices A
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223
  //Allocate matrix A whose elements are nointers to Aij
  //if Aij is zero (point i is not visible in camera j) then A(i,j) contains NULL
  //A = cvCreateMat( num_points, num_cams, CV_32S /*pointer is stored here*/ );
  //B = cvCreateMat( num_points, num_cams, CV_32S /*pointer is stored here*/ );
  //W = cvCreateMat( num_cams, num_points, CV_32S /*pointer is stored here*/ );

  A = new CvMat* [num_points * num_cams];
  B = new CvMat* [num_points * num_cams];
  W = new CvMat* [num_cams * num_points];
  Vis_index = cvCreateMat( num_points, num_cams, CV_32S /*integer index is stored here*/ );
  //cvSetZero( A );
  //cvSetZero( B );
  //cvSetZero( W );
  cvSet( Vis_index, cvScalar(-1) );
224

225 226 227 228 229 230
  //fill matrices A and B based on visibility
  CvMat _vis = visibility;
  int index = 0;
  for (int i = 0; i < num_points; i++ ) {
    for (int j = 0; j < num_cams; j++ ) {
      if (((int*)(_vis.data.ptr+ i * _vis.step))[j] ) {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  ((int*)(Vis_index->data.ptr + i * Vis_index->step))[j] = index;
  index += num_err_param;

  //create matrices Aij, Bij
  CvMat* tmp = cvCreateMat(Aij_height, Aij_width, CV_64F );
  //((CvMat**)(A->data.ptr + i * A->step))[j] = tmp;
  cvSet(tmp,cvScalar(1.0,1.0,1.0,1.0));
  A[j+i*num_cams] = tmp;

  tmp = cvCreateMat( Bij_height, Bij_width, CV_64F );
  //((CvMat**)(B->data.ptr + i * B->step))[j] = tmp;
  cvSet(tmp,cvScalar(1.0,1.0,1.0,1.0));
  B[j+i*num_cams] = tmp;

  tmp = cvCreateMat( Wij_height, Wij_width, CV_64F );
  //((CvMat**)(W->data.ptr + j * W->step))[i] = tmp;  //note indices i and j swapped
  cvSet(tmp,cvScalar(1.0,1.0,1.0,1.0));
  W[j+i*num_cams] = tmp;
249
      } else{
250 251 252
  A[j+i*num_cams] = NULL;
  B[j+i*num_cams] = NULL;
  W[j+i*num_cams] = NULL;
253
      }
254
    }
255
  }
256

257 258 259 260 261 262 263 264 265 266 267 268 269
  //allocate U
  U = new CvMat* [num_cams];
  for (int j = 0; j < num_cams; j++ ) {
    U[j] = cvCreateMat( U_size, U_size, CV_64F );
    cvSetZero(U[j]);

  }
  //allocate ea
  ea = new CvMat* [num_cams];
  for (int j = 0; j < num_cams; j++ ) {
    ea[j] = cvCreateMat( U_size, 1, CV_64F );
    cvSetZero(ea[j]);
  }
270

271 272 273 274 275 276 277 278 279
  //allocate V and inv_V_star
  V = new CvMat* [num_points];
  inv_V_star = new CvMat* [num_points];
  for (int i = 0; i < num_points; i++ ) {
    V[i] = cvCreateMat( V_size, V_size, CV_64F );
    inv_V_star[i] = cvCreateMat( V_size, V_size, CV_64F );
    cvSetZero(V[i]);
    cvSetZero(inv_V_star[i]);
  }
280

281 282 283 284 285
  //allocate eb
  eb = new CvMat* [num_points];
  for (int i = 0; i < num_points; i++ ) {
    eb[i] = cvCreateMat( V_size, 1, CV_64F );
    cvSetZero(eb[i]);
286 287
  }

288 289 290 291 292
  //allocate Yj
  Yj = new CvMat* [num_points];
  for (int i = 0; i < num_points; i++ ) {
    Yj[i] = cvCreateMat( Wij_height, Wij_width, CV_64F );  //Yij has the same size as Wij
    cvSetZero(Yj[i]);
293 294
  }

295 296 297 298 299
  //allocate matrix S
  S = cvCreateMat( num_cams * num_cam_param, num_cams * num_cam_param, CV_64F);
  cvSetZero(S);
  JtJ_diag = cvCreateMat( num_cams * num_cam_param + num_points * num_point_param, 1, CV_64F );
  cvSetZero(JtJ_diag);
300

301
  //set starting parameters
302 303
  CvMat _tmp_ = CvMat(P0);
  prevP = cvCloneMat( &_tmp_ );
304 305
  P = cvCloneMat( &_tmp_ );
  deltaP = cvCloneMat( &_tmp_ );
306

307 308
  //set measurements
  _tmp_ = CvMat(X_init);
309
  X = cvCloneMat( &_tmp_ );
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  //create vector for estimated measurements
  hX = cvCreateMat( X->rows, X->cols, CV_64F );
  cvSetZero(hX);
  //create error vector
  err = cvCreateMat( X->rows, X->cols, CV_64F );
  cvSetZero(err);
  ask_for_proj(_vis);
  //compute initial error
  cvSub(X, hX, err );

  /*
    assert(X->rows == hX->rows);
    std::cerr<<"X size = "<<X->rows<<" "<<X->cols<<std::endl;
    std::cerr<<"hX size = "<<hX->rows<<" "<<hX->cols<<std::endl;
    for (int j=0;j<X->rows;j+=2) {
    double Xj1 = *(double*)(X->data.ptr + j * X->step);
    double hXj1 = *(double*)(hX->data.ptr + j * hX->step);
    double err1 = *(double*)(err->data.ptr + j * err->step);
    double Xj2 = *(double*)(X->data.ptr + (j+1) * X->step);
    double hXj2 = *(double*)(hX->data.ptr + (j+1) * hX->step);
    double err2 = *(double*)(err->data.ptr + (j+1) * err->step);
    std::cerr<<"("<<Xj1<<","<<Xj2<<") -> ("<<hXj1<<","<<hXj2<<"). err = ("<<err1<<","<<err2<<")"<<std::endl;
    }
  */

  prevErrNorm = cvNorm( err, 0,  CV_L2 );
  //    std::cerr<<"prevErrNorm = "<<prevErrNorm<<std::endl;
337
  iters = 0;
338
  criteria = criteria_init;
339

340 341 342 343 344
  optimize(_vis);

  ask_for_proj(_vis,true);
  cvSub(X, hX, err );
  errNorm = cvNorm( err, 0,  CV_L2 );
345 346
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
void LevMarqSparse::ask_for_proj(CvMat &/*_vis*/,bool once) {
    (void)once;
    //given parameter P, compute measurement hX
    int ind = 0;
    for (int i = 0; i < num_points; i++ ) {
        CvMat point_mat;
        cvGetSubRect( P, &point_mat, cvRect( 0, num_cams * num_cam_param + num_point_param * i, 1, num_point_param ));
        for (int j = 0; j < num_cams; j++ ) {
            //CvMat* Aij = ((CvMat**)(A->data.ptr + A->step * i))[j];
            CvMat* Aij = A[j+i*num_cams];
            if (Aij ) { //visible
                CvMat cam_mat;
                cvGetSubRect( P, &cam_mat, cvRect( 0, j * num_cam_param, 1, num_cam_param ));
                CvMat measur_mat;
                cvGetSubRect( hX, &measur_mat, cvRect( 0, ind * num_err_param, 1, num_err_param ));
                Mat _point_mat(&point_mat), _cam_mat(&cam_mat), _measur_mat(&measur_mat);
                func( i, j, _point_mat, _cam_mat, _measur_mat, data);
                assert( ind*num_err_param == ((int*)(Vis_index->data.ptr + i * Vis_index->step))[j]);
                ind+=1;
366 367
            }
        }
368
    }
369
}
370

371
//iteratively asks for Jacobians for every camera_point pair
372 373 374
void LevMarqSparse::ask_for_projac(CvMat &/*_vis*/)   //should be evaluated at point prevP
{
    // compute jacobians Aij and Bij
375
    for (int i = 0; i < num_points; i++ )
376 377 378 379 380 381
    {
        CvMat point_mat;
        cvGetSubRect( prevP, &point_mat, cvRect( 0, num_cams * num_cam_param + num_point_param * i, 1, num_point_param ));

        //CvMat** A_line = (CvMat**)(A->data.ptr + A->step * i);
        //CvMat** B_line = (CvMat**)(B->data.ptr + B->step * i);
382
        for( int j = 0; j < num_cams; j++ )
383 384 385 386 387
        {
            //CvMat* Aij = A_line[j];
            //if( Aij ) //Aij is not zero
            CvMat* Aij = A[j+i*num_cams];
            CvMat* Bij = B[j+i*num_cams];
388
            if(Aij)
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
            {
                //CvMat** A_line = (CvMat**)(A->data.ptr + A->step * i);
                //CvMat** B_line = (CvMat**)(B->data.ptr + B->step * i);

                //CvMat* Aij = A_line[j];
                //CvMat* Aij = ((CvMat**)(A->data.ptr + A->step * i))[j];

                CvMat cam_mat;
                cvGetSubRect( prevP, &cam_mat, cvRect( 0, j * num_cam_param, 1, num_cam_param ));

                //CvMat* Bij = B_line[j];
                //CvMat* Bij = ((CvMat**)(B->data.ptr + B->step * i))[j];
                Mat _point_mat(&point_mat), _cam_mat(&cam_mat), _Aij(Aij), _Bij(Bij);
                (*fjac)(i, j, _point_mat, _cam_mat, _Aij, _Bij, data);
            }
        }
405
    }
406
}
407

408 409
void LevMarqSparse::optimize(CvMat &_vis) { //main function that runs minimization
  bool done = false;
410 411 412

  CvMat* YWt = cvCreateMat( num_cam_param, num_cam_param, CV_64F ); //this matrix used to store Yij*Wik'
  CvMat* E = cvCreateMat( S->height, 1 , CV_64F ); //this is right part of system with S
413 414
  cvSetZero(YWt);
  cvSetZero(E);
415

416 417 418 419 420 421
  while(!done) {
    // compute jacobians Aij and Bij
    ask_for_projac(_vis);
    int invisible_count=0;
    //compute U_j  and  ea_j
    for (int j = 0; j < num_cams; j++ ) {
422
      cvSetZero(U[j]);
423 424 425
      cvSetZero(ea[j]);
      //summ by i (number of points)
      for (int i = 0; i < num_points; i++ ) {
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  //get Aij
  //CvMat* Aij = ((CvMat**)(A->data.ptr + A->step * i))[j];
  CvMat* Aij = A[j+i*num_cams];
  if (Aij ) {
    //Uj+= AijT*Aij
    cvGEMM( Aij, Aij, 1, U[j], 1, U[j], CV_GEMM_A_T );
    //ea_j += AijT * e_ij
    CvMat eij;

    int index = ((int*)(Vis_index->data.ptr + i * Vis_index->step))[j];

    cvGetSubRect( err, &eij, cvRect( 0, index, 1, Aij->height  ) ); //width of transposed Aij
    cvGEMM( Aij, &eij, 1, ea[j], 1, ea[j], CV_GEMM_A_T );
  }
  else
    invisible_count++;
442 443 444 445
      }
    } //U_j and ea_j computed for all j

    //    if (!(iters%100))
446 447 448 449 450
    {
      int nviz = X->rows / num_err_param;
      double e2 = prevErrNorm*prevErrNorm, e2n = e2 / nviz;
      std::cerr<<"Iteration: "<<iters<<", normError: "<<e2<<" ("<<e2n<<")"<<std::endl;
    }
451 452 453 454
    if (cb)
      cb(iters, prevErrNorm, user_data);
    //compute V_i  and  eb_i
    for (int i = 0; i < num_points; i++ ) {
455
      cvSetZero(V[i]);
456
      cvSetZero(eb[i]);
457

458 459
      //summ by i (number of points)
      for( int j = 0; j < num_cams; j++ ) {
460 461 462 463 464 465 466 467 468 469 470 471 472 473
  //get Bij
  //CvMat* Bij = ((CvMat**)(B->data.ptr + B->step * i))[j];
  CvMat* Bij = B[j+i*num_cams];
  if (Bij ) {
    //Vi+= BijT*Bij
    cvGEMM( Bij, Bij, 1, V[i], 1, V[i], CV_GEMM_A_T );

    //eb_i += BijT * e_ij
    int index = ((int*)(Vis_index->data.ptr + i * Vis_index->step))[j];

    CvMat eij;
    cvGetSubRect( err, &eij, cvRect( 0, index, 1, Bij->height  ) ); //width of transposed Bij
    cvGEMM( Bij, &eij, 1, eb[i], 1, eb[i], CV_GEMM_A_T );
  }
474 475 476 477 478 479
      }
    } //V_i and eb_i computed for all i

      //compute W_ij
    for( int i = 0; i < num_points; i++ ) {
      for( int j = 0; j < num_cams; j++ ) {
480 481 482 483 484 485 486 487 488 489 490
  //CvMat* Aij = ((CvMat**)(A->data.ptr + A->step * i))[j];
  CvMat* Aij = A[j+i*num_cams];
  if( Aij ) { //visible
    //CvMat* Bij = ((CvMat**)(B->data.ptr + B->step * i))[j];
    CvMat* Bij = B[j+i*num_cams];
    //CvMat* Wij = ((CvMat**)(W->data.ptr + W->step * j))[i];
    CvMat* Wij = W[j+i*num_cams];

    //multiply
    cvGEMM( Aij, Bij, 1, NULL, 0, Wij, CV_GEMM_A_T );
  }
491 492 493 494
      }
    } //Wij computed

      //backup diagonal of JtJ before we start augmenting it
495
    {
496 497 498
      CvMat dia;
      CvMat subr;
      for( int j = 0; j < num_cams; j++ ) {
499 500 501 502 503
  cvGetDiag(U[j], &dia);
  cvGetSubRect(JtJ_diag, &subr,
         cvRect(0, j*num_cam_param, 1, num_cam_param ));
  cvCopy( &dia, &subr );
      }
504
      for( int i = 0; i < num_points; i++ ) {
505 506 507 508 509 510
  cvGetDiag(V[i], &dia);
  cvGetSubRect(JtJ_diag, &subr,
         cvRect(0, num_cams*num_cam_param + i * num_point_param, 1, num_point_param ));
  cvCopy( &dia, &subr );
      }
    }
511 512 513 514 515

    if( iters == 0 ) {
      //initialize lambda. It is set to 1e-3 * average diagonal element in JtJ
      double average_diag = 0;
      for( int j = 0; j < num_cams; j++ ) {
516
  average_diag += cvTrace( U[j] ).val[0];
517 518
      }
      for( int i = 0; i < num_points; i++ ) {
519
  average_diag += cvTrace( V[i] ).val[0];
520 521
      }
      average_diag /= (num_cams*num_cam_param + num_points * num_point_param );
522 523 524

      //      lambda = 1e-3 * average_diag;
      lambda = 1e-3 * average_diag;
525 526
      lambda = 0.245560;
    }
527

528 529 530 531
    //now we are going to find good step and make it
    for(;;) {
      //augmentation of diagonal
      for(int j = 0; j < num_cams; j++ ) {
532 533
  CvMat diag;
  cvGetDiag( U[j], &diag );
534
#if 1
535
  cvAddS( &diag, cvScalar( lambda ), &diag );
536
#else
537
  cvScale( &diag, &diag, 1 + lambda );
538
#endif
539 540
      }
      for(int i = 0; i < num_points; i++ ) {
541 542
  CvMat diag;
  cvGetDiag( V[i], &diag );
543
#if 1
544
  cvAddS( &diag, cvScalar( lambda ), &diag );
545
#else
546
  cvScale( &diag, &diag, 1 + lambda );
547
#endif
548
      }
549 550 551 552
      bool error = false;
      //compute inv(V*)
      bool inverted_ok = true;
      for(int i = 0; i < num_points; i++ ) {
553
  double det = cvInvert( V[i], inv_V_star[i] );
554

555 556 557 558 559
  if( fabs(det) <= FLT_EPSILON )  {
    inverted_ok = false;
    std::cerr<<"V["<<i<<"] failed"<<std::endl;
    break;
  } //means we did wrong augmentation, try to choose different lambda
560 561 562
      }

      if( inverted_ok ) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  cvSetZero( E );
  //loop through cameras, compute upper diagonal blocks of matrix S
  for( int j = 0; j < num_cams; j++ ) {
    //compute Yij = Wij (V*_i)^-1  for all i   (if Wij exists/nonzero)
    for( int i = 0; i < num_points; i++ ) {
      //
      //CvMat* Wij = ((CvMat**)(W->data.ptr + W->step * j))[i];
      CvMat* Wij = W[j+i*num_cams];
      if( Wij ) {
        cvMatMul( Wij, inv_V_star[i], Yj[i] );
      }
    }

    //compute Sjk   for k>=j  (because Sjk = Skj)
    for( int k = j; k < num_cams; k++ ) {
      cvSetZero( YWt );
      for( int i = 0; i < num_points; i++ ) {
        //check that both Wij and Wik exist
        // CvMat* Wij = ((CvMat**)(W->data.ptr + W->step * j))[i];
        CvMat* Wij = W[j+i*num_cams];
        //CvMat* Wik = ((CvMat**)(W->data.ptr + W->step * k))[i];
        CvMat* Wik = W[k+i*num_cams];

        if( Wij && Wik ) {
    //multiply YWt += Yj[i]*Wik'
    cvGEMM( Yj[i], Wik, 1, YWt, 1, YWt, CV_GEMM_B_T  ); ///*transpose Wik
        }
      }

      //copy result to matrix S

      CvMat Sjk;
      //extract submat
      cvGetSubRect( S, &Sjk, cvRect( k * num_cam_param, j * num_cam_param, num_cam_param, num_cam_param ));


      //if j==k, add diagonal
      if( j != k ) {
        //just copy with minus
        cvScale( YWt, &Sjk, -1 ); //if we set initial S to zero then we can use cvSub( Sjk, YWt, Sjk);
      } else {
        //add diagonal value

        //subtract YWt from augmented Uj
        cvSub( U[j], YWt, &Sjk );
      }
    }

    //compute right part of equation involving matrix S
    // e_j=ea_j - \sum_i Y_ij eb_i
    {
      CvMat e_j;

      //select submat
      cvGetSubRect( E, &e_j, cvRect( 0, j * num_cam_param, 1, num_cam_param ) );

      for( int i = 0; i < num_points; i++ ) {
        //CvMat* Wij = ((CvMat**)(W->data.ptr + W->step * j))[i];
        CvMat* Wij = W[j+i*num_cams];
        if( Wij )
    cvMatMulAdd( Yj[i], eb[i], &e_j, &e_j );
      }

      cvSub( ea[j], &e_j, &e_j );
    }

  }
  //fill below diagonal elements of matrix S
  cvCompleteSymm( S,  0  ); ///*from upper to low //operation may be done by nonzero blocks or during upper diagonal computation

  //Solve linear system  S * deltaP_a = E
  CvMat dpa;
  cvGetSubRect( deltaP, &dpa, cvRect(0, 0, 1, S->width ) );
  int res = cvSolve( S, E, &dpa, CV_CHOLESKY );

  if( res ) { //system solved ok
    //compute db_i
    for( int i = 0; i < num_points; i++ ) {
      CvMat dbi;
      cvGetSubRect( deltaP, &dbi, cvRect( 0, dpa.height + i * num_point_param, 1, num_point_param ) );

      // compute \sum_j W_ij^T da_j
      for( int j = 0; j < num_cams; j++ ) {
        //get Wij
        //CvMat* Wij = ((CvMat**)(W->data.ptr + W->step * j))[i];
        CvMat* Wij = W[j+i*num_cams];
        if( Wij ) {
    //get da_j
    CvMat daj;
    cvGetSubRect( &dpa, &daj, cvRect( 0, j * num_cam_param, 1, num_cam_param ));
    cvGEMM( Wij, &daj, 1, &dbi, 1, &dbi, CV_GEMM_A_T  ); ///* transpose Wij
        }
      }
      //finalize dbi
      cvSub( eb[i], &dbi, &dbi );
      cvMatMul(inv_V_star[i], &dbi, &dbi );  //here we get final dbi
    }  //now we computed whole deltaP

    //add deltaP to delta
    cvAdd( prevP, deltaP, P );

    //evaluate  function with new parameters
    ask_for_proj(_vis); // func( P, hX );

    //compute error
    errNorm = cvNorm( X, hX, CV_L2 );

  } else {
    error = true;
  }
673
      } else {
674
  error = true;
675 676 677
      }
      //check solution
      if( error || ///* singularities somewhere
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    errNorm > prevErrNorm )  { //step was not accepted
  //increase lambda and reject change
  lambda *= 10;
  {
    int nviz = X->rows / num_err_param;
    double e2 = errNorm*errNorm, e2_prev = prevErrNorm*prevErrNorm;
    double e2n = e2/nviz, e2n_prev = e2_prev/nviz;
    std::cerr<<"move failed: lambda = "<<lambda<<", e2 = "<<e2<<" ("<<e2n<<") > "<<e2_prev<<" ("<<e2n_prev<<")"<<std::endl;
  }

  //restore diagonal from backup
  {
    CvMat dia;
    CvMat subr;
    for( int j = 0; j < num_cams; j++ ) {
      cvGetDiag(U[j], &dia);
      cvGetSubRect(JtJ_diag, &subr,
       cvRect(0, j*num_cam_param, 1, num_cam_param ));
      cvCopy( &subr, &dia );
    }
    for( int i = 0; i < num_points; i++ ) {
      cvGetDiag(V[i], &dia);
      cvGetSubRect(JtJ_diag, &subr,
       cvRect(0, num_cams*num_cam_param + i * num_point_param, 1, num_point_param ));
      cvCopy( &subr, &dia );
    }
  }
705
      } else {  //all is ok
706 707 708 709 710 711 712 713 714
  //accept change and decrease lambda
  lambda /= 10;
  lambda = MAX(lambda, 1e-16);
  std::cerr<<"decreasing lambda to "<<lambda<<std::endl;
  prevErrNorm = errNorm;

  //compute new projection error vector
  cvSub(  X, hX, err );
  break;
715
      }
716
    }
717 718 719 720
    iters++;

    double param_change_norm = cvNorm(P, prevP, CV_RELATIVE_L2);
    //check termination criteria
721 722
    if( (criteria.type&CV_TERMCRIT_ITER && iters > criteria.max_iter ) ||
  (criteria.type&CV_TERMCRIT_EPS && param_change_norm < criteria.epsilon) ) {
723 724 725 726 727 728 729
      //      std::cerr<<"relative norm change "<<param_change_norm<<" lower than eps "<<criteria.epsilon<<", stopping"<<std::endl;
      done = true;
      break;
    } else {
      //copy new params and continue iterations
      cvCopy( P, prevP );
    }
730 731
  }
  cvReleaseMat(&YWt);
732
  cvReleaseMat(&E);
733
}
734 735 736

//Utilities

737
static void fjac(int /*i*/, int /*j*/, CvMat *point_params, CvMat* cam_params, CvMat* A, CvMat* B, void* /*data*/) {
738 739
  //compute jacobian per camera parameters (i.e. Aij)
  //take i-th point 3D current coordinates
740

741 742
  CvMat _Mi;
  cvReshape(point_params, &_Mi, 3, 1 );
743

744
  CvMat* _mp = cvCreateMat(1, 1, CV_64FC2 ); //projection of the point
745

746 747 748 749
  //split camera params into different matrices
  CvMat _ri, _ti, _k;
  cvGetRows( cam_params, &_ri, 0, 3 );
  cvGetRows( cam_params, &_ti, 3, 6 );
750

751 752 753 754 755
  double intr_data[9] = {0, 0, 0, 0, 0, 0, 0, 0, 1};
  intr_data[0] = cam_params->data.db[6];
  intr_data[4] = cam_params->data.db[7];
  intr_data[2] = cam_params->data.db[8];
  intr_data[5] = cam_params->data.db[9];
756

757
  CvMat _A = cvMat(3,3, CV_64F, intr_data );
758

759
  CvMat _dpdr, _dpdt, _dpdf, _dpdc, _dpdk;
760

761
  bool have_dk = cam_params->height - 10 ? true : false;
762

763 764 765 766
  cvGetCols( A, &_dpdr, 0, 3 );
  cvGetCols( A, &_dpdt, 3, 6 );
  cvGetCols( A, &_dpdf, 6, 8 );
  cvGetCols( A, &_dpdc, 8, 10 );
767

768 769 770 771 772
  if( have_dk ) {
    cvGetRows( cam_params, &_k, 10, cam_params->height );
    cvGetCols( A, &_dpdk, 10, A->width );
  }
  cvProjectPoints2(&_Mi, &_ri, &_ti, &_A, have_dk ? &_k : NULL, _mp, &_dpdr, &_dpdt,
773
       &_dpdf, &_dpdc, have_dk ? &_dpdk : NULL, 0);
774

775
  cvReleaseMat( &_mp );
776

777 778
  //compute jacobian for point params
  //compute dMeasure/dPoint3D
779

780 781 782
  // x = (r11 * X + r12 * Y + r13 * Z + t1)
  // y = (r21 * X + r22 * Y + r23 * Z + t2)
  // z = (r31 * X + r32 * Y + r33 * Z + t3)
783

784 785
  // x' = x/z
  // y' = y/z
786

787
  //d(x') = ( dx*z - x*dz)/(z*z)
788
  //d(y') = ( dy*z - y*dz)/(z*z)
789

790 791
  //g = 1 + k1*r_2 + k2*r_4 + k3*r_6
  //r_2 = x'*x' + y'*y'
792

793
  //d(r_2) = 2*x'*dx' + 2*y'*dy'
794

795
  //dg = k1* d(r_2) + k2*2*r_2*d(r_2) + k3*3*r_2*r_2*d(r_2)
796

797 798
  //x" = x'*g + 2*p1*x'*y' + p2(r_2+2*x'_2)
  //y" = y'*g + p1(r_2+2*y'_2) + 2*p2*x'*y'
799

800
  //d(x") = d(x') * g + x' * d(g) + 2*p1*( d(x')*y' + x'*dy) + p2*(d(r_2) + 2*2*x'* dx')
801
  //d(y") = d(y') * g + y' * d(g) + 2*p2*( d(x')*y' + x'*dy) + p1*(d(r_2) + 2*2*y'* dy')
802

803 804
  // u = fx*( x") + cx
  // v = fy*( y") + cy
805

806 807
  // du = fx * d(x")  = fx * ( dx*z - x*dz)/ (z*z)
  // dv = fy * d(y")  = fy * ( dy*z - y*dz)/ (z*z)
808

809
  // dx/dX = r11,  dx/dY = r12, dx/dZ = r13
810
  // dy/dX = r21,  dy/dY = r22, dy/dZ = r23
811
  // dz/dX = r31,  dz/dY = r32, dz/dZ = r33
812

813 814 815
  // du/dX = fx*(r11*z-x*r31)/(z*z)
  // du/dY = fx*(r12*z-x*r32)/(z*z)
  // du/dZ = fx*(r13*z-x*r33)/(z*z)
816

817 818 819
  // dv/dX = fy*(r21*z-y*r31)/(z*z)
  // dv/dY = fy*(r22*z-y*r32)/(z*z)
  // dv/dZ = fy*(r23*z-y*r33)/(z*z)
820

821 822 823 824
  //get rotation matrix
  double R[9], t[3], fx = intr_data[0], fy = intr_data[4];
  CvMat _R = cvMat( 3, 3, CV_64F, R );
  cvRodrigues2(&_ri, &_R);
825

826 827 828 829
  double X,Y,Z;
  X = point_params->data.db[0];
  Y = point_params->data.db[1];
  Z = point_params->data.db[2];
830

831 832 833
  t[0] = _ti.data.db[0];
  t[1] = _ti.data.db[1];
  t[2] = _ti.data.db[2];
834

835 836 837 838
  //compute x,y,z
  double x = R[0] * X + R[1] * Y + R[2] * Z + t[0];
  double y = R[3] * X + R[4] * Y + R[5] * Z + t[1];
  double z = R[6] * X + R[7] * Y + R[8] * Z + t[2];
839

840
#if 1
841 842
  //compute x',y'
  double x_strike = x/z;
843
  double y_strike = y/z;
844 845
  //compute dx',dy'  matrix
  //
846
  //    dx'/dX  dx'/dY dx'/dZ    =
847 848 849
  //    dy'/dX  dy'/dY dy'/dZ

  double coeff[6] = { z, 0, -x,
850
          0, z, -y };
851 852 853 854
  CvMat coeffmat = cvMat( 2, 3, CV_64F, coeff );

  CvMat* dstrike_dbig = cvCreateMat(2,3,CV_64F);
  cvMatMul(&coeffmat, &_R, dstrike_dbig);
855 856
  cvScale(dstrike_dbig, dstrike_dbig, 1/(z*z) );

857 858
  if( have_dk ) {
    double strike_[2] = {x_strike, y_strike};
859 860
    CvMat strike = cvMat(1, 2, CV_64F, strike_);

861 862 863 864 865 866 867 868 869 870 871 872 873
    //compute r_2
    double r_2 = x_strike*x_strike + y_strike*y_strike;
    double r_4 = r_2*r_2;
    double r_6 = r_4*r_2;

    //compute d(r_2)/dbig
    CvMat* dr2_dbig = cvCreateMat(1,3,CV_64F);
    cvMatMul( &strike, dstrike_dbig, dr2_dbig);
    cvScale( dr2_dbig, dr2_dbig, 2 );

    double& k1 = _k.data.db[0];
    double& k2 = _k.data.db[1];
    double& p1 = _k.data.db[2];
874
    double& p2 = _k.data.db[3];
875 876 877 878
    double k3 = 0;

    if( _k.cols*_k.rows == 5 ) {
      k3 = _k.data.db[4];
879
    }
880 881 882 883 884
    //compute dg/dbig
    double dg_dr2 = k1 + k2*2*r_2 + k3*3*r_4;
    double g = 1+k1*r_2+k2*r_4+k3*r_6;

    CvMat* dg_dbig = cvCreateMat(1,3,CV_64F);
885
    cvScale( dr2_dbig, dg_dbig, dg_dr2 );
886 887 888

    CvMat* tmp = cvCreateMat( 2, 3, CV_64F );
    CvMat* dstrike2_dbig = cvCreateMat( 2, 3, CV_64F );
889

890
    double c[4] = { g+2*p1*y_strike+4*p2*x_strike,       2*p1*x_strike,
891
        2*p2*y_strike,                 g+2*p2*x_strike + 4*p1*y_strike };
892

893
    CvMat coeffmat2 = cvMat(2,2,CV_64F, c );
894

895
    cvMatMul(&coeffmat2, dstrike_dbig, dstrike2_dbig );
896

897 898
    cvGEMM( &strike, dg_dbig, 1, NULL, 0, tmp, CV_GEMM_A_T );
    cvAdd( dstrike2_dbig, tmp, dstrike2_dbig );
899

900 901
    double p[2] = { p2, p1 };
    CvMat pmat = cvMat(2, 1, CV_64F, p );
902

903
    cvMatMul( &pmat, dr2_dbig ,tmp);
904
    cvAdd( dstrike2_dbig, tmp, dstrike2_dbig );
905

906
    cvCopy( dstrike2_dbig, B );
907

908 909
    cvReleaseMat(&dr2_dbig);
    cvReleaseMat(&dg_dbig);
910

911 912
    cvReleaseMat(&tmp);
    cvReleaseMat(&dstrike2_dbig);
913
    cvReleaseMat(&tmp);
914 915 916 917 918 919
  } else {
    cvCopy(dstrike_dbig, B);
  }
  //multiply by fx, fy
  CvMat row;
  cvGetRows( B, &row, 0, 1 );
920 921
  cvScale( &row, &row, fx );

922 923
  cvGetRows( B, &row, 1, 2 );
  cvScale( &row, &row, fy );
924 925 926

#else

927
  double k = fx/(z*z);
928

929 930 931
  cvmSet( B, 0, 0, k*(R[0]*z-x*R[6]));
  cvmSet( B, 0, 1, k*(R[1]*z-x*R[7]));
  cvmSet( B, 0, 2, k*(R[2]*z-x*R[8]));
932 933 934

  k = fy/(z*z);

935 936 937
  cvmSet( B, 1, 0, k*(R[3]*z-y*R[6]));
  cvmSet( B, 1, 1, k*(R[4]*z-y*R[7]));
  cvmSet( B, 1, 2, k*(R[5]*z-y*R[8]));
938

939
#endif
940

941
};
942
static void func(int /*i*/, int /*j*/, CvMat *point_params, CvMat* cam_params, CvMat* estim, void* /*data*/) {
943 944 945
  //just do projections
  CvMat _Mi;
  cvReshape( point_params, &_Mi, 3, 1 );
946

947 948
  CvMat* _mp = cvCreateMat(1, 1, CV_64FC2 ); //projection of the point
  CvMat* _mp2 = cvCreateMat(1, 2, CV_64F ); //projection of the point
949

950 951
  //split camera params into different matrices
  CvMat _ri, _ti, _k;
952

953 954
  cvGetRows( cam_params, &_ri, 0, 3 );
  cvGetRows( cam_params, &_ti, 3, 6 );
955

956 957 958 959 960
  double intr_data[9] = {0, 0, 0, 0, 0, 0, 0, 0, 1};
  intr_data[0] = cam_params->data.db[6];
  intr_data[4] = cam_params->data.db[7];
  intr_data[2] = cam_params->data.db[8];
  intr_data[5] = cam_params->data.db[9];
961

962
  CvMat _A = cvMat(3,3, CV_64F, intr_data );
963

964
  //int cn = CV_MAT_CN(_Mi.type);
965

966
  bool have_dk = cam_params->height - 10 ? true : false;
967

968
  if( have_dk ) {
969 970
    cvGetRows( cam_params, &_k, 10, cam_params->height );
  }
971
  cvProjectPoints2( &_Mi, &_ri, &_ti, &_A, have_dk ? &_k : NULL, _mp, NULL, NULL,
972
        NULL, NULL, NULL, 0);
973
  //    std::cerr<<"_mp = "<<_mp->data.db[0]<<","<<_mp->data.db[1]<<std::endl;
974
  //
975 976 977 978 979
  _mp2->data.db[0] = _mp->data.db[0];
  _mp2->data.db[1] = _mp->data.db[1];
  cvTranspose( _mp2, estim );
  cvReleaseMat( &_mp );
  cvReleaseMat( &_mp2 );
980 981
};

982
static void fjac_new(int i, int j, Mat& point_params, Mat& cam_params, Mat& A, Mat& B, void* data) {
Andrey Kamaev's avatar
Andrey Kamaev committed
983 984
  CvMat _point_params = point_params, _cam_params = cam_params, _Al = A, _Bl = B;
  fjac(i,j, &_point_params, &_cam_params, &_Al, &_Bl, data);
985 986
};

987
static void func_new(int i, int j, Mat& point_params, Mat& cam_params, Mat& estim, void* data)  {
988 989
  CvMat _point_params = point_params, _cam_params = cam_params, _estim = estim;
  func(i,j,&_point_params,&_cam_params,&_estim,data);
990
};
991 992

void LevMarqSparse::bundleAdjust( vector<Point3d>& points, //positions of points in global coordinate system (input and output)
993 994 995 996 997 998 999 1000
          const vector<vector<Point2d> >& imagePoints, //projections of 3d points for every camera
          const vector<vector<int> >& visibility, //visibility of 3d points for every camera
          vector<Mat>& cameraMatrix, //intrinsic matrices of all cameras (input and output)
          vector<Mat>& R, //rotation matrices of all cameras (input and output)
          vector<Mat>& T, //translation vector of all cameras (input and output)
          vector<Mat>& distCoeffs, //distortion coefficients of all cameras (input and output)
          const TermCriteria& criteria,
          BundleAdjustCallback cb, void* user_data) {
1001
  //,enum{MOTION_AND_STRUCTURE,MOTION,STRUCTURE})
1002 1003
  int num_points = (int)points.size();
  int num_cameras = (int)cameraMatrix.size();
1004

1005 1006 1007 1008 1009
  CV_Assert( imagePoints.size() == (size_t)num_cameras &&
       visibility.size() == (size_t)num_cameras &&
       R.size() == (size_t)num_cameras &&
       T.size() == (size_t)num_cameras &&
       (distCoeffs.size() == (size_t)num_cameras || distCoeffs.size() == 0) );
1010 1011 1012 1013

  int numdist = distCoeffs.size() ? (distCoeffs[0].rows * distCoeffs[0].cols) : 0;

  int num_cam_param = 3 /* rotation vector */ + 3 /* translation vector */
1014
    + 2 /* fx, fy */ + 2 /* cx, cy */ + numdist;
1015

1016
  int num_point_param = 3;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  //collect camera parameters into vector
  Mat params( num_cameras * num_cam_param + num_points * num_point_param, 1, CV_64F );

  //fill camera params
  for( int i = 0; i < num_cameras; i++ ) {
    //rotation
    Mat rot_vec; Rodrigues( R[i], rot_vec );
    Mat dst = params.rowRange(i*num_cam_param, i*num_cam_param+3);
    rot_vec.copyTo(dst);

    //translation
    dst = params.rowRange(i*num_cam_param + 3, i*num_cam_param+6);
1030 1031
    T[i].copyTo(dst);

1032 1033 1034 1035 1036 1037 1038 1039
    //intrinsic camera matrix
    double* intr_data = (double*)cameraMatrix[i].data;
    double* intr = (double*)(params.data + params.step * (i*num_cam_param+6));
    //focals
    intr[0] = intr_data[0];  //fx
    intr[1] = intr_data[4];  //fy
    //center of projection
    intr[2] = intr_data[2];  //cx
1040
    intr[3] = intr_data[5];  //cy
1041 1042 1043 1044

    //add distortion if exists
    if( distCoeffs.size() ) {
      dst = params.rowRange(i*num_cam_param + 10, i*num_cam_param+10+numdist);
1045
      distCoeffs[i].copyTo(dst);
1046
    }
1047
  }
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

  //fill point params
  Mat ptparams(num_points, 1, CV_64FC3, params.data + num_cameras*num_cam_param*params.step);
  Mat _points(points);
  CV_Assert(_points.size() == ptparams.size() && _points.type() == ptparams.type());
  _points.copyTo(ptparams);

  //convert visibility vectors to visibility matrix
  Mat vismat(num_points, num_cameras, CV_32S);
  for( int i = 0; i < num_cameras; i++ ) {
    //get row
    Mat col = vismat.col(i);
    Mat((int)visibility[i].size(), 1, vismat.type(), (void*)&visibility[i][0]).copyTo( col );
  }

  int num_proj = countNonZero(vismat); //total number of points projections

  //collect measurements
1066 1067
  Mat X(num_proj*2,1,CV_64F); //measurement vector

1068 1069 1070 1071 1072
  int counter = 0;
  for(int i = 0; i < num_points; i++ ) {
    for(int j = 0; j < num_cameras; j++ ) {
      //check visibility
      if( visibility[j][i] ) {
1073 1074 1075 1076 1077 1078 1079 1080
  //extract point and put tu vector
  Point2d p = imagePoints[j][i];
  ((double*)(X.data))[counter] = p.x;
  ((double*)(X.data))[counter+1] = p.y;
  assert(p.x != -1 || p.y != -1);
  counter+=2;
      }
    }
1081 1082 1083
  }

  LevMarqSparse levmar( num_points, num_cameras, num_point_param, num_cam_param, 2, vismat, params, X,
1084 1085
      TermCriteria(criteria), fjac_new, func_new, NULL,
      cb, user_data);
1086 1087 1088 1089
  //extract results
  //fill point params
  /*Mat final_points(num_points, 1, CV_64FC3,
    levmar.P->data.db + num_cameras*num_cam_param *levmar.P->step);
1090
    CV_Assert(_points.size() == final_points.size() && _points.type() == final_points.type());
1091 1092 1093 1094 1095 1096 1097
    final_points.copyTo(_points);*/

  points.clear();
  for( int i = 0; i < num_points; i++ ) {
    CvMat point_mat;
    cvGetSubRect( levmar.P, &point_mat, cvRect( 0, levmar.num_cams * levmar.num_cam_param+ levmar.num_point_param * i, 1, levmar.num_point_param ));
    CvScalar x = cvGet2D(&point_mat,0,0); CvScalar y = cvGet2D(&point_mat,1,0); CvScalar z = cvGet2D(&point_mat,2,0);
1098
    points.push_back(Point3d(x.val[0],y.val[0],z.val[0]));
1099 1100 1101 1102 1103 1104 1105 1106 1107
    //std::cerr<<"point"<<points[points.size()-1].x<<","<<points[points.size()-1].y<<","<<points[points.size()-1].z<<std::endl;
  }
  //fill camera params
  //R.clear();T.clear();cameraMatrix.clear();
  for( int i = 0; i < num_cameras; i++ ) {
    //rotation
    Mat rot_vec = Mat(levmar.P).rowRange(i*num_cam_param, i*num_cam_param+3);
    Rodrigues( rot_vec, R[i] );
    //translation
1108
    T[i] = Mat(levmar.P).rowRange(i*num_cam_param + 3, i*num_cam_param+6);
1109 1110 1111 1112 1113 1114 1115 1116 1117

    //intrinsic camera matrix
    double* intr_data = (double*)cameraMatrix[i].data;
    double* intr = (double*)(Mat(levmar.P).data + Mat(levmar.P).step * (i*num_cam_param+6));
    //focals
    intr_data[0] = intr[0];  //fx
    intr_data[4] = intr[1];  //fy
    //center of projection
    intr_data[2] = intr[2];  //cx
1118
    intr_data[5] = intr[3];  //cy
1119 1120 1121 1122 1123

    //add distortion if exists
    if( distCoeffs.size() ) {
      Mat(levmar.P).rowRange(i*num_cam_param + 10, i*num_cam_param+10+numdist).copyTo(distCoeffs[i]);
    }
1124 1125
  }
}