@@ -2780,15 +2780,16 @@ void cv::reprojectImageTo3D( InputArray _disparity,
2780
2780
Mat _3dImage = __3dImage.getMat ();
2781
2781
2782
2782
const double bigZ = 10000 .;
2783
- double q[4 ][4 ];
2784
- Mat _Q (4 , 4 , CV_64F, q);
2783
+ Matx44d _Q;
2785
2784
Q.convertTo (_Q, CV_64F);
2786
2785
2787
2786
int x, cols = disparity.cols ;
2788
2787
CV_Assert ( cols >= 0 );
2789
2788
2790
- std::vector<float > _sbuf (cols+1 ), _dbuf (cols*3 +1 );
2791
- float * sbuf = &_sbuf[0 ], *dbuf = &_dbuf[0 ];
2789
+ std::vector<float > _sbuf (cols);
2790
+ std::vector<Vec3f> _dbuf (cols);
2791
+ float * sbuf = &_sbuf[0 ];
2792
+ Vec3f* dbuf = &_dbuf[0 ];
2792
2793
double minDisparity = FLT_MAX;
2793
2794
2794
2795
// NOTE: here we quietly assume that at least one pixel in the disparity map is not defined.
@@ -2798,9 +2799,8 @@ void cv::reprojectImageTo3D( InputArray _disparity,
2798
2799
2799
2800
for ( int y = 0 ; y < disparity.rows ; y++ )
2800
2801
{
2801
- float *sptr = sbuf, *dptr = dbuf;
2802
- double qx = q[0 ][1 ]*y + q[0 ][3 ], qy = q[1 ][1 ]*y + q[1 ][3 ];
2803
- double qz = q[2 ][1 ]*y + q[2 ][3 ], qw = q[3 ][1 ]*y + q[3 ][3 ];
2802
+ float * sptr = sbuf;
2803
+ Vec3f* dptr = dbuf;
2804
2804
2805
2805
if ( stype == CV_8UC1 )
2806
2806
{
@@ -2821,42 +2821,36 @@ void cv::reprojectImageTo3D( InputArray _disparity,
2821
2821
sptr[x] = (float )sptr0[x];
2822
2822
}
2823
2823
else
2824
- sptr = ( float *) disparity.ptr <float >(y);
2824
+ sptr = disparity.ptr <float >(y);
2825
2825
2826
2826
if ( dtype == CV_32FC3 )
2827
- dptr = _3dImage.ptr <float >(y);
2827
+ dptr = _3dImage.ptr <Vec3f >(y);
2828
2828
2829
- for ( x = 0 ; x < cols; x++, qx += q[ 0 ][ 0 ], qy += q[ 1 ][ 0 ], qz += q[ 2 ][ 0 ], qw += q[ 3 ][ 0 ] )
2829
+ for ( x = 0 ; x < cols; x++)
2830
2830
{
2831
2831
double d = sptr[x];
2832
- double iW = 1 ./(qw + q[3 ][2 ]*d);
2833
- double X = (qx + q[0 ][2 ]*d)*iW;
2834
- double Y = (qy + q[1 ][2 ]*d)*iW;
2835
- double Z = (qz + q[2 ][2 ]*d)*iW;
2836
- if ( fabs (d-minDisparity) <= FLT_EPSILON )
2837
- Z = bigZ;
2832
+ Vec4d homg_pt = _Q*Vec4d (x, y, d, 1.0 );
2833
+ dptr[x] = Vec3d (homg_pt.val );
2834
+ dptr[x] /= homg_pt[3 ];
2838
2835
2839
- dptr[x*3 ] = (float )X;
2840
- dptr[x*3 +1 ] = (float )Y;
2841
- dptr[x*3 +2 ] = (float )Z;
2836
+ if ( fabs (d-minDisparity) <= FLT_EPSILON )
2837
+ dptr[x][2 ] = bigZ;
2842
2838
}
2843
2839
2844
2840
if ( dtype == CV_16SC3 )
2845
2841
{
2846
- short * dptr0 = _3dImage.ptr <short >(y);
2847
- for ( x = 0 ; x < cols* 3 ; x++ )
2842
+ Vec3s * dptr0 = _3dImage.ptr <Vec3s >(y);
2843
+ for ( x = 0 ; x < cols; x++ )
2848
2844
{
2849
- int ival = cvRound (dptr[x]);
2850
- dptr0[x] = cv::saturate_cast<short >(ival);
2845
+ dptr0[x] = dptr[x];
2851
2846
}
2852
2847
}
2853
2848
else if ( dtype == CV_32SC3 )
2854
2849
{
2855
- int * dptr0 = _3dImage.ptr <int >(y);
2856
- for ( x = 0 ; x < cols* 3 ; x++ )
2850
+ Vec3i * dptr0 = _3dImage.ptr <Vec3i >(y);
2851
+ for ( x = 0 ; x < cols; x++ )
2857
2852
{
2858
- int ival = cvRound (dptr[x]);
2859
- dptr0[x] = ival;
2853
+ dptr0[x] = dptr[x];
2860
2854
}
2861
2855
}
2862
2856
}
0 commit comments