Repository: opencv/opencv
File: modules/dnn/src/layers/nonmaxsuppression_layer.cpp (C++)

Code before:
        {
            CV_Assert(inputs[2].channels() == 1 && inputs[2].total() == 1);
            inputs[2].convertTo(Mat(1, 1, CV_32S, &maxOut), CV_32S);
        }
        float iouThr = defaultIouThr;
        if (inputs.size() >= 4 && !inputs[3].empty())
        {
            CV_Assert(inputs[3].channels() == 1 && inputs[3].total() == 1);
            inputs[3].convertTo(Mat(1, 1, CV_32F, &iouThr), CV_32F);
        }
        float scoreThr = defaultScoreThr;
        if (inputs.size() >= 5 && !inputs[4].empty())
        {
            CV_Assert(inputs[4].channels() == 1 && inputs[4].total() == 1);
            inputs[4].convertTo(Mat(1, 1, CV_32F, &scoreThr), CV_32F);
        }

        const int tasks = B * C;
        std::vector<std::vector<Vec<int64_t,3>>> tripletsPerBC(tasks);

        cv::parallel_for_(cv::Range(0, tasks), [&](const cv::Range& r){
            for (int taskIdx = r.start; taskIdx < r.end; ++taskIdx) {
                const int b = taskIdx / C;
                const int c = taskIdx % C;

                std::vector<Rect2f> rects;
                std::vector<float> scores;
                std::vector<int> globalIndices;
                rects.reserve(N); scores.reserve(N); globalIndices.reserve(N);

                const float* sPtr = scoresBlob.ptr<float>(b, c);
                for (int n = 0; n < N; ++n) {
                    float sc = sPtr[n];
                    const float* bx = boxesBlob.ptr<float>(b, n);
                    Rect2f r;
                    if (centerPointBox == 0) {
                        const float y1 = bx[0], x1 = bx[1], y2 = bx[2], x2 = bx[3];
                        const float x = std::min(x1, x2), y = std::min(y1, y2);
                        const float w = std::max(0.f, std::abs(x2 - x1));
                        const float h = std::max(0.f, std::abs(y2 - y1));
                        r = Rect2f(x, y, w, h);
                    } else {
                        const float yc = bx[0], xc = bx[1];
                        const float h = std::max(0.f, bx[2]), w = std::max(0.f, bx[3]);
                        r = Rect2f(xc - 0.5f*w, yc - 0.5f*h, w, h);
                    }
                    rects.push_back(r);
                    scores.push_back(sc);
                    globalIndices.push_back(n);
                }


Review comment (suggestion):
move those `std::vector<>` declarations one level up

Code after:
            scoresBlob = uinputs[1].getMat(ACCESS_READ);
            if (uinputs.size() >= 3 && !uinputs[2].empty()) {
                Mat tmp = uinputs[2].getMat(ACCESS_READ);
                CV_Assert(tmp.channels() == 1 && tmp.total() == 1);
                maxOut = tensorToScalar<int>(tmp);
            }
            if (uinputs.size() >= 4 && !uinputs[3].empty()) {
                Mat tmp = uinputs[3].getMat(ACCESS_READ);
                CV_Assert(tmp.channels() == 1 && tmp.total() == 1);
                iouThr = tensorToScalar<float>(tmp);
            }
            if (uinputs.size() >= 5 && !uinputs[4].empty()) {
                Mat tmp = uinputs[4].getMat(ACCESS_READ);
                CV_Assert(tmp.channels() == 1 && tmp.total() == 1);
                scoreThr = tensorToScalar<float>(tmp);
            }
        } else {
            inputs_arr.getMatVector(inputs);
            CV_Assert(inputs.size() >= 2);
            boxesBlob  = inputs[0];
            scoresBlob = inputs[1];
            if (inputs.size() >= 3 && !inputs[2].empty()) {
                CV_Assert(inputs[2].channels() == 1 && inputs[2].total() == 1);
                maxOut = tensorToScalar<int>(inputs[2]);
            }
            if (inputs.size() >= 4 && !inputs[3].empty()) {
                CV_Assert(inputs[3].channels() == 1 && inputs[3].total() == 1);
                iouThr = tensorToScalar<float>(inputs[3]);
            }
            if (inputs.size() >= 5 && !inputs[4].empty()) {
                CV_Assert(inputs[4].channels() == 1 && inputs[4].total() == 1);
                scoreThr = tensorToScalar<float>(inputs[4]);
            }
        }

        const int B = boxesBlob.size[0];
        const int C = scoresBlob.size[1];
        const int tasks = B * C;

        std::vector<std::vector<Vec<int64_t,3>>> tripletsPerBC;
        run(boxesBlob, scoresBlob, maxOut, iouThr, scoreThr, tripletsPerBC);

        int K = 0;
        for (int t = 0; t < tasks; ++t) K += (int)tripletsPerBC[t].size();

        Mat outMat;
        auto outKind = outputs_arr.kind();
        if (outKind == _InputArray::STD_VECTOR_MAT) {
            std::vector<Mat>& outs = outputs_arr.getMatVecRef();
            CV_Assert(outs.size() == 1);
            outs[0].create(K, 3, CV_64S);