replaced alloca() (a.k.a. cvStackAlloc) with AutoBuffer or vector() everywhere. cvStackAlloc() is still defined, but we do not need alloca() anymore to compile and run OpenCV (fixes #889 and may be some others)

This commit is contained in:
Vadim Pisarevsky
2011-02-18 10:29:57 +00:00
parent 7b2ec0a1e6
commit 65a7f13af3
21 changed files with 286 additions and 263 deletions

View File

@@ -2753,66 +2753,72 @@ CV_IMPL int cvStereoRectifyUncalibrated(
}
CV_IMPL void cvReprojectImageTo3D(
const CvArr* disparityImage,
CvArr* _3dImage, const CvMat* matQ,
int handleMissingValues )
void cv::reprojectImageTo3D( const Mat& disparity,
Mat& _3dImage, const Mat& Q,
bool handleMissingValues, int dtype )
{
int stype = disparity.type();
CV_Assert( stype == CV_8UC1 || stype == CV_16SC1 ||
stype == CV_32SC1 || stype == CV_32FC1 );
CV_Assert( Q.size() == Size(4,4) );
if( dtype < 0 )
dtype = CV_32FC3;
else
{
dtype = CV_MAKETYPE(CV_MAT_DEPTH(dtype), 3);
CV_Assert( dtype == CV_16SC3 || dtype == CV_32SC3 || dtype == CV_32FC3 );
}
_3dImage.create(disparity.size(), CV_MAKETYPE(dtype, 3));
const double bigZ = 10000.;
double q[4][4];
CvMat Q = cvMat(4, 4, CV_64F, q);
CvMat sstub, *src = cvGetMat( disparityImage, &sstub );
CvMat dstub, *dst = cvGetMat( _3dImage, &dstub );
int stype = CV_MAT_TYPE(src->type), dtype = CV_MAT_TYPE(dst->type);
int x, y, rows = src->rows, cols = src->cols;
float* sbuf = (float*)cvStackAlloc( cols*sizeof(sbuf[0]) );
float* dbuf = (float*)cvStackAlloc( cols*3*sizeof(dbuf[0]) );
Mat _Q(4, 4, CV_64F, q);
Q.convertTo(_Q, CV_64F);
int x, cols = disparity.cols;
CV_Assert( cols >= 0 );
vector<float> _sbuf(cols+1), _dbuf(cols*3+1);
float* sbuf = &_sbuf[0], *dbuf = &_dbuf[0];
double minDisparity = FLT_MAX;
CV_Assert( CV_ARE_SIZES_EQ(src, dst) &&
(CV_MAT_TYPE(stype) == CV_8UC1 || CV_MAT_TYPE(stype) == CV_16SC1 ||
CV_MAT_TYPE(stype) == CV_32SC1 || CV_MAT_TYPE(stype) == CV_32FC1) &&
(CV_MAT_TYPE(dtype) == CV_16SC3 || CV_MAT_TYPE(dtype) == CV_32SC3 ||
CV_MAT_TYPE(dtype) == CV_32FC3) );
cvConvert( matQ, &Q );
// NOTE: here we quietly assume that at least one pixel in the disparity map is not defined.
// and we set the corresponding Z's to some fixed big value.
if( handleMissingValues )
cvMinMaxLoc( disparityImage, &minDisparity, 0, 0, 0 );
for( y = 0; y < rows; y++ )
cv::minMaxIdx( disparity, &minDisparity, 0, 0, 0 );
for( int y = 0; y < disparity.rows; y++ )
{
const float* sptr = (const float*)(src->data.ptr + src->step*y);
float* dptr0 = (float*)(dst->data.ptr + dst->step*y), *dptr = dptr0;
float *sptr = sbuf, *dptr = dbuf;
double qx = q[0][1]*y + q[0][3], qy = q[1][1]*y + q[1][3];
double qz = q[2][1]*y + q[2][3], qw = q[3][1]*y + q[3][3];
if( stype == CV_8UC1 )
{
const uchar* sptr0 = (const uchar*)sptr;
const uchar* sptr0 = disparity.ptr<uchar>(y);
for( x = 0; x < cols; x++ )
sbuf[x] = (float)sptr0[x];
sptr = sbuf;
sptr[x] = (float)sptr0[x];
}
else if( stype == CV_16SC1 )
{
const short* sptr0 = (const short*)sptr;
const short* sptr0 = disparity.ptr<short>(y);
for( x = 0; x < cols; x++ )
sbuf[x] = (float)sptr0[x];
sptr = sbuf;
sptr[x] = (float)sptr0[x];
}
else if( stype == CV_32SC1 )
{
const int* sptr0 = (const int*)sptr;
const int* sptr0 = disparity.ptr<int>(y);
for( x = 0; x < cols; x++ )
sbuf[x] = (float)sptr0[x];
sptr = sbuf;
sptr[x] = (float)sptr0[x];
}
else
sptr = (float*)disparity.ptr<float>(y);
if( dtype != CV_32FC3 )
dptr = dbuf;
if( dtype == CV_32FC3 )
dptr = _3dImage.ptr<float>(y);
for( x = 0; x < cols; x++, qx += q[0][0], qy += q[1][0], qz += q[2][0], qw += q[3][0] )
{
@@ -2831,24 +2837,41 @@ CV_IMPL void cvReprojectImageTo3D(
if( dtype == CV_16SC3 )
{
short* dptr0 = _3dImage.ptr<short>(y);
for( x = 0; x < cols*3; x++ )
{
int ival = cvRound(dptr[x]);
((short*)dptr0)[x] = CV_CAST_16S(ival);
dptr0[x] = CV_CAST_16S(ival);
}
}
else if( dtype == CV_32SC3 )
{
int* dptr0 = _3dImage.ptr<int>(y);
for( x = 0; x < cols*3; x++ )
{
int ival = cvRound(dptr[x]);
((int*)dptr0)[x] = ival;
dptr0[x] = ival;
}
}
}
}
void cvReprojectImageTo3D( const CvArr* disparityImage,
CvArr* _3dImage, const CvMat* matQ,
int handleMissingValues )
{
cv::Mat disp = cv::cvarrToMat(disparityImage);
cv::Mat _3dimg = cv::cvarrToMat(_3dImage);
cv::Mat mq = cv::cvarrToMat(matQ);
CV_Assert( disp.size() == _3dimg.size() );
int dtype = _3dimg.type();
CV_Assert( dtype == CV_16SC3 || dtype == CV_32SC3 || dtype == CV_32FC3 );
cv::reprojectImageTo3D(disp, _3dimg, mq, handleMissingValues != 0, dtype );
}
CV_IMPL void
cvRQDecomp3x3( const CvMat *matrixM, CvMat *matrixR, CvMat *matrixQ,
CvMat *matrixQx, CvMat *matrixQy, CvMat *matrixQz,
@@ -3428,16 +3451,6 @@ bool cv::stereoRectifyUncalibrated( const Mat& points1, const Mat& points2,
return cvStereoRectifyUncalibrated(&_pt1, &_pt2, pF, imgSize, &_H1, &_H2, threshold) > 0;
}
void cv::reprojectImageTo3D( const Mat& disparity,
Mat& _3dImage, const Mat& Q,
bool handleMissingValues )
{
_3dImage.create(disparity.size(), CV_32FC3);
CvMat _disparity = disparity, __3dImage = _3dImage, matQ = Q;
cvReprojectImageTo3D( &_disparity, &__3dImage, &matQ, handleMissingValues );
}
cv::Mat cv::getOptimalNewCameraMatrix( const Mat& cameraMatrix, const Mat& distCoeffs,
Size imgSize, double alpha, Size newImgSize,
Rect* validPixROI )

View File

@@ -274,7 +274,8 @@ bool CvModelEstimator2::runLMeDS( const CvMat* m1, const CvMat* m2, CvMat* model
bool CvModelEstimator2::getSubset( const CvMat* m1, const CvMat* m2,
CvMat* ms1, CvMat* ms2, int maxAttempts )
{
int* idx = (int*)cvStackAlloc( modelPoints*sizeof(idx[0]) );
cv::AutoBuffer<int> _idx(modelPoints);
int* idx = _idx;
int i = 0, j, k, idx_i, iters = 0;
int type = CV_MAT_TYPE(m1->type), elemSize = CV_ELEM_SIZE(type);
const int *m1ptr = m1->data.i, *m2ptr = m2->data.i;

View File

@@ -482,8 +482,9 @@ icvComputeK( CvStereoGCState* state )
{
int x, y, x1, d, i, j, rows = state->left->rows, cols = state->left->cols, n = 0;
int mind = state->minDisparity, nd = state->numberOfDisparities, maxd = mind + nd;
int k = MIN(MAX((nd + 2)/4, 3), nd);
int *arr = (int*)cvStackAlloc(k*sizeof(arr[0])), delta, t, sum = 0;
int k = MIN(MAX((nd + 2)/4, 3), nd), delta, t, sum = 0;
vector<int> _arr(k);
int *arr = &_arr[0];
for( y = 0; y < rows; y++ )
{
@@ -846,8 +847,6 @@ CV_IMPL void cvFindStereoCorrespondenceGC( const CvArr* _left, const CvArr* _rig
CvSize size;
int iter, i, nZeroExpansions = 0;
CvRNG rng = cvRNG(-1);
int* disp;
CvMat _disp;
int64 E;
CV_Assert( state != 0 );
@@ -902,8 +901,9 @@ CV_IMPL void cvFindStereoCorrespondenceGC( const CvArr* _left, const CvArr* _rig
icvInitStereoConstTabs();
icvInitGraySubpix( left, right, state->left, state->right );
disp = (int*)cvStackAlloc( state->numberOfDisparities*sizeof(disp[0]) );
_disp = cvMat( 1, state->numberOfDisparities, CV_32S, disp );
vector<int> disp(state->numberOfDisparities);
CvMat _disp = cvMat( 1, (int)disp.size(), CV_32S, &disp[0] );
cvRange( &_disp, state->minDisparity, state->minDisparity + state->numberOfDisparities );
cvRandShuffle( &_disp, &rng );