# Error: (-215:Assertion failed) i \< dims() in function 'cv::MatSize::operator \[\]'

**URL:** <https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212>\
**Category:** C++\
**Tags:** dnn, core\
**Created:** [April 8, 2024, 5:33pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212 "2024-04-08T17:33:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![McKua1](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@McKua1](https://forum.opencv.org/u/McKua1)\
**Post date:** [April 8, 2024, 5:33pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/1 "2024-04-08T17:33:22Z")

</div>

I get this error and I dont understand very well why. I create my own model with yolov5 and I export it as ONNX symplified. Also I am using opencv 4.5.4 and c++14 in visual studio 2022.

Here i leave mi code:

#include \<opencv2/opencv.hpp\>  
#include \<opencv2/dnn.hpp\>  
#include

using namespace cv;  
using namespace dnn;  
using namespace std;

int main() {

```
String modelFile = "C://Capturas//best.onnx";
Net net = readNet(modelFile);

String imageFile = "C://Capturas//data//images//train//positivo121.jpg";
Mat frame = imread(imageFile);

if (frame.empty()) {
    cerr << "No se pudo cargar la imagen." << endl;
    return -1;
}

Size targetSize(640, 640);
resize(frame, frame, targetSize);

Mat blob = blobFromImage(frame, 1, Size(640, 640), true, false);

net.setInput(blob);

try {

    Mat detection = net.forward();
    Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());

    for (int i = 0; i < detectionMat.rows; i++) {
        float confidence = detectionMat.at<float>(i, 2);

        if (confidence > 0.5) {
            int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
            int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
            int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
            int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);

            rectangle(frame, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 2);
        }
    }
}
catch (Exception ex) {
    cout << "HUBO UN ERROR GORDO: " << ex.what() << endl;
}

imshow("Objetos detectados", frame);
waitKey(0);
destroyAllWindows();

return 0;

```

}

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 8, 2024, 10:58pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/2 "2024-04-08T22:58:04Z")

</div>

> [@McKua1](#):
>
> `detection.size[2], detection.size[3]`

… index out of bounds …

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [April 9, 2024, 7:36am UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/3 "2024-04-09T07:36:40Z")

</div>

> [@crackwitz](#):
>
> … index out of bounds …

indeed

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/98daea5011e016b682fc50fa752a475c4f9db4b3.png)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 9, 2024, 12:12pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/4 "2024-04-09T12:12:09Z")

</div>

what IS `detection.size` ?

what about class probabilities ? NMS ? yolo boxes being [cx,cy,w,h] ?

you’ll have to rewrite this anyway …

@laurent.berger , where is the image from ?

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [April 9, 2024, 12:21pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/5 "2024-04-09T12:21:38Z")

</div>

> [@berak](#):
>
> where is the image from ?

I downloaded [onnx model](https://objects.githubusercontent.com/github-production-release-asset-2e65be/264818686/d97949b1-35b6-4952-97aa-f64697370f97?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240409%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240409T073410Z&X-Amz-Expires=300&X-Amz-Signature=440c89aff74564461317ff96dba0d07a2536a7a6708db03b470569f62ed5f366&X-Amz-SignedHeaders=host&actor_id=3591626&key_id=0&repo_id=264818686&response-content-disposition=attachment%3B%20filename%3Dyolov5s6.onnx&response-content-type=application%2Foctet-stream) link in [ultralytics/yolov5: YOLOv5 🚀 in PyTorch \> ONNX \> CoreML \> TFLite](https://github.com/ultralytics/yolov5)  
then I use netron get input and output size

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 9, 2024, 12:23pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/6 "2024-04-09T12:23:37Z")

</div>

and, does opencv’s dnn really output a 3d float16 result ?

whoa, that would be new to me, and we’d have to check a lot of resp. samples now …

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [April 9, 2024, 12:33pm UTC](https://forum.opencv.org/t/error-215-assertion-failed-i-dims-in-function-cv-operator/17212/7 "2024-04-09T12:33:55Z")

</div>

No

```auto
import numpy as np
import cv2 as cv

yolov5 = cv.dnn.readNet(r"C:\Users\laurent\Downloads\yolov5s6.onnx")

img = cv.imread(cv.samples.findFile('baboon.jpg'))

paramYolov5 = cv.dnn.Image2BlobParams()
paramYolov5.datalayout = cv.dnn.DNN_LAYOUT_NCHW
paramYolov5.ddepth = cv.CV_32F
paramYolov5.mean = (0, 0, 0)
paramYolov5.scalefactor = (1 / 255, 1 / 255, 1 / 255)
paramYolov5.size = (640, 640)
paramYolov5.swapRB = True

blob = cv.dnn.blobFromImageWithParams(img, paramYolov5)
yolov5.setInput(blob)

output = yolov5.forward()

print("output.shape : ", output.shape)
print("output.dtype : ", output.dtype)

```

Results

```auto
output.shape : (1, 25500, 85)
output.dtype : float32

```

PS No idea what’s scalefactor and mean real values
