{
  "id": "opencv/kmeans-empty-labels",
  "signature": "cv::error: (-215:Assertion failed) labels.size() == samples.rows in function 'cv::kmeans'",
  "signature_zh": "cv::error: (-215：断言失败) labels.size() == samples.rows 在函数 'cv::kmeans' 中",
  "regex": "cv::error:\\s*\\(-215:Assertion failed\\) labels.size\\(\\) == samples\\.rows in function 'cv::kmeans'",
  "domain": "opencv",
  "category": "assertion_error",
  "subcategory": null,
  "root_cause": "The labels array provided to kmeans has a different size than the number of samples, or the samples matrix is empty or has zero rows.",
  "root_cause_type": "generic",
  "root_cause_zh": "传递给 kmeans 的 labels 数组大小与样本数量不一致，或者样本矩阵为空或行数为零。",
  "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": "Pre-allocating labels with np.zeros((1, N)) instead of np.zeros((N, 1), dtype=np.int32) — shape mismatch",
      "fail_rate": 0.3,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using np.random.randint to initialize labels without ensuring the dtype is np.int32",
      "fail_rate": 0.25,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Assuming the error is about K value being too large and reducing K, which doesn't fix the label size mismatch",
      "fail_rate": 0.2,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Let OpenCV allocate labels automatically by passing None: retval, labels, centers = cv2.kmeans(samples, K, None, criteria, attempts, flags). This avoids manual size issues.",
      "success_rate": 0.95,
      "how": "Let OpenCV allocate labels automatically by passing None: retval, labels, centers = cv2.kmeans(samples, K, None, criteria, attempts, flags). This avoids manual size issues.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If manually providing labels, ensure shape: labels = np.zeros((samples.shape[0], 1), dtype=np.int32). Verify with print(labels.shape) before calling kmeans.",
      "success_rate": 0.9,
      "how": "If manually providing labels, ensure shape: labels = np.zeros((samples.shape[0], 1), dtype=np.int32). Verify with print(labels.shape) before calling kmeans.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert samples to a 2D float32 matrix: samples = np.float32(samples).reshape(-1, 2) if working with points. Ensure samples.rows > 0.",
      "success_rate": 0.85,
      "how": "Convert samples to a 2D float32 matrix: samples = np.float32(samples).reshape(-1, 2) if working with points. Ensure samples.rows > 0.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "让 OpenCV 自动分配 labels，传递 None：retval, labels, centers = cv2.kmeans(samples, K, None, criteria, attempts, flags)。避免手动尺寸问题。",
    "如果需要手动提供 labels，确保形状：labels = np.zeros((samples.shape[0], 1), dtype=np.int32)。在调用 kmeans 前用 print(labels.shape) 验证。",
    "将 samples 转换为 2D float32 矩阵：samples = np.float32(samples).reshape(-1, 2)（处理点集）。确保 samples.rows > 0。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d5/d38/group__core__cluster.html#ga9a34e1b8c9c3f5a0b8b0c0a0c0a0c0a0",
  "official_doc_section": null,
  "error_code": "-215",
  "verification_tier": "ai_generated",
  "confidence": 0.8,
  "fix_success_rate": 0.87,
  "resolvable": "true",
  "first_seen": "2024-01-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}