I'm moving my project from Python's libraries to opencv, and I have one big problem.

In Python skylearn, I have kmeans object, which has two useful methods: fit_predict and fit, which both are nescessery for my project.

The problem is I can't find any equivalent in opencv. There is only function cv::kmeans, which is not enough for me.

When I'm using Python skylearn the pseudo code of my program looks like:

kmeans.fit_predict(train_data) // it gaves me labels, and compute centers

kmeans.predict(test_sample) // it gaves me labels for test sample computed from centers which were computed above

Is there any equivalent or way to do the same in opencv?


1 Answer 1


The OpenCV kmeans function is equivalent to fit(). It returns the cluster centers, and you can use this to implement your own predict()function. For a new sample, calculate the distance to each of the cluster centers. The cluster with the lowest distance is the predicted class.

  • $\begingroup$ You're right, I did it and it works like I need. $\endgroup$
    – bielu000
    Commented Dec 17, 2018 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.