# cv2.error: OpenCV(4.9.0) /tmp/opencv-4.9.0/modules/core/src/kmeans.cpp:245: error: (-215:Assertion failed) N >= K in function 'cv::kmeans'

- **ID:** `opencv/kmeans-n-less-than-k`
- **Domain:** opencv
- **Category:** assertion_error
- **Error Code:** `-215`
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

The number of data points (N) provided to k-means is less than the requested number of clusters (K), making clustering impossible.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |
| 4.10.0 | active | — | — |
| 5.0.0 | active | — | — |

## Workarounds

1. **Reduce K to be less than or equal to N: `k = min(k, len(data))` before calling kmeans.** (95% success)
   ```
   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.** (85% success)
   ```
   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')`.** (90% success)
   ```
   Add a check before kmeans: `if len(samples) < k: raise ValueError('Not enough samples for k-means')`.
   ```

## Dead Ends

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