# cv::error: (-215:Assertion failed) centers.cols == data.cols in function 'cv::kmeans'

- **ID:** `opencv/kmeans-initial-centers-mismatch`
- **Domain:** opencv
- **Category:** assertion_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| opencv-4.5.5 | active | — | — |
| opencv-4.8.0 | active | — | — |
| opencv-4.9.0 | active | — | — |

## Workarounds

1. **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);** (90% success)
   ```
   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);
   ```
2. **Convert data to CV_32F type if it is CV_64F: data.convertTo(data, CV_32F); and ensure centers is also CV_32F.** (80% success)
   ```
   Convert data to CV_32F type if it is CV_64F: data.convertTo(data, CV_32F); and ensure centers is also CV_32F.
   ```
3. **Use KMEANS_RANDOM_CENTERS flag instead of providing initial centers: kmeans(data, K, labels, criteria, attempts, KMEANS_RANDOM_CENTERS);** (95% success)
   ```
   Use KMEANS_RANDOM_CENTERS flag instead of providing initial centers: kmeans(data, K, labels, criteria, attempts, KMEANS_RANDOM_CENTERS);
   ```

## Dead Ends

- **Transpose the data matrix using data.t()** — 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. (85% fail)
- **Set the number of clusters K to a smaller value** — The error is about feature dimension mismatch, not the number of clusters. Changing K doesn't affect the column count. (95% fail)
- **Use cv::Mat::reshape to flatten the data to 1 column** — Flattening to 1 column changes the feature space entirely, causing centers.cols (still original features) to mismatch. (90% fail)
