有关此功能的文档几乎不可能找到。我有点匆忙地编写了以下 Python 代码,但它可以在我的机器上运行。它生成两个具有不同均值的多元高斯分布,然后使用 cv2.kmeans() 对它们进行分类。您可以参考这篇博文 http://www.aishack.in/tutorials/kmeans-clustering-opencv/了解参数。
处理进口:
import cv
import cv2
import numpy as np
import numpy.random as r
生成一些随机点并适当地调整它们的形状:
samples = cv.CreateMat(50, 2, cv.CV_32FC1)
random_points = r.multivariate_normal((100,100), np.array([[150,400],[150,150]]), size=(25))
random_points_2 = r.multivariate_normal((300,300), np.array([[150,400],[150,150]]), size=(25))
samples_list = np.append(random_points, random_points_2).reshape(50,2)
random_points_list = np.array(samples_list, np.float32)
samples = cv.fromarray(random_points_list)
绘制分类前后的点:
blank_image = np.zeros((400,400,3))
blank_image_classified = np.zeros((400,400,3))
for point in random_points_list:
cv2.circle(blank_image, (int(point[0]),int(point[1])), 1, (0,255,0),-1)
temp, classified_points, means = cv2.kmeans(data=np.asarray(samples), K=2, bestLabels=None,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1, 10), attempts=1,
flags=cv2.KMEANS_RANDOM_CENTERS) #Let OpenCV choose random centers for the clusters
for point, allocation in zip(random_points_list, classified_points):
if allocation == 0:
color = (255,0,0)
elif allocation == 1:
color = (0,0,255)
cv2.circle(blank_image_classified, (int(point[0]),int(point[1])), 1, color,-1)
cv2.imshow("Points", blank_image)
cv2.imshow("Points Classified", blank_image_classified)
cv2.waitKey()
在这里你可以看到原始点:
Here are the points after they have been classified:
![Points after classification](https://i.stack.imgur.com/EXEsv.png)
我希望这个答案可以帮助你,它不是 k-means 的完整指南,但它至少会告诉你如何将参数传递给 OpenCV。