# Cv2 kmeans with initial labels

**URL:** <https://forum.opencv.org/t/cv2-kmeans-with-initial-labels/13761>\
**Category:** Python\
**Created:** [July 4, 2023, 8:35pm UTC](https://forum.opencv.org/t/cv2-kmeans-with-initial-labels/13761 "2023-07-04T20:35:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arale\_Mononoko](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/arale_mononoko/32/8224_2.png) [@Arale\_Mononoko](https://forum.opencv.org/u/Arale_Mononoko)\
**Post date:** [July 4, 2023, 8:35pm UTC](https://forum.opencv.org/t/cv2-kmeans-with-initial-labels/13761/1 "2023-07-04T20:35:08Z")

</div>

Hi!

I’m trying to use cv2.kmenas with initial values, but I don’t succeed in passing the right parameters.

After reading the documentation I’ve assumed that parameter centers is the same as the centers obtained as a return from calling the function.

Thus I do `ret, label, center_obtingut = cv2.kmeans(Z,16,None,criteria, 1,cv2.KMEANS_RANDOM_CENTERS)` and the funtion runs without problem.

Then I want to use it again with the centers obtained (center\_obtingut), and I do `ret, label, center = cv2.kmeans(Z,16,None,criteria, 1,cv2.KMEANS_USE_INITIAL_LABELS, centers=center_obtingut)` but I get the error:

`error: OpenCV(4.7.0) /home/abuild/rpmbuild/BUILD/opencv-4.7.0/modules/core/src/kmeans.cpp:259: error: (-215:Assertion failed) (unsigned)_labels.at<int>(i) < (unsigned)K in function 'kmeans'`.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 4, 2023, 10:35pm UTC](https://forum.opencv.org/t/cv2-kmeans-with-initial-labels/13761/2 "2023-07-04T22:35:24Z")

</div>

please [look at the kmeans() docs again](https://docs.opencv.org/4.x/d5/d38/group __core__ cluster.html#ga9a34dc06c6ec9460e90860f15bcd2f88)

you can set initial _labels_, but NOT _centers_  
(which internally are calculated _from_ the labels as last step of kmeans())

---

<div class="post-metadata">

**Author:** ![Arale\_Mononoko](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/arale_mononoko/32/8224_2.png) [@Arale\_Mononoko](https://forum.opencv.org/u/Arale_Mononoko)\
**Post date:** [July 6, 2023, 7:51am UTC](https://forum.opencv.org/t/cv2-kmeans-with-initial-labels/13761/3 "2023-07-06T07:51:53Z")

</div>

Oh! I see.  
I was really surprised to see bestLabels as parameter, but now I understand why the flag `cv2.KMEANS_USE_INITIAL_LABELS` does not include the name centers. So, when you use kmeans with this flag, you need to provide the initial labels.

With this way of working, I assume that the parameter centers is also needed with the initial\_labels to compute the first attempt.

Finally, if you had an image already clustered and then you add a new element in the image and you want to use the previous clustering, the best option is to use Initial\_Label with PP\_Centers.

Thanks a lot berak for the answer, I should read the documentation more thoroughly.
