how to set initial centers of K-means openCV c++

早过忘川 提交于 2019-12-06 11:27:22

The function allows you to directly set the initial labeling, not centers. Fortunately, since k-means alternates between assignment and update steps, you can get the effect you want indirectly.


From the docs:

labels – Input/output integer array that stores the cluster indices for every sample.

KMEANS_USE_INITIAL_LABELS During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of KMEANS_*_CENTERS flag to specify the exact method.

So, what the docs say is that you can set the initial labeling. If you want to do this, in your code

kmeans(samples, 2, labels, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 3, KMEANS_USE_INITIAL_LABELS, centers);

initialize the 3rd parameter to be the input labels (for use in the first iteration).


If you want to get the effect of setting the initial centers, you can do the following:

  1. Decide what the centers are.

  2. Calculate the labeling like the algorithm does in the assignment step.

  3. Pass the resulting labeling to the function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!