Skip to content

Commit 3786d30

Browse files
committed
Merge pull request opencv#9225 from paroj:reproject_cpp
2 parents adb6240 + 94fdd45 commit 3786d30

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

modules/calib3d/src/calibration.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,15 +2780,16 @@ void cv::reprojectImageTo3D( InputArray _disparity,
27802780
Mat _3dImage = __3dImage.getMat();
27812781

27822782
const double bigZ = 10000.;
2783-
double q[4][4];
2784-
Mat _Q(4, 4, CV_64F, q);
2783+
Matx44d _Q;
27852784
Q.convertTo(_Q, CV_64F);
27862785

27872786
int x, cols = disparity.cols;
27882787
CV_Assert( cols >= 0 );
27892788

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];
27922793
double minDisparity = FLT_MAX;
27932794

27942795
// 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,
27982799

27992800
for( int y = 0; y < disparity.rows; y++ )
28002801
{
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;
28042804

28052805
if( stype == CV_8UC1 )
28062806
{
@@ -2821,42 +2821,36 @@ void cv::reprojectImageTo3D( InputArray _disparity,
28212821
sptr[x] = (float)sptr0[x];
28222822
}
28232823
else
2824-
sptr = (float*)disparity.ptr<float>(y);
2824+
sptr = disparity.ptr<float>(y);
28252825

28262826
if( dtype == CV_32FC3 )
2827-
dptr = _3dImage.ptr<float>(y);
2827+
dptr = _3dImage.ptr<Vec3f>(y);
28282828

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++)
28302830
{
28312831
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];
28382835

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;
28422838
}
28432839

28442840
if( dtype == CV_16SC3 )
28452841
{
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++ )
28482844
{
2849-
int ival = cvRound(dptr[x]);
2850-
dptr0[x] = cv::saturate_cast<short>(ival);
2845+
dptr0[x] = dptr[x];
28512846
}
28522847
}
28532848
else if( dtype == CV_32SC3 )
28542849
{
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++ )
28572852
{
2858-
int ival = cvRound(dptr[x]);
2859-
dptr0[x] = ival;
2853+
dptr0[x] = dptr[x];
28602854
}
28612855
}
28622856
}

0 commit comments

Comments
 (0)