# cv::error: (-215:断言失败) !buf.empty() && buf.data != NULL 在函数 'cv::imdecode' 中

- **ID:** `opencv/io-imdecode-corrupt-data`
- **领域:** opencv
- **类别:** data_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

imdecode 的输入缓冲区为空或包含无法解码的损坏图像数据。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

1. ```
   Validate the buffer before decoding: check that buf.size() > 0 and optionally check the first few bytes for a valid image signature (e.g., JPEG: 0xFF 0xD8, PNG: 0x89 0x50). Example in C++: if (buf.size() < 4 || buf[0] != 0xFF || buf[1] != 0xD8) { /* handle error */ }.
   ```
2. ```
   If reading from a network stream, use a robust HTTP client that checks Content-Length and verifies the response body is complete before passing to imdecode.
   ```

## 无效尝试

- **Attempting to decode a buffer read from a file that was truncated mid-write** — Truncated data is still non-empty but corrupt; imdecode will fail silently or with assertion error. (90% 失败率)
- **Passing a buffer that was decoded from base64 without proper validation** — Base64 decoding may produce empty or invalid binary data if the input string is malformed. (85% 失败率)
