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

Code before:
    }

    void getTypes(const std::vector<MatType>&, const int requiredOutputs, const int requiredInternals,
                   std::vector<MatType>& outputs, std::vector<MatType>& internals) const CV_OVERRIDE {
        outputs.assign(requiredOutputs, MatType(CV_64S));
    }

    void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE {
        std::vector<Mat> inputs, outputs;

        Mat boxesBlob, scoresBlob;
        int maxOut = defaultMaxOut;
        float iouThr = defaultIouThr;
        float scoreThr = defaultScoreThr;

        auto inKind = inputs_arr.kind();
        if (inKind == _InputArray::STD_VECTOR_UMAT) {
            std::vector<UMat> uinputs;
            inputs_arr.getUMatVector(uinputs);
            CV_Assert(uinputs.size() >= 2);
            boxesBlob = uinputs[0].getMat(ACCESS_READ);
            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);
                tmp.convertTo(Mat(1, 1, CV_32S, &maxOut), CV_32S);
            }
            if (uinputs.size() >= 4 && !uinputs[3].empty()) {
                Mat tmp = uinputs[3].getMat(ACCESS_READ);
                CV_Assert(tmp.channels() == 1 && tmp.total() == 1);
                tmp.convertTo(Mat(1, 1, CV_32F, &iouThr), CV_32F);
            }
            if (uinputs.size() >= 5 && !uinputs[4].empty()) {
                Mat tmp = uinputs[4].getMat(ACCESS_READ);
                CV_Assert(tmp.channels() == 1 && tmp.total() == 1);
                tmp.convertTo(Mat(1, 1, CV_32F, &scoreThr), CV_32F);
            }
        } 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);
                inputs[2].convertTo(Mat(1, 1, CV_32S, &maxOut), CV_32S);
            }
            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);
            }
            if (inputs.size() >= 5 && !inputs[4].empty()) {

Review comment (suggestion):
this is very bad pattern. _never_ construct temporary matrix, e.g. using `Mat()` constructor for output matrices. It's very error-prone. Instead, there is a function `tensorToScalar()` from layers_common.hpp, which you should use here and in all other cases in this file and others.

Code after:
    }

    void getTypes(const std::vector<MatType>&, const int requiredOutputs, const int requiredInternals,
                   std::vector<MatType>& outputs, std::vector<MatType>& internals) const CV_OVERRIDE {
        outputs.assign(requiredOutputs, MatType(CV_64S));
    }

    void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE {
        std::vector<Mat> inputs, outputs;

        Mat boxesBlob, scoresBlob;
        int maxOut = defaultMaxOut;
        float iouThr = defaultIouThr;
        float scoreThr = defaultScoreThr;

        auto inKind = inputs_arr.kind();
        if (inKind == _InputArray::STD_VECTOR_UMAT) {
            std::vector<UMat> uinputs;
            inputs_arr.getUMatVector(uinputs);
            CV_Assert(uinputs.size() >= 2);
            boxesBlob = uinputs[0].getMat(ACCESS_READ);
            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()) {