-215 opencv assertion_error ai_generated true

cv::error: (-215:断言失败) nfeatures > 0 在函数 'cv::goodFeaturesToTrack' 中

cv::error: (-215:Assertion failed) nfeatures > 0 in function 'cv::goodFeaturesToTrack'

ID: opencv/good-features-to-track-empty

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

版本兼容性

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

根因分析

goodFeaturesToTrack 被调用时 maxCorners 设置为 0 或负值,这是无效的,因为该函数至少需要检测一个角点。

English

goodFeaturesToTrack was called with maxCorners set to 0 or a negative value, which is invalid because the function requires at least one corner to detect.

generic

官方文档

https://docs.opencv.org/4.x/dd/d1a/group__imgproc__feature.html#ga1d6bb77486c8f92d79c8793ad995d541

解决方案

  1. 将 maxCorners 设置为正整数,例如:corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10)。确保 maxCorners > 0。
  2. 如果未知要检测的角点数量,使用合理的默认值如 1000,然后后续过滤:corners = cv2.goodFeaturesToTrack(gray, maxCorners=1000, qualityLevel=0.01, minDistance=10)。
  3. 在调用前验证 maxCorners:if max_corners <= 0: raise ValueError('maxCorners must be positive')。同时确保图像是灰度且非空。

无效尝试

常见但无效的做法:

  1. 30% 失败

    Increasing the qualityLevel parameter to a very high value (e.g., 0.99) hoping to find more corners but maxCorners is still 0

  2. 25% 失败

    Assuming the error is from the image being too dark and applying histogram equalization without fixing maxCorners

  3. 20% 失败

    Passing maxCorners as a float (e.g., 0.5) which gets truncated to 0 in some language bindings