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

- **ID:** `opencv/core-mat-initialization-size-overflow`
- **领域:** opencv
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.1 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

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));
   ```

## 无效尝试

- **Using cv::Mat::zeros with the same large size hoping it handles memory differently** — zeros() internally calls create() with the same allocation logic. (100% 失败率)
- **Reducing the number of channels to 1 to save memory** — If the dimensions are already too large, reducing channels may not be enough; the size itself is the issue. (60% 失败率)
- **Allocating on GPU using cv::cuda::GpuMat** — GPU memory is even more limited; the same overflow can occur if dimensions exceed GPU memory. (75% 失败率)
