opencv assertion_error ai_generated true

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

cv::error: (-215:Assertion failed) !_prevImg.empty() && !_nextImg.empty() && prevImg.size() == nextImg.size() in function 'cv::DualTVL1OpticalFlow::calc'

ID: opencv/opticalflow-dual-layer-pyramid-failed

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

版本兼容性

版本状态引入弃用备注
opencv-4.5.5 active
opencv-4.8.0 active
opencv-4.9.0 active

根因分析

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

English

Input images for Dual TV-L1 optical flow are empty or have mismatched dimensions.

generic

官方文档

https://docs.opencv.org/4.x/dc/d6b/classcv_1_1DualTVL1OpticalFlow.html

解决方案

  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; }

无效尝试

常见但无效的做法:

  1. Convert images to grayscale using cvtColor(src, gray, COLOR_BGR2GRAY) 75% 失败

    The Dual TV-L1 algorithm expects single-channel float images, not 3-channel BGR. Grayscale conversion alone does not change dtype to float.

  2. Resize both images to the same size using cv::resize(img, img, cv::Size(640, 480)) 60% 失败

    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.

  3. Set the algorithm parameters to default by creating a new instance 90% 失败

    The error is a precondition check on input data, not algorithm configuration. Reinstantiating doesn't address empty or mismatched images.