Ask Your Question
0

How to calculate labels given centers of K-Means in OpenCV

asked 2017-01-28 04:27:55 -0600

RossFe92 gravatar image

updated 2017-01-28 04:51:41 -0600

Hello, I read this question about how to set initial centers of k-mean on Stackoverflow. I read the answer, but I have no idea how to implement it in C ++. Could anyone give me any suggestions? I'd like to calculate first labels using centers that I already have and pass them to kmeans function with flag KMEANS_USE_INITIAL_LABELS for calculate new centers on new (but similar) data. Data are features of images.

edit retag flag offensive close merge delete

Comments

1

kmeans returns labels, so what is your actual question ? (the SO link is useless here)

please edit, show us , what you have, and what you require !

berak gravatar imageberak ( 2017-01-28 04:36:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-01-28 04:47:41 -0600

LBerger gravatar image

updated 2017-01-28 05:33:05 -0600

I'm not sure to understand your question.

You cannot set initial centers in k-means. But you can set initial labels. It means that if you know intial labels then you know initial centers. You can do like this :

  1. set initial center
  2. compute distance between centers and data. Set data label equal to nearest center
  3. call kmean with falg KMEANS_USE_INITIAL_LABELS

I think you can use this sample : replace Mat points(sampleCount, 1, CV_32FC2), labels; by Mat points(sampleCount, 1, CV_32FC2), labels(sampleCount, 1,CV_32SC1); and in loop

label.rowRange(k*sampleCount/clusterCount, k == clusterCount - 1 ? sampleCount :(k+1)*sampleCount/clusterCount) =k;

I haven't tested..

edit flag offensive delete link more

Comments

Can you suggest me how to do this in c++? Thanks.

RossFe92 gravatar imageRossFe92 ( 2017-01-28 05:03:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-28 04:27:55 -0600

Seen: 662 times

Last updated: Jan 28 '17