@@ -1629,7 +1629,12 @@ static double cvCalibrateCamera2Internal( const CvMat* objectPoints,
1629
1629
JtErr.rowRange (NINTRINSIC + i * 6 , NINTRINSIC + (i + 1 ) * 6 ) = _Je.t () * _err;
1630
1630
}
1631
1631
1632
- reprojErr += norm (_err, NORM_L2SQR);
1632
+ double viewErr = norm (_err, NORM_L2SQR);
1633
+
1634
+ if ( perViewErrors )
1635
+ perViewErrors->data .db [i] = std::sqrt (viewErr / ni);
1636
+
1637
+ reprojErr += viewErr;
1633
1638
}
1634
1639
if ( _errNorm )
1635
1640
*_errNorm = reprojErr;
@@ -1671,13 +1676,6 @@ static double cvCalibrateCamera2Internal( const CvMat* objectPoints,
1671
1676
for ( i = 0 , pos = 0 ; i < nimages; i++ )
1672
1677
{
1673
1678
CvMat src, dst;
1674
- if ( perViewErrors )
1675
- {
1676
- ni = npoints->data .i [i*npstep];
1677
- perViewErrors->data .db [i] = std::sqrt (cv::norm (allErrors.colRange (pos, pos + ni),
1678
- NORM_L2SQR) / ni);
1679
- pos+=ni;
1680
- }
1681
1679
1682
1680
if ( rvecs )
1683
1681
{
@@ -1757,14 +1755,13 @@ static int dbCmp( const void* _a, const void* _b )
1757
1755
return (a > b) - (a < b);
1758
1756
}
1759
1757
1760
-
1761
- double cvStereoCalibrate ( const CvMat* _objectPoints, const CvMat* _imagePoints1,
1758
+ static double cvStereoCalibrateImpl ( const CvMat* _objectPoints, const CvMat* _imagePoints1,
1762
1759
const CvMat* _imagePoints2, const CvMat* _npoints,
1763
1760
CvMat* _cameraMatrix1, CvMat* _distCoeffs1,
1764
1761
CvMat* _cameraMatrix2, CvMat* _distCoeffs2,
1765
1762
CvSize imageSize, CvMat* matR, CvMat* matT,
1766
1763
CvMat* matE, CvMat* matF,
1767
- int flags,
1764
+ CvMat* perViewErr, int flags,
1768
1765
CvTermCriteria termCrit )
1769
1766
{
1770
1767
const int NINTRINSIC = 18 ;
@@ -1866,9 +1863,6 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1
1866
1863
// for intrinisic parameters of each camera ((fx,fy,cx,cy,k1,k2,p1,p2) ~ 8 parameters).
1867
1864
nparams = 6 *(nimages+1 ) + (recomputeIntrinsics ? NINTRINSIC*2 : 0 );
1868
1865
1869
- // storage for initial [om(R){i}|t{i}] (in order to compute the median for each component)
1870
- RT0.reset (cvCreateMat ( 6 , nimages, CV_64F ));
1871
-
1872
1866
CvLevMarq solver ( nparams, 0 , termCrit );
1873
1867
1874
1868
if (flags & CALIB_USE_LU) {
@@ -1918,6 +1912,8 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1
1918
1912
}
1919
1913
}
1920
1914
1915
+ // storage for initial [om(R){i}|t{i}] (in order to compute the median for each component)
1916
+ RT0.reset (cvCreateMat ( 6 , nimages, CV_64F ));
1921
1917
/*
1922
1918
Compute initial estimate of pose
1923
1919
For each image, compute:
@@ -1968,12 +1964,32 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1
1968
1964
RT0->data .db [i + nimages*5 ] = t[1 ][2 ];
1969
1965
}
1970
1966
1971
- // find the medians and save the first 6 parameters
1972
- for ( i = 0 ; i < 6 ; i++ )
1967
+ if (flags & CALIB_USE_EXTRINSIC_GUESS)
1973
1968
{
1974
- qsort ( RT0->data .db + i*nimages, nimages, CV_ELEM_SIZE (RT0->type ), dbCmp );
1975
- solver.param ->data .db [i] = nimages % 2 != 0 ? RT0->data .db [i*nimages + nimages/2 ] :
1976
- (RT0->data .db [i*nimages + nimages/2 - 1 ] + RT0->data .db [i*nimages + nimages/2 ])*0.5 ;
1969
+ Vec3d R, T;
1970
+ cvarrToMat (matT).convertTo (T, CV_64F);
1971
+
1972
+ if ( matR->rows == 3 && matR->cols == 3 )
1973
+ Rodrigues (cvarrToMat (matR), R);
1974
+ else
1975
+ cvarrToMat (matR).convertTo (R, CV_64F);
1976
+
1977
+ solver.param ->data .db [0 ] = R[0 ];
1978
+ solver.param ->data .db [1 ] = R[1 ];
1979
+ solver.param ->data .db [2 ] = R[2 ];
1980
+ solver.param ->data .db [3 ] = T[0 ];
1981
+ solver.param ->data .db [4 ] = T[1 ];
1982
+ solver.param ->data .db [5 ] = T[2 ];
1983
+ }
1984
+ else
1985
+ {
1986
+ // find the medians and save the first 6 parameters
1987
+ for ( i = 0 ; i < 6 ; i++ )
1988
+ {
1989
+ qsort ( RT0->data .db + i*nimages, nimages, CV_ELEM_SIZE (RT0->type ), dbCmp );
1990
+ solver.param ->data .db [i] = nimages % 2 != 0 ? RT0->data .db [i*nimages + nimages/2 ] :
1991
+ (RT0->data .db [i*nimages + nimages/2 - 1 ] + RT0->data .db [i*nimages + nimages/2 ])*0.5 ;
1992
+ }
1977
1993
}
1978
1994
1979
1995
if ( recomputeIntrinsics )
@@ -2151,7 +2167,12 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1
2151
2167
}
2152
2168
}
2153
2169
2154
- reprojErr += norm (err, NORM_L2SQR);
2170
+ double viewErr = norm (err, NORM_L2SQR);
2171
+
2172
+ if (perViewErr)
2173
+ perViewErr->data .db [i*2 + k] = std::sqrt (viewErr/ni);
2174
+
2175
+ reprojErr += viewErr;
2155
2176
}
2156
2177
}
2157
2178
if (_errNorm)
@@ -2209,7 +2230,19 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1
2209
2230
2210
2231
return std::sqrt (reprojErr/(pointsTotal*2 ));
2211
2232
}
2212
-
2233
+ double cvStereoCalibrate ( const CvMat* _objectPoints, const CvMat* _imagePoints1,
2234
+ const CvMat* _imagePoints2, const CvMat* _npoints,
2235
+ CvMat* _cameraMatrix1, CvMat* _distCoeffs1,
2236
+ CvMat* _cameraMatrix2, CvMat* _distCoeffs2,
2237
+ CvSize imageSize, CvMat* matR, CvMat* matT,
2238
+ CvMat* matE, CvMat* matF,
2239
+ int flags,
2240
+ CvTermCriteria termCrit )
2241
+ {
2242
+ return cvStereoCalibrateImpl (_objectPoints, _imagePoints1, _imagePoints2, _npoints, _cameraMatrix1,
2243
+ _distCoeffs1, _cameraMatrix2, _distCoeffs2, imageSize, matR, matT, matE,
2244
+ matF, NULL , flags, termCrit);
2245
+ }
2213
2246
2214
2247
static void
2215
2248
icvGetRectangles ( const CvMat* cameraMatrix, const CvMat* distCoeffs,
@@ -3474,7 +3507,26 @@ double cv::stereoCalibrate( InputArrayOfArrays _objectPoints,
3474
3507
InputOutputArray _cameraMatrix1, InputOutputArray _distCoeffs1,
3475
3508
InputOutputArray _cameraMatrix2, InputOutputArray _distCoeffs2,
3476
3509
Size imageSize, OutputArray _Rmat, OutputArray _Tmat,
3477
- OutputArray _Emat, OutputArray _Fmat, int flags ,
3510
+ OutputArray _Emat, OutputArray _Fmat, int flags,
3511
+ TermCriteria criteria)
3512
+ {
3513
+ Mat Rmat, Tmat;
3514
+ double ret = stereoCalibrate (_objectPoints, _imagePoints1, _imagePoints2, _cameraMatrix1, _distCoeffs1,
3515
+ _cameraMatrix2, _distCoeffs2, imageSize, Rmat, Tmat, _Emat, _Fmat,
3516
+ noArray (), flags, criteria);
3517
+ Rmat.copyTo (_Rmat);
3518
+ Tmat.copyTo (_Tmat);
3519
+ return ret;
3520
+ }
3521
+
3522
+ double cv::stereoCalibrate ( InputArrayOfArrays _objectPoints,
3523
+ InputArrayOfArrays _imagePoints1,
3524
+ InputArrayOfArrays _imagePoints2,
3525
+ InputOutputArray _cameraMatrix1, InputOutputArray _distCoeffs1,
3526
+ InputOutputArray _cameraMatrix2, InputOutputArray _distCoeffs2,
3527
+ Size imageSize, InputOutputArray _Rmat, InputOutputArray _Tmat,
3528
+ OutputArray _Emat, OutputArray _Fmat,
3529
+ OutputArray _perViewErrors, int flags ,
3478
3530
TermCriteria criteria)
3479
3531
{
3480
3532
int rtype = CV_64F;
@@ -3495,8 +3547,11 @@ double cv::stereoCalibrate( InputArrayOfArrays _objectPoints,
3495
3547
distCoeffs2 = distCoeffs2.rows == 1 ? distCoeffs2.colRange (0 , 5 ) : distCoeffs2.rowRange (0 , 5 );
3496
3548
}
3497
3549
3498
- _Rmat.create (3 , 3 , rtype);
3499
- _Tmat.create (3 , 1 , rtype);
3550
+ if ((flags & CALIB_USE_EXTRINSIC_GUESS) == 0 )
3551
+ {
3552
+ _Rmat.create (3 , 3 , rtype);
3553
+ _Tmat.create (3 , 1 , rtype);
3554
+ }
3500
3555
3501
3556
Mat objPt, imgPt, imgPt2, npoints;
3502
3557
@@ -3505,22 +3560,32 @@ double cv::stereoCalibrate( InputArrayOfArrays _objectPoints,
3505
3560
CvMat c_objPt = objPt, c_imgPt = imgPt, c_imgPt2 = imgPt2, c_npoints = npoints;
3506
3561
CvMat c_cameraMatrix1 = cameraMatrix1, c_distCoeffs1 = distCoeffs1;
3507
3562
CvMat c_cameraMatrix2 = cameraMatrix2, c_distCoeffs2 = distCoeffs2;
3508
- CvMat c_matR = _Rmat.getMat (), c_matT = _Tmat.getMat (), c_matE, c_matF, *p_matE = 0 , *p_matF = 0 ;
3563
+ CvMat c_matR = _Rmat.getMat (), c_matT = _Tmat.getMat (), c_matE, c_matF, c_matErr;
3564
+
3565
+ bool E_needed = _Emat.needed (), F_needed = _Fmat.needed (), errors_needed = _perViewErrors.needed ();
3509
3566
3510
- if ( _Emat. needed () )
3567
+ if ( E_needed )
3511
3568
{
3512
3569
_Emat.create (3 , 3 , rtype);
3513
- p_matE = &( c_matE = _Emat.getMat () );
3570
+ c_matE = _Emat.getMat ();
3514
3571
}
3515
- if ( _Fmat. needed () )
3572
+ if ( F_needed )
3516
3573
{
3517
3574
_Fmat.create (3 , 3 , rtype);
3518
- p_matF = &(c_matF = _Fmat.getMat ());
3575
+ c_matF = _Fmat.getMat ();
3576
+ }
3577
+
3578
+ if ( errors_needed )
3579
+ {
3580
+ int nimages = int (_objectPoints.total ());
3581
+ _perViewErrors.create (nimages, 2 , CV_64F);
3582
+ c_matErr = _perViewErrors.getMat ();
3519
3583
}
3520
3584
3521
- double err = cvStereoCalibrate (&c_objPt, &c_imgPt, &c_imgPt2, &c_npoints, &c_cameraMatrix1,
3522
- &c_distCoeffs1, &c_cameraMatrix2, &c_distCoeffs2, imageSize,
3523
- &c_matR, &c_matT, p_matE, p_matF, flags, criteria );
3585
+ double err = cvStereoCalibrateImpl (&c_objPt, &c_imgPt, &c_imgPt2, &c_npoints, &c_cameraMatrix1,
3586
+ &c_distCoeffs1, &c_cameraMatrix2, &c_distCoeffs2, imageSize, &c_matR,
3587
+ &c_matT, E_needed ? &c_matE : NULL , F_needed ? &c_matF : NULL ,
3588
+ errors_needed ? &c_matErr : NULL , flags, criteria);
3524
3589
3525
3590
cameraMatrix1.copyTo (_cameraMatrix1);
3526
3591
cameraMatrix2.copyTo (_cameraMatrix2);
0 commit comments