# cv::error: (-215:断言失败) !_prevImg.empty() && !_nextImg.empty() && prevImg.size() == nextImg.size() 在函数 'cv::DualTVL1OpticalFlow::calc' 中

- **ID:** `opencv/opticalflow-dual-layer-pyramid-failed`
- **领域:** opencv
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

用于 Dual TV-L1 光流的输入图像为空或尺寸不匹配。

## 版本兼容性

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

## 解决方案

1. ```
   Ensure both images are loaded successfully and convert to 32-bit float single-channel: cv::Mat prevGray, nextGray; cv::cvtColor(prevFrame, prevGray, cv::COLOR_BGR2GRAY); cv::cvtColor(nextFrame, nextGray, cv::COLOR_BGR2GRAY); prevGray.convertTo(prevGray, CV_32F, 1.0/255.0); nextGray.convertTo(nextGray, CV_32F, 1.0/255.0); Then call calc(prevGray, nextGray, flow).
   ```
2. ```
   Add explicit size check before calling calc: if (prevImg.size() != nextImg.size()) { cv::resize(nextImg, nextImg, prevImg.size()); }
   ```
3. ```
   Use cv::imread with cv::IMREAD_GRAYSCALE to directly load grayscale images and verify with if (prevImg.empty() || nextImg.empty()) { return; }
   ```

## 无效尝试

- **Convert images to grayscale using cvtColor(src, gray, COLOR_BGR2GRAY)** — The Dual TV-L1 algorithm expects single-channel float images, not 3-channel BGR. Grayscale conversion alone does not change dtype to float. (75% 失败率)
- **Resize both images to the same size using cv::resize(img, img, cv::Size(640, 480))** — While size mismatch is checked, the error often arises from empty images due to failed imread, not just size. Resize doesn't fix empty Mat. (60% 失败率)
- **Set the algorithm parameters to default by creating a new instance** — The error is a precondition check on input data, not algorithm configuration. Reinstantiating doesn't address empty or mismatched images. (90% 失败率)
