@@ -238,7 +238,7 @@ bool FindFundamentalMatrixWithRANSAC(const vector<pair<Feature, Feature>>& match
238
238
}
239
239
if (localInliers > 0 )
240
240
avgError /= localInliers;
241
- // cout << avgError << endl;
241
+
242
242
if (localInliers > MIN_NUM_INLIERS && avgError < minError)
243
243
{
244
244
minError = avgError;
@@ -346,7 +346,6 @@ bool DecomposeEssentialMatrix(
346
346
t (2 ) = U (2 , 2 );
347
347
348
348
// we either need t or -t
349
-
350
349
// t can also be t = UWDU.transpose()
351
350
// or t = UZU.transpose, z = -W without the 1 in the borrom right
352
351
@@ -417,15 +416,10 @@ bool Triangulate(float& depth0, float& depth1, Vector3f& x, Vector3f& xprime, Ma
417
416
418
417
float g = c * c - b * e;
419
418
if (fabs (g) < 1e-9 ) return false ;
420
- // {
421
- // do this the other way
422
- // turns out this doesn't work.
423
- // This gets degenerate solutions when the epipolar lines are parallel
424
- // Need to compute an example with some rotation
425
- // }
419
+
426
420
float d0 = 0 ;
427
421
float d1 = 0 ;
428
- if (fabs (c) < 1e-9 )// return false;
422
+ if (fabs (c) < 1e-9 )
429
423
{
430
424
d1 = (a * c - b * d) / (c * c - b * e);
431
425
d0 = (c * d1 - a) / b;
@@ -461,10 +455,6 @@ bool Triangulate(float& depth0, float& depth1, Vector3f& x, Vector3f& xprime, Ma
461
455
by virtue of being a rotation matrix. Conveniently, RQ decomposition decomposes a matrix A into
462
456
two components, one being upper-triangular - R - and the other being orthogonal. While every rotation
463
457
is orthogonal, it's also true that every orthogonal matrix is a rotation.
464
-
465
- Questions:
466
- - is this how the translation is computed? What if it is just the final column?
467
- - Do we need to scale K? Divide by final element?
468
458
*/
469
459
void DecomposeProjectiveMatrixIntoKAndE (const MatrixXf& P, Matrix3f& K, Matrix3f& E)
470
460
{
@@ -517,7 +507,6 @@ void DecomposeProjectiveMatrixIntoKAndE(const MatrixXf& P, Matrix3f& K, Matrix3f
517
507
compute the necessary rotations that transform the images
518
508
into the rectified versions
519
509
520
-
521
510
Output: both homographies
522
511
*/
523
512
void ComputeRectificationRotations (
@@ -553,34 +542,25 @@ void ComputeRectificationRotations(
553
542
// We get the logarithm of the rotation to put this in the
554
543
// Lie algebra space, halve this vector, and then take the exponential
555
544
// to convert back to the group space
556
- cout << " R total:" << endl << R1 << endl;
557
545
Vector3f rotation = SO3_log (R1);
558
- cout << " log:" << endl << rotation << endl;
559
546
rotation /= 2 ;
560
547
Matrix3f R_half = SO3_exp (rotation);
561
548
562
- cout << " R_half: " << endl << R_half << endl;
563
-
564
- // TODO: This might need to be reversed
565
- R_0 = R_half;// .transpose();
566
- R_1 = R_half.transpose ();
567
-
568
- // Z axis and Y axis are flipped???
549
+ R_0 = R_half.transpose ();
550
+ R_1 = R_half;// .transpose();
569
551
570
552
// Now build the rotation that makes the baseline the x-axis
571
553
Vector3f rx = t / t.norm ();
572
- cout << " rx " << endl << rx << endl;
573
554
Vector3f ry = Vector3f (0 , 0 , 1 ).cross (rx);
574
555
ry /= ry.norm ();
575
- cout << " ry " << endl << ry << endl;
576
556
Vector3f rz = rx.cross (ry);
577
557
// make sure everything is normalised
578
558
rz /= rz.norm ();
579
- cout << " rz " << endl << rz << endl;
559
+
580
560
Matrix3f R_baseline;
581
561
R_baseline.row (0 ) = rx;
582
- R_baseline.row (1 ) = -rz; // ry;
583
- R_baseline.row (2 ) = ry; // rz;
562
+ R_baseline.row (1 ) = ry;
563
+ R_baseline.row (2 ) = rz;
584
564
R_baseline << rx (0 ), rx (1 ), rx (2 ),
585
565
ry (0 ), ry (1 ), ry (2 ),
586
566
rz (0 ), rz (1 ), rz (2 );
@@ -592,7 +572,6 @@ void ComputeRectificationRotations(
592
572
/*
593
573
Given a homography from the original to the rectified image,
594
574
and an image, compute the rectified image using projection.
595
-
596
575
*/
597
576
// Helper
598
577
uchar BilinearInterpolatePixel (const Mat& img, const float & x, const float & y)
@@ -624,9 +603,12 @@ void RectifyImage(
624
603
// If it is there, bilinearly interpolate the value of that sub-pixel location
625
604
// In the original Mat, if this clashes with a point in the original image,
626
605
// take the average and place that there
606
+
627
607
// TODO: multithread
628
608
// To multithread, just use openmp on the outer for loop or something
629
609
// and cap threads at a reasonable number
610
+
611
+ // Iterate over the size of the original image
630
612
for (unsigned int y = 0 ; y < rectified.rows ; ++y)
631
613
{
632
614
for (unsigned int x = 0 ; x < rectified.cols ; ++x)
@@ -668,7 +650,44 @@ Mat ComputeDepthImage(
668
650
_In_ const Mat& img0,
669
651
_In_ const Mat& img1)
670
652
{
671
- return img0;
653
+ // This assumes vertical alignment
654
+ // and the same image size
655
+ if (img0.cols != img1.cols || img0.rows != img1.rows )
656
+ {
657
+ cout << " Cannot compute depth!" << endl;
658
+ return Mat (Size (0 ,0 ), CV_8U);
659
+ }
660
+
661
+ // Depth Image
662
+ Mat depth = Mat::zeros (Size (img0.cols , img0.rows ), CV_8U);
663
+
664
+ for (int y = 0 ; y < img0.rows ; ++y)
665
+ {
666
+ for (int x = 0 ; x < img0.cols ; ++x)
667
+ {
668
+ // For each pixel, find the best-matching pixel in the same row in the other image
669
+ uchar pixel0 = img0.at <uchar>(y,x);
670
+ if (pixel0 == 0 ) continue ;
671
+ int xBestMatch = 0 ;
672
+ int distance = 255 ; // biggest distance any two pixel values can be
673
+ for (int x1 = 0 ; x1 < img1.cols ; ++x1)
674
+ {
675
+ uchar pixel1 = img1.at <uchar>(y,x1);
676
+ if (pixel1 == 0 ) continue ;
677
+ if (abs (pixel1 - pixel0) < distance)
678
+ {
679
+ distance = abs (pixel1 - pixel0);
680
+ xBestMatch = x1;
681
+ }
682
+ }
683
+
684
+ // Now that we have the best match
685
+ // For now, directly set depth to x distance
686
+ depth.at <uchar>(y,x) = abs (xBestMatch -x);
687
+ }
688
+ }
689
+
690
+ return depth;
672
691
}
673
692
674
693
/*
0 commit comments