# cv2.error: OpenCV(4.9.0) /tmp/opencv-4.9.0/modules/core/src/kmeans.cpp:245: error: (-215:断言失败) N >= K 在函数 'cv::kmeans' 中

- **ID:** `opencv/kmeans-n-less-than-k`
- **领域:** opencv
- **类别:** assertion_error
- **错误码:** `-215`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

提供给 k-means 的数据点数量 (N) 少于请求的聚类数量 (K)，导致无法进行聚类。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |
| 4.10.0 | active | — | — |
| 5.0.0 | active | — | — |

## 解决方案

1. ```
   Reduce K to be less than or equal to N: `k = min(k, len(data))` before calling kmeans.
   ```
2. ```
   Collect more data points to ensure N >= K, e.g., by increasing the sample size in data acquisition.
   ```
3. ```
   Add a check before kmeans: `if len(samples) < k: raise ValueError('Not enough samples for k-means')`.
   ```

## 无效尝试

- **** — Duplicating points creates artificial clusters and does not solve the fundamental issue of insufficient unique data. (80% 失败率)
- **** — More iterations do not help when N < K; the algorithm cannot proceed with fewer points than clusters. (95% 失败率)
- **** — The assertion fails before any distance computation; the error is in the input validation. (90% 失败率)
