mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge pull request #28907 from chacha21:more_autobuffer
More use of AutoBuffer #28907 When possible, AutoBuffer should be faster than std::vector<>, and should not be worse if it requires a heap allocation rather than a stack allocation. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -98,8 +98,8 @@ static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
|
||||
return false;
|
||||
|
||||
copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
|
||||
std::vector<float> _space_weight(d * d);
|
||||
std::vector<int> _space_ofs(d * d);
|
||||
AutoBuffer<float> _space_weight(d * d);
|
||||
AutoBuffer<int> _space_ofs(d * d);
|
||||
float * const space_weight = &_space_weight[0];
|
||||
int * const space_ofs = &_space_ofs[0];
|
||||
|
||||
@@ -188,9 +188,9 @@ bilateralFilter_8u( const Mat& src, Mat& dst, int d,
|
||||
Mat temp;
|
||||
copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
|
||||
|
||||
std::vector<float> _color_weight(cn*256);
|
||||
std::vector<float> _space_weight(d*d);
|
||||
std::vector<int> _space_ofs(d*d);
|
||||
AutoBuffer<float> _color_weight(cn*256);
|
||||
AutoBuffer<float> _space_weight(d*d);
|
||||
AutoBuffer<int> _space_ofs(d*d);
|
||||
float* color_weight = &_color_weight[0];
|
||||
float* space_weight = &_space_weight[0];
|
||||
int* space_ofs = &_space_ofs[0];
|
||||
@@ -283,15 +283,15 @@ bilateralFilter_32f( const Mat& src, Mat& dst, int d,
|
||||
copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
|
||||
|
||||
// allocate lookup tables
|
||||
std::vector<float> _space_weight(d*d);
|
||||
std::vector<int> _space_ofs(d*d);
|
||||
AutoBuffer<float> _space_weight(d*d);
|
||||
AutoBuffer<int> _space_ofs(d*d);
|
||||
float* space_weight = &_space_weight[0];
|
||||
int* space_ofs = &_space_ofs[0];
|
||||
|
||||
// assign a length which is slightly more than needed
|
||||
len = (float)(maxValSrc - minValSrc) * cn;
|
||||
kExpNumBins = kExpNumBinsPerChannel * cn;
|
||||
std::vector<float> _expLUT(kExpNumBins+2);
|
||||
AutoBuffer<float> _expLUT(kExpNumBins+2);
|
||||
float* expLUT = &_expLUT[0];
|
||||
|
||||
scale_index = kExpNumBins/len;
|
||||
|
||||
@@ -1153,10 +1153,10 @@ namespace cv{
|
||||
//Array used to store info and labeled pixel by each thread.
|
||||
//Different threads affect different memory location of chunksSizeAndLabels
|
||||
const int chunksSizeAndLabelsSize = roundUp(h, 2);
|
||||
std::vector<int> chunksSizeAndLabels(chunksSizeAndLabelsSize);
|
||||
AutoBuffer<int> chunksSizeAndLabels(chunksSizeAndLabelsSize);
|
||||
|
||||
//Tree of labels
|
||||
std::vector<LabelT> P(Plength, 0);
|
||||
AutoBuffer<LabelT> P(Plength, 0);
|
||||
//First label is for background
|
||||
//P[0] = 0;
|
||||
|
||||
@@ -1176,7 +1176,7 @@ namespace cv{
|
||||
}
|
||||
|
||||
//Array for statistics data
|
||||
std::vector<StatsOp> sopArray(h);
|
||||
AutoBuffer<StatsOp> sopArray(h);
|
||||
sop.init(nLabels);
|
||||
|
||||
//Second scan
|
||||
@@ -1218,7 +1218,7 @@ namespace cv{
|
||||
// ............
|
||||
const size_t Plength = size_t(((h + 1) / 2) * size_t((w + 1) / 2)) + 1;
|
||||
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT *P = P_.data();
|
||||
//P[0] = 0;
|
||||
LabelT lunique = 1;
|
||||
@@ -1782,10 +1782,10 @@ namespace cv{
|
||||
|
||||
//Array used to store info and labeled pixel by each thread.
|
||||
//Different threads affect different memory location of chunksSizeAndLabels
|
||||
std::vector<int> chunksSizeAndLabels(roundUp(h, 2));
|
||||
AutoBuffer<int> chunksSizeAndLabels(roundUp(h, 2));
|
||||
|
||||
//Tree of labels
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT* P = P_.data();
|
||||
//First label is for background
|
||||
//P[0] = 0;
|
||||
@@ -1806,7 +1806,7 @@ namespace cv{
|
||||
}
|
||||
|
||||
//Array for statistics dataof threads
|
||||
std::vector<StatsOp> sopArray(h);
|
||||
AutoBuffer<StatsOp> sopArray(h);
|
||||
|
||||
sop.init(nLabels);
|
||||
//Second scan
|
||||
@@ -1842,7 +1842,7 @@ namespace cv{
|
||||
// ............
|
||||
const size_t Plength = size_t((size_t(h) * size_t(w) + 1) / 2) + 1;
|
||||
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT* P = P_.data();
|
||||
P[0] = 0;
|
||||
LabelT lunique = 1;
|
||||
@@ -2315,10 +2315,10 @@ namespace cv{
|
||||
|
||||
//Array used to store info and labeled pixel by each thread.
|
||||
//Different threads affect different memory location of chunksSizeAndLabels
|
||||
std::vector<int> chunksSizeAndLabels(roundUp(h, 2));
|
||||
AutoBuffer<int> chunksSizeAndLabels(roundUp(h, 2));
|
||||
|
||||
//Tree of labels
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT *P = P_.data();
|
||||
//First label is for background
|
||||
//P[0] = 0;
|
||||
@@ -2352,7 +2352,7 @@ namespace cv{
|
||||
}
|
||||
|
||||
//Array for statistics dataof threads
|
||||
std::vector<StatsOp> sopArray(h);
|
||||
AutoBuffer<StatsOp> sopArray(h);
|
||||
|
||||
sop.init(nLabels);
|
||||
//Second scan
|
||||
@@ -2387,7 +2387,7 @@ namespace cv{
|
||||
//Obviously, 4-way connectivity upper bound is also good for 8-way connectivity labeling
|
||||
const size_t Plength = (size_t(h) * size_t(w) + 1) / 2 + 1;
|
||||
//array P for equivalences resolution
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT *P = P_.data();
|
||||
//first label is for background pixels
|
||||
//P[0] = 0;
|
||||
@@ -4265,10 +4265,10 @@ namespace cv{
|
||||
//Array used to store info and labeled pixel by each thread.
|
||||
//Different threads affect different memory location of chunksSizeAndLabels
|
||||
const int chunksSizeAndLabelsSize = roundUp(h, 2);
|
||||
std::vector<int> chunksSizeAndLabels(chunksSizeAndLabelsSize);
|
||||
AutoBuffer<int> chunksSizeAndLabels(chunksSizeAndLabelsSize);
|
||||
|
||||
//Tree of labels
|
||||
std::vector<LabelT> P(Plength, 0);
|
||||
AutoBuffer<LabelT> P(Plength, 0);
|
||||
//First label is for background
|
||||
//P[0] = 0;
|
||||
|
||||
@@ -4288,7 +4288,7 @@ namespace cv{
|
||||
}
|
||||
|
||||
//Array for statistics data
|
||||
std::vector<StatsOp> sopArray(h);
|
||||
AutoBuffer<StatsOp> sopArray(h);
|
||||
sop.init(nLabels);
|
||||
|
||||
//Second scan
|
||||
@@ -4323,7 +4323,7 @@ namespace cv{
|
||||
//............
|
||||
const size_t Plength = size_t(((h + 1) / 2) * size_t((w + 1) / 2)) + 1;
|
||||
|
||||
std::vector<LabelT> P_(Plength, 0);
|
||||
AutoBuffer<LabelT> P_(Plength, 0);
|
||||
LabelT *P = P_.data();
|
||||
//P[0] = 0;
|
||||
LabelT lunique = 1;
|
||||
|
||||
@@ -101,7 +101,7 @@ static void getSobelKernels( OutputArray _kx, OutputArray _ky,
|
||||
|
||||
if( _ksize % 2 == 0 || _ksize > 31 )
|
||||
CV_Error( cv::Error::StsOutOfRange, "The kernel size must be odd and not larger than 31" );
|
||||
std::vector<int> kerI(std::max(ksizeX, ksizeY) + 1);
|
||||
AutoBuffer<int> kerI(std::max(ksizeX, ksizeY) + 1);
|
||||
|
||||
CV_Assert( dx >= 0 && dy >= 0 && dx+dy > 0 );
|
||||
|
||||
|
||||
@@ -359,13 +359,13 @@ HoughLinesSDiv( InputArray image, OutputArray lines, int type,
|
||||
lst.push_back(hough_index(threshold, -1.f, 0.f));
|
||||
|
||||
// Precalculate sin table
|
||||
std::vector<float> _sinTable( 5 * tn * stn );
|
||||
AutoBuffer<float> _sinTable( 5 * tn * stn );
|
||||
float* sinTable = &_sinTable[0];
|
||||
|
||||
for( index = 0; index < 5 * tn * stn; index++ )
|
||||
sinTable[index] = (float)cos( stheta * index * 0.2f );
|
||||
|
||||
std::vector<uchar> _caccum(rn * tn, (uchar)0);
|
||||
AutoBuffer<uchar> _caccum(rn * tn, (uchar)0);
|
||||
uchar* caccum = &_caccum[0];
|
||||
|
||||
// Counting all feature pixels
|
||||
@@ -373,7 +373,7 @@ HoughLinesSDiv( InputArray image, OutputArray lines, int type,
|
||||
for( col = 0; col < w; col++ )
|
||||
fn += _POINT( row, col ) != 0;
|
||||
|
||||
std::vector<int> _x(fn), _y(fn);
|
||||
AutoBuffer<int> _x(fn), _y(fn);
|
||||
int* x = &_x[0], *y = &_y[0];
|
||||
|
||||
// Full Hough Transform (it's accumulator update part)
|
||||
@@ -445,7 +445,7 @@ HoughLinesSDiv( InputArray image, OutputArray lines, int type,
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uchar> _buffer(srn * stn + 2);
|
||||
AutoBuffer<uchar> _buffer(srn * stn + 2);
|
||||
uchar* buffer = &_buffer[0];
|
||||
uchar* mcaccum = buffer + 1;
|
||||
|
||||
@@ -586,7 +586,7 @@ HoughLinesProbabilistic( Mat& image,
|
||||
|
||||
Mat accum = Mat::zeros( numangle, numrho, CV_32SC1 );
|
||||
Mat mask( height, width, CV_8UC1 );
|
||||
std::vector<float> trigtab(numangle*2);
|
||||
AutoBuffer<float> trigtab(numangle*2);
|
||||
|
||||
for( int n = 0; n < numangle; n++ )
|
||||
{
|
||||
@@ -2382,7 +2382,7 @@ static void HoughCircles( InputArray _image, OutputArray _circles,
|
||||
|
||||
if( type == CV_32FC4 )
|
||||
{
|
||||
std::vector<Vec4f> cw(ncircles);
|
||||
AutoBuffer<Vec4f> cw(ncircles);
|
||||
for( i = 0; i < ncircles; i++ )
|
||||
cw[i] = GetCircle4f(circles[i]);
|
||||
if (ncircles > 0)
|
||||
@@ -2390,7 +2390,7 @@ static void HoughCircles( InputArray _image, OutputArray _circles,
|
||||
}
|
||||
else if( type == CV_32FC3 )
|
||||
{
|
||||
std::vector<Vec3f> cwow(ncircles);
|
||||
AutoBuffer<Vec3f> cwow(ncircles);
|
||||
for( i = 0; i < ncircles; i++ )
|
||||
cwow[i] = GetCircle(circles[i]);
|
||||
if (ncircles > 0)
|
||||
|
||||
@@ -128,8 +128,8 @@ medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
|
||||
# define CV_ALIGNMENT 16
|
||||
#endif
|
||||
|
||||
std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + CV_ALIGNMENT);
|
||||
std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + CV_ALIGNMENT);
|
||||
AutoBuffer<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + CV_ALIGNMENT);
|
||||
AutoBuffer<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + CV_ALIGNMENT);
|
||||
HT* h_coarse = alignPtr(&_h_coarse[0], CV_ALIGNMENT);
|
||||
HT* h_fine = alignPtr(&_h_fine[0], CV_ALIGNMENT);
|
||||
|
||||
|
||||
@@ -887,7 +887,7 @@ static bool ocl_morphOp(InputArray _src, OutputArray _dst, InputArray _kernel,
|
||||
if (actual_op < 0)
|
||||
actual_op = op;
|
||||
|
||||
std::vector<ocl::Kernel> kernels(iterations);
|
||||
AutoBuffer<ocl::Kernel> kernels(iterations);
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
int current_op = iterations == i + 1 ? actual_op : op;
|
||||
|
||||
@@ -793,6 +793,7 @@ void Subdiv2D::getLeadingEdgeList(std::vector<int>& leadingEdgeList) const
|
||||
{
|
||||
leadingEdgeList.clear();
|
||||
int i, total = (int)(qedges.size()*4);
|
||||
//use a std::vector<bool> to benefit from the "bitset size/8" implementation
|
||||
std::vector<bool> edgemask(total, false);
|
||||
|
||||
for( i = 4; i < total; i += 2 )
|
||||
@@ -813,6 +814,7 @@ void Subdiv2D::getTriangleList(std::vector<Vec6f>& triangleList) const
|
||||
{
|
||||
triangleList.clear();
|
||||
int i, total = (int)(qedges.size()*4);
|
||||
//use a std::vector<bool> to benefit from the "bitset size/8" implementation
|
||||
std::vector<bool> edgemask(total, false);
|
||||
Rect2f rect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
|
||||
|
||||
|
||||
@@ -568,7 +568,6 @@ void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
|
||||
{
|
||||
const double blockScale = 4.5;
|
||||
const int minBlockSize = 256;
|
||||
std::vector<uchar> buf;
|
||||
|
||||
Mat templ = _templ;
|
||||
int depth = img.depth(), cn = img.channels();
|
||||
@@ -624,7 +623,7 @@ void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
|
||||
if( (ccn > 1 || cn > 1) && cdepth != maxDepth )
|
||||
bufSize = std::max( bufSize, blocksize.width*blocksize.height*CV_ELEM_SIZE(cdepth));
|
||||
|
||||
buf.resize(bufSize);
|
||||
AutoBuffer<uchar> buf(bufSize);
|
||||
|
||||
Ptr<hal::DFT2D> c = hal::DFT2D::create(dftsize.width, dftsize.height, dftTempl.depth(), 1, 1, CV_HAL_DFT_IS_INPLACE, templ.rows);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user