{
  "id": "opencv/kmeans-initial-centers-mismatch",
  "signature": "cv::error: (-215:Assertion failed) centers.cols == data.cols in function 'cv::kmeans'",
  "signature_zh": "cv::error: (-215:断言失败) centers.cols == data.cols 在函数 'cv::kmeans' 中",
  "regex": "centers\\.cols == data\\.cols",
  "domain": "opencv",
  "category": "assertion_error",
  "subcategory": null,
  "root_cause": "When providing initial cluster centers to kmeans, the number of columns (features) in the centers matrix does not match the number of columns in the data matrix.",
  "root_cause_type": "generic",
  "root_cause_zh": "当向 kmeans 提供初始聚类中心时，中心矩阵的列数（特征数）与数据矩阵的列数不匹配。",
  "versions": [
    {
      "version": "opencv-4.5.5",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "opencv-4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "opencv-4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Transpose the data matrix using data.t()",
      "why_fails": "kmeans expects data as rows of samples (each row is a sample). Transposing changes the shape incorrectly, leading to centers.cols != data.cols in a different way.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Set the number of clusters K to a smaller value",
      "why_fails": "The error is about feature dimension mismatch, not the number of clusters. Changing K doesn't affect the column count.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use cv::Mat::reshape to flatten the data to 1 column",
      "why_fails": "Flattening to 1 column changes the feature space entirely, causing centers.cols (still original features) to mismatch.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the centers matrix has the same number of columns as the data matrix. For example, if data is a 100x3 matrix (100 samples, 3 features), centers should be Kx3. Initialize centers as: cv::Mat centers(K, data.cols, CV_32FC1); for (int i = 0; i < K; i++) { data.row(rand() % data.rows).copyTo(centers.row(i)); } Then call kmeans(data, K, labels, criteria, attempts, KMEANS_USE_INITIAL_LABELS, centers);",
      "success_rate": 0.9,
      "how": "Ensure the centers matrix has the same number of columns as the data matrix. For example, if data is a 100x3 matrix (100 samples, 3 features), centers should be Kx3. Initialize centers as: cv::Mat centers(K, data.cols, CV_32FC1); for (int i = 0; i < K; i++) { data.row(rand() % data.rows).copyTo(centers.row(i)); } Then call kmeans(data, K, labels, criteria, attempts, KMEANS_USE_INITIAL_LABELS, centers);",
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert data to CV_32F type if it is CV_64F: data.convertTo(data, CV_32F); and ensure centers is also CV_32F.",
      "success_rate": 0.8,
      "how": "Convert data to CV_32F type if it is CV_64F: data.convertTo(data, CV_32F); and ensure centers is also CV_32F.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use KMEANS_RANDOM_CENTERS flag instead of providing initial centers: kmeans(data, K, labels, criteria, attempts, KMEANS_RANDOM_CENTERS);",
      "success_rate": 0.95,
      "how": "Use KMEANS_RANDOM_CENTERS flag instead of providing initial centers: kmeans(data, K, labels, criteria, attempts, KMEANS_RANDOM_CENTERS);",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Ensure the centers matrix has the same number of columns as the data matrix. For example, if data is a 100x3 matrix (100 samples, 3 features), centers should be Kx3. Initialize centers as: cv::Mat centers(K, data.cols, CV_32FC1); for (int i = 0; i < K; i++) { data.row(rand() % data.rows).copyTo(centers.row(i)); } Then call kmeans(data, K, labels, criteria, attempts, KMEANS_USE_INITIAL_LABELS, centers);",
    "Convert data to CV_32F type if it is CV_64F: data.convertTo(data, CV_32F); and ensure centers is also CV_32F.",
    "Use KMEANS_RANDOM_CENTERS flag instead of providing initial centers: kmeans(data, K, labels, criteria, attempts, KMEANS_RANDOM_CENTERS);"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d5/d38/group__core__cluster.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-03-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}