{
  "id": "opencv/contour-approximation-empty",
  "signature": "cv::error: (-215:Assertion failed) count >= 0 in function 'cv::approxPolyDP'",
  "signature_zh": "cv::error: (-215：断言失败) count >= 0 在函数 'cv::approxPolyDP' 中",
  "regex": "cv::error:\\s*\\(-215:Assertion failed\\) count >= 0 in function 'cv::approxPolyDP'",
  "domain": "opencv",
  "category": "assertion_error",
  "subcategory": null,
  "root_cause": "approxPolyDP received an empty or invalid contour (zero points) because the input contour was not properly extracted or was filtered out.",
  "root_cause_type": "generic",
  "root_cause_zh": "approxPolyDP 接收到空或无效的轮廓（零个点），因为输入轮廓未被正确提取或被过滤掉。",
  "versions": [
    {
      "version": "4.5.5",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Increasing the epsilon parameter to make the approximation less strict, which only masks the issue but doesn't fix empty contours",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Re-running findContours with different retrieval modes (e.g., RETR_EXTERNAL) without checking if contours exist",
      "fail_rate": 0.3,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Assuming the contour is always valid and skipping the empty check entirely",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add a guard clause before calling approxPolyDP: if len(contour) == 0: continue. Example: for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)",
      "success_rate": 0.95,
      "how": "Add a guard clause before calling approxPolyDP: if len(contour) == 0: continue. Example: for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use cv2.contourArea to filter out contours with zero area before approximation: if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)",
      "success_rate": 0.9,
      "how": "Use cv2.contourArea to filter out contours with zero area before approximation: if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure findContours is called with the correct mode (e.g., RETR_TREE or RETR_LIST) and that the hierarchy is not corrupt; sometimes contour[0] can be empty if the image is all black",
      "success_rate": 0.8,
      "how": "Ensure findContours is called with the correct mode (e.g., RETR_TREE or RETR_LIST) and that the hierarchy is not corrupt; sometimes contour[0] can be empty if the image is all black",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在调用 approxPolyDP 前添加守卫检查：if len(contour) == 0: continue。示例：for cnt in contours: if cnt.shape[0] < 3: continue; approx = cv2.approxPolyDP(cnt, epsilon, True)",
    "使用 cv2.contourArea 过滤面积为零的轮廓：if cv2.contourArea(cnt) > 0: approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)",
    "确保 findContours 使用正确的模式（如 RETR_TREE 或 RETR_LIST），并且层次结构未损坏；如果图像全黑，轮廓[0]可能为空"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/dd/d49/tutorial_py_contour_features.html",
  "official_doc_section": null,
  "error_code": "-215",
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-03-22",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}