Skip to content

Commit 710506e

Browse files
committed
calib3d: add a new overload for recoverPose
1 parent dcbed8d commit 710506e

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

modules/calib3d/include/opencv2/calib3d.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,28 @@ CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray point
14331433
double focal = 1.0, Point2d pp = Point2d(0, 0),
14341434
InputOutputArray mask = noArray() );
14351435

1436+
/** @overload
1437+
@param E The input essential matrix.
1438+
@param points1 Array of N 2D points from the first image. The point coordinates should be
1439+
floating-point (single or double precision).
1440+
@param points2 Array of the second image points of the same size and format as points1.
1441+
@param cameraMatrix Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ .
1442+
Note that this function assumes that points1 and points2 are feature points from cameras with the
1443+
same camera matrix.
1444+
@param R Recovered relative rotation.
1445+
@param t Recoverd relative translation.
1446+
@param distanceThresh threshold distance which is used to filter out far away points (i.e. infinite points).
1447+
@param mask Input/output mask for inliers in points1 and points2.
1448+
: If it is not empty, then it marks inliers in points1 and points2 for then given essential
1449+
matrix E. Only these inliers will be used to recover pose. In the output mask only inliers
1450+
which pass the cheirality check.
1451+
@param triangulatedPoints 3d points which were reconstructed by triangulation.
1452+
*/
1453+
1454+
CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2,
1455+
InputArray cameraMatrix, OutputArray R, OutputArray t, double distanceThresh, InputOutputArray mask = noArray(),
1456+
OutputArray triangulatedPoints = noArray());
1457+
14361458
/** @brief For points in an image of a stereo pair, computes the corresponding epilines in the other image.
14371459
14381460
@param points Input points. \f$N \times 1\f$ or \f$1 \times N\f$ matrix of type CV_32FC2 or

modules/calib3d/src/five-point.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,9 @@ cv::Mat cv::findEssentialMat( InputArray _points1, InputArray _points2, double f
458458
return cv::findEssentialMat(_points1, _points2, cameraMatrix, method, prob, threshold, _mask);
459459
}
460460

461-
int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, InputArray _cameraMatrix,
462-
OutputArray _R, OutputArray _t, InputOutputArray _mask)
461+
int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2,
462+
InputArray _cameraMatrix, OutputArray _R, OutputArray _t, double distanceThresh,
463+
InputOutputArray _mask, OutputArray triangulatedPoints)
463464
{
464465
CV_INSTRUMENT_REGION()
465466

@@ -506,51 +507,60 @@ int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, Inp
506507
// Notice here a threshold dist is used to filter
507508
// out far away points (i.e. infinite points) since
508509
// there depth may vary between postive and negtive.
509-
double dist = 50.0;
510+
std::vector<Mat> allTriangulations(4);
510511
Mat Q;
512+
511513
triangulatePoints(P0, P1, points1, points2, Q);
514+
if(triangulatedPoints.needed())
515+
Q.copyTo(allTriangulations[0]);
512516
Mat mask1 = Q.row(2).mul(Q.row(3)) > 0;
513517
Q.row(0) /= Q.row(3);
514518
Q.row(1) /= Q.row(3);
515519
Q.row(2) /= Q.row(3);
516520
Q.row(3) /= Q.row(3);
517-
mask1 = (Q.row(2) < dist) & mask1;
521+
mask1 = (Q.row(2) < distanceThresh) & mask1;
518522
Q = P1 * Q;
519523
mask1 = (Q.row(2) > 0) & mask1;
520-
mask1 = (Q.row(2) < dist) & mask1;
524+
mask1 = (Q.row(2) < distanceThresh) & mask1;
521525

522526
triangulatePoints(P0, P2, points1, points2, Q);
527+
if(triangulatedPoints.needed())
528+
Q.copyTo(allTriangulations[1]);
523529
Mat mask2 = Q.row(2).mul(Q.row(3)) > 0;
524530
Q.row(0) /= Q.row(3);
525531
Q.row(1) /= Q.row(3);
526532
Q.row(2) /= Q.row(3);
527533
Q.row(3) /= Q.row(3);
528-
mask2 = (Q.row(2) < dist) & mask2;
534+
mask2 = (Q.row(2) < distanceThresh) & mask2;
529535
Q = P2 * Q;
530536
mask2 = (Q.row(2) > 0) & mask2;
531-
mask2 = (Q.row(2) < dist) & mask2;
537+
mask2 = (Q.row(2) < distanceThresh) & mask2;
532538

533539
triangulatePoints(P0, P3, points1, points2, Q);
540+
if(triangulatedPoints.needed())
541+
Q.copyTo(allTriangulations[2]);
534542
Mat mask3 = Q.row(2).mul(Q.row(3)) > 0;
535543
Q.row(0) /= Q.row(3);
536544
Q.row(1) /= Q.row(3);
537545
Q.row(2) /= Q.row(3);
538546
Q.row(3) /= Q.row(3);
539-
mask3 = (Q.row(2) < dist) & mask3;
547+
mask3 = (Q.row(2) < distanceThresh) & mask3;
540548
Q = P3 * Q;
541549
mask3 = (Q.row(2) > 0) & mask3;
542-
mask3 = (Q.row(2) < dist) & mask3;
550+
mask3 = (Q.row(2) < distanceThresh) & mask3;
543551

544552
triangulatePoints(P0, P4, points1, points2, Q);
553+
if(triangulatedPoints.needed())
554+
Q.copyTo(allTriangulations[3]);
545555
Mat mask4 = Q.row(2).mul(Q.row(3)) > 0;
546556
Q.row(0) /= Q.row(3);
547557
Q.row(1) /= Q.row(3);
548558
Q.row(2) /= Q.row(3);
549559
Q.row(3) /= Q.row(3);
550-
mask4 = (Q.row(2) < dist) & mask4;
560+
mask4 = (Q.row(2) < distanceThresh) & mask4;
551561
Q = P4 * Q;
552562
mask4 = (Q.row(2) > 0) & mask4;
553-
mask4 = (Q.row(2) < dist) & mask4;
563+
mask4 = (Q.row(2) < distanceThresh) & mask4;
554564

555565
mask1 = mask1.t();
556566
mask2 = mask2.t();
@@ -583,20 +593,23 @@ int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, Inp
583593

584594
if (good1 >= good2 && good1 >= good3 && good1 >= good4)
585595
{
596+
if(triangulatedPoints.needed()) allTriangulations[0].copyTo(triangulatedPoints);
586597
R1.copyTo(_R);
587598
t.copyTo(_t);
588599
if (_mask.needed()) mask1.copyTo(_mask);
589600
return good1;
590601
}
591602
else if (good2 >= good1 && good2 >= good3 && good2 >= good4)
592603
{
604+
if(triangulatedPoints.needed()) allTriangulations[1].copyTo(triangulatedPoints);
593605
R2.copyTo(_R);
594606
t.copyTo(_t);
595607
if (_mask.needed()) mask2.copyTo(_mask);
596608
return good2;
597609
}
598610
else if (good3 >= good1 && good3 >= good2 && good3 >= good4)
599611
{
612+
if(triangulatedPoints.needed()) allTriangulations[2].copyTo(triangulatedPoints);
600613
t = -t;
601614
R1.copyTo(_R);
602615
t.copyTo(_t);
@@ -605,6 +618,7 @@ int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, Inp
605618
}
606619
else
607620
{
621+
if(triangulatedPoints.needed()) allTriangulations[3].copyTo(triangulatedPoints);
608622
t = -t;
609623
R2.copyTo(_R);
610624
t.copyTo(_t);
@@ -613,6 +627,12 @@ int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, Inp
613627
}
614628
}
615629

630+
int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, InputArray _cameraMatrix,
631+
OutputArray _R, OutputArray _t, InputOutputArray _mask)
632+
{
633+
return cv::recoverPose(E, _points1, _points2, _cameraMatrix, _R, _t, 50, _mask);
634+
}
635+
616636
int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, OutputArray _R,
617637
OutputArray _t, double focal, Point2d pp, InputOutputArray _mask)
618638
{

0 commit comments

Comments
 (0)