-215 opencv assertion_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
92%Fix Rate
81%Confidence
1Evidence
2023-04-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success Set maxCorners to a positive integer, e.g., corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10). Ensure maxCorners > 0.
    Set maxCorners to a positive integer, e.g., corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10). Ensure maxCorners > 0.
  2. 90% success If the number of corners to detect is unknown, use a reasonable default like 1000 and then filter later: corners = cv2.goodFeaturesToTrack(gray, maxCorners=1000, qualityLevel=0.01, minDistance=10).
    If the number of corners to detect is unknown, use a reasonable default like 1000 and then filter later: corners = cv2.goodFeaturesToTrack(gray, maxCorners=1000, qualityLevel=0.01, minDistance=10).
  3. 85% success Validate maxCorners before calling: if max_corners <= 0: raise ValueError('maxCorners must be positive'). Also ensure the image is grayscale and non-empty.
    Validate maxCorners before calling: if max_corners <= 0: raise ValueError('maxCorners must be positive'). Also ensure the image is grayscale and non-empty.

中文步骤

  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')。同时确保图像是灰度且非空。

Dead Ends

Common approaches that don't work:

  1. 30% fail

    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% fail

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

  3. 20% fail

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