{
  "id": "opencv/contours-not-found-in-binary-image",
  "signature": "cv::error: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'cv::findContours'",
  "signature_zh": "cv::error: (-215:断言失败) _src.type() == CV_8UC1 在函数 'cv::findContours' 中",
  "regex": "cv::error: \\(-215:Assertion failed\\) _src\\.type\\(\\) == CV_8UC1 in function 'cv::findContours'",
  "domain": "opencv",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "findContours requires an 8-bit single-channel binary image, but the input is multi-channel or non-8-bit.",
  "root_cause_type": "generic",
  "root_cause_zh": "findContours 需要8位单通道二值图像，但输入图像是多通道或非8位类型。",
  "versions": [
    {
      "version": "4.5.5",
      "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": "Grayscale may still have multiple channels (e.g., 3-channel grayscale) or be 16-bit. findContours explicitly needs CV_8UC1.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Canny output is binary but may still have non-zero values outside 0-255 if not properly normalized; thresholding ensures binary.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Resize does not change channel count; the image remains 3-channel.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n_, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)\ncontours, _ = cv2.findContours(img_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)",
      "success_rate": 0.95,
      "how": "img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n_, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)\ncontours, _ = cv2.findContours(img_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)",
      "condition": "",
      "sources": []
    },
    {
      "action": "edges = cv2.Canny(img_gray, 50, 150)\ncontours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)",
      "success_rate": 0.85,
      "how": "edges = cv2.Canny(img_gray, 50, 150)\ncontours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n_, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)\ncontours, _ = cv2.findContours(img_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)",
    "edges = cv2.Canny(img_gray, 50, 150)\ncontours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0",
  "official_doc_section": null,
  "error_code": "-215",
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2024-03-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}