Unexpected behavior with ONNX model when read from OpenCV DNN module (Java)

You Can Try this example:

struct BestResult {
    int bestId;
    float bestScore;
};

BestResult getBestFromConfidenceValue(float confidenceValues[], size_t size) {
    BestResult result;
    result.bestId = -1; 
    result.bestScore = 0.0f;
    for (size_t i = 0; i < size; ++i) {
        if (confidenceValues[i] > result.bestScore) {
            result.bestId = static_cast<int>(i);
            result.bestScore = confidenceValues[i];
        }
    }
    return result;
}

void postprocess(cv::Mat& frame, const std::vector<cv::Mat>& outs, float confThreshold, float nmsThreshold) {

    std::vector<int> classIds;
    std::vector<float> confidences;
    std::vector<cv::Rect> boxes;
    int columns = 84;
    int rows = 8400;
    for (const auto& out : outs) {
        float* data_ptr = (float*)out.data;
        std::cout <<"out.rows: "<< out.size << std::endl;
        for (int i = 0; i < rows; ++i) {
            auto x = (data_ptr[i+rows*0]);
            auto y = (data_ptr[i+rows*1]);
            auto w = (data_ptr[i+rows*2]);
            auto h = (data_ptr[i+rows*3]);
            float confidenceValues[80] ={};
            for (int j = 4; j < columns; ++j) {
                confidenceValues[j-4]=data_ptr[i+rows*j];
            }
            BestResult result = getBestFromConfidenceValue(confidenceValues, 80);
            classIds.push_back(result.bestId);
            confidences.push_back(result.bestScore);
            boxes.push_back(cv::Rect(int(x-w/2), int(y-h/2) , w, h));
            std::cout <<"x="<<x <<", y="<<y <<", w="<<w <<", h="<<h << std::endl;
        }
    }