# opencv triangulatePoints() giving NaN values

I'm trying to implement standard Structure from Motion code but the triangulatePoints is resulting in NaN values. I do the following steps -

1. get ORB features for image 1 and image 2.
2. Match the features.
3. get essential matrix from the matched features and camera intrinsic info available, using findEssentialMat()
4. using recoverPose() to get pose of camera 2. Camera 1 is assumed to be at the origin.
5. get projection matrices for camera1 and camera2.
6. use triangulatePoints() to get 3D points.

I have tried the Matlab triangulate() function as well with the projection matrices for camera1 and camera2 as well as matched points obtained above. In case of Matlab I get good results. But using opencv in C++ (Opencv 3.3) I get NaN values.

edit retag close merge delete

I'm not sure what's wrong with triangulate, but you can try using THIS module, which can handle more than two cameras.

Can you post your projection matrices, a few points, and the results you expect?

( 2017-09-26 18:30:15 -0500 )edit

Here are the two matrices - Cam1 = [1000 0 500 0 0 1000 500 0 0 0 1 0]

cam2 = [720.6221455223817, -103.3307817561171, 848.5437366007654, -951.8867514749264; -177.5149711798716, 950.5881039101392, 561.1333992122866, -148.7626862924559; -0.3953628981993094, -0.08757713995677291, 0.9143404307392484, 0.0602672098669313]

A few points - image1 - [363, 240] [226, 308] [204, 308] [217, 310] [260, 281]

image2 - [325.2, 264] [157.2, 324] [132, 324] [147, 325] [198, 301.2]

result i expect (that i get from matlab) - -0.2931 -0.55565 2.1387 -0.5184 -0.36494 1.893 -0.54948 -0.35738 1.8569 -0.53127 -0.35903 1.8787 -0.46763 -0.42688 1.9486

( 2017-10-02 16:46:17 -0500 )edit

Sort by » oldest newest most voted

So I didn't have an problems, using the latest version. Only very small errors, e-5, and e-6. Are you sure you are passing the correct inputs and normalizing the outputs?

Mat_< float > Cam1 = ( Mat_< float >( 3, 4 ) << 1000 , 0 , 500 , 0 , 0 , 1000 , 500 , 0 , 0 , 0 , 1 , 0 );
Mat_< float > Cam2 = ( Mat_< float >( 3, 4 ) << 720.6221455223817 , -103.3307817561171 , 848.5437366007654 , -
951.8867514749264 , -177.5149711798716 , 950.5881039101392 , 561.1333992122866 , -
148.7626862924559 , -0.3953628981993094 , -0.08757713995677291 , 0.9143404307392484 ,
0.0602672098669313 );
vector< Point2f > ptsA{
Point2f( 363, 240 ),
Point2f( 226, 308 ),
Point2f( 204, 308 ),
Point2f( 217, 310 ),
Point2f( 260, 281 )
};
vector< Point2f > ptsB{
Point2f( 325.2, 264 ),
Point2f( 157.2, 324 ),
Point2f( 132, 324 ),
Point2f( 147, 325 ),
Point2f( 198, 301.2 )
};
vector< Point3f > expected{
Point3f( -0.2931, -0.55565, 2.1387 ),
Point3f( -0.5184, -0.36494, 1.893 ),
Point3f( -0.54948, -0.35738, 1.8569 ),
Point3f( -0.53127, -0.35903, 1.8787 ),
Point3f( -0.46763, -0.42688, 1.9486 )
};
Mat exp(expected);
exp = exp.reshape( 1, expected.size() );

Mat res;
triangulatePoints( Cam1, Cam2, ptsA, ptsB, res );
res.row( 0 ) /= res.row( 3 );
res.row( 1 ) /= res.row( 3 );
res.row( 2 ) /= res.row( 3 );
res.row( 3 ) /= res.row( 3 );
res = res.rowRange( 0, 3 ).t();
for ( int i = 0; i < res.rows; ++i )
cout << norm( res.row( i ), exp.row( i ) ) << "\n";

more

Official site

GitHub

Wiki

Documentation