1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

optimize: replace push_back with emplace_back in core loops

Replaces wasteful temporary object construction with in-place construction using emplace_back. Covers hot paths in objdetect, imgproc, and stitching modules.
This commit is contained in:
AdwaithBatchu
2025-12-19 11:56:20 +05:30
parent b157553e55
commit 5f0bd387db
5 changed files with 50 additions and 50 deletions
+12 -12
View File
@@ -1063,9 +1063,9 @@ public:
if( classifier->data.stages.size() + result == 0 )
{
mtx->lock();
rectangles->push_back(Rect(cvRound(x*scalingFactor),
cvRound(y*scalingFactor),
winSize.width, winSize.height));
rectangles->emplace_back(cvRound(x*scalingFactor),
cvRound(y*scalingFactor),
winSize.width, winSize.height);
rejectLevels->push_back(-result);
levelWeights->push_back(gypWeight);
mtx->unlock();
@@ -1074,9 +1074,9 @@ public:
else if( result > 0 )
{
mtx->lock();
rectangles->push_back(Rect(cvRound(x*scalingFactor),
cvRound(y*scalingFactor),
winSize.width, winSize.height));
rectangles->emplace_back(cvRound(x*scalingFactor),
cvRound(y*scalingFactor),
winSize.width, winSize.height);
mtx->unlock();
}
if( result == 0 )
@@ -1222,10 +1222,10 @@ bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector<fl
for( int i = 0; i < nfaces; i++ )
{
const FeatureEvaluator::ScaleData& s = featureEvaluator->getScaleData(fptr[i*3 + 1]);
candidates.push_back(Rect(cvRound(fptr[i*3 + 2]*s.scale),
cvRound(fptr[i*3 + 3]*s.scale),
cvRound(data.origWinSize.width*s.scale),
cvRound(data.origWinSize.height*s.scale)));
candidates.emplace_back(cvRound(fptr[i*3 + 2]*s.scale),
cvRound(fptr[i*3 + 3]*s.scale),
cvRound(data.origWinSize.width*s.scale),
cvRound(data.origWinSize.height*s.scale));
}
}
return ok;
@@ -1570,8 +1570,8 @@ bool CascadeClassifierImpl::Data::read(const FileNode &root)
for( int i = 0; i < ntrees; i++, nodeOfs++, leafOfs+= 2 )
{
const DTreeNode& node = nodes[nodeOfs];
stumps.push_back(Stump(node.featureIdx, node.threshold,
leaves[leafOfs], leaves[leafOfs+1]));
stumps.emplace_back(node.featureIdx, node.threshold,
leaves[leafOfs], leaves[leafOfs+1]);
}
}
}
+17 -17
View File
@@ -412,9 +412,9 @@ void QRDetect::fixationPoints(vector<Point2f> &local_point)
list_area_pnt.push_back(current_point);
vector<LineIterator> list_line_iter;
list_line_iter.push_back(LineIterator(bin_barcode, current_point, left_point));
list_line_iter.push_back(LineIterator(bin_barcode, current_point, central_point));
list_line_iter.push_back(LineIterator(bin_barcode, current_point, right_point));
list_line_iter.emplace_back(bin_barcode, current_point, left_point);
list_line_iter.emplace_back(bin_barcode, current_point, central_point);
list_line_iter.emplace_back(bin_barcode, current_point, right_point);
for (size_t k = 0; k < list_line_iter.size(); k++)
{
@@ -757,7 +757,7 @@ vector<Point2f> QRDetect::getQuadrilateral(vector<Point2f> angle_list)
{
int x = cvRound(angle_list[i].x);
int y = cvRound(angle_list[i].y);
locations.push_back(Point(x, y));
locations.emplace_back(x,y);
}
vector<Point> integer_hull;
@@ -2217,13 +2217,13 @@ bool QRDecode::straightenQRCodeInParts()
vector<Point2f> perspective_points;
perspective_points.push_back(Point2f(0.0, start_cut));
perspective_points.push_back(Point2f(perspective_curved_size, start_cut));
perspective_points.emplace_back(0.0, start_cut);
perspective_points.emplace_back(perspective_curved_size, start_cut);
perspective_points.push_back(Point2f(perspective_curved_size, start_cut + dist));
perspective_points.push_back(Point2f(0.0, start_cut+dist));
perspective_points.emplace_back(perspective_curved_size, start_cut + dist);
perspective_points.emplace_back(0.0, start_cut+dist);
perspective_points.push_back(Point2f(perspective_curved_size * 0.5f, start_cut + dist * 0.5f));
perspective_points.emplace_back(perspective_curved_size * 0.5f, start_cut + dist * 0.5f);
if (i == 1)
{
@@ -2294,13 +2294,13 @@ bool QRDecode::straightenQRCodeInParts()
}
vector<Point2f> perspective_straight_points;
perspective_straight_points.push_back(Point2f(0.f, 0.f));
perspective_straight_points.push_back(Point2f(perspective_curved_size, 0.f));
perspective_straight_points.emplace_back(0.f, 0.f);
perspective_straight_points.emplace_back(perspective_curved_size, 0.f);
perspective_straight_points.push_back(Point2f(perspective_curved_size, perspective_curved_size));
perspective_straight_points.push_back(Point2f(0.f, perspective_curved_size));
perspective_straight_points.emplace_back(perspective_curved_size, perspective_curved_size);
perspective_straight_points.emplace_back(0.f, perspective_curved_size);
perspective_straight_points.push_back(Point2f(perspective_curved_size * 0.5f, perspective_curved_size * 0.5f));
perspective_straight_points.emplace_back(perspective_curved_size * 0.5f, perspective_curved_size * 0.5f);
Mat H = findHomography(original_curved_points, perspective_straight_points);
if (H.empty())
@@ -3319,9 +3319,9 @@ void QRDetectMulti::fixationPoints(vector<Point2f> &local_point)
list_area_pnt.push_back(current_point);
vector<LineIterator> list_line_iter;
list_line_iter.push_back(LineIterator(bin_barcode_temp, current_point, left_point));
list_line_iter.push_back(LineIterator(bin_barcode_temp, current_point, central_point));
list_line_iter.push_back(LineIterator(bin_barcode_temp, current_point, right_point));
list_line_iter.emplace_back(bin_barcode_temp, current_point, left_point);
list_line_iter.emplace_back(bin_barcode_temp, current_point, central_point);
list_line_iter.emplace_back(bin_barcode_temp, current_point, right_point);
for (size_t k = 0; k < list_line_iter.size(); k++)
{