opencv assertion_error ai_generated partial

cv::error: (-215:断言失败) total() == 0 || data != NULL 在函数 'Mat::create' 中

cv::error: (-215:Assertion failed) total() == 0 || data != NULL in function 'Mat::create'

ID: opencv/core-mat-initialization-size-overflow

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
1证据数
2023-09-05首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.1 active
4.9.0 active

根因分析

创建具有极大尺寸(例如宽度或高度超过内存限制)的 cv::Mat 会导致内存分配失败或溢出,使数据指针为 null。

English

Creating a cv::Mat with extremely large dimensions (e.g., width or height exceeding memory limits) causes memory allocation failure or overflow, leaving data pointer null.

generic

官方文档

https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html

解决方案

  1. Validate dimensions before creating the matrix: if (width <= 0 || height <= 0 || width > 10000 || height > 10000) { /* handle error */ }
  2. Use cv::Mat::reshape or tiling to process large images in chunks, e.g., cv::Mat tile = img(cv::Rect(0, 0, 1000, 1000));

无效尝试

常见但无效的做法:

  1. Using cv::Mat::zeros with the same large size hoping it handles memory differently 100% 失败

    zeros() internally calls create() with the same allocation logic.

  2. Reducing the number of channels to 1 to save memory 60% 失败

    If the dimensions are already too large, reducing channels may not be enough; the size itself is the issue.

  3. Allocating on GPU using cv::cuda::GpuMat 75% 失败

    GPU memory is even more limited; the same overflow can occur if dimensions exceed GPU memory.