其他分享
首页 > 其他分享> > 第22章 K均值

第22章 K均值

作者:互联网

《OpenCV 轻松入门 面向Python》 学习笔记

函数原型:
retval, bestLabels, centers=cv2.kmeans(data, K, bestLabels, criteria, attempts, flags)
参数:
返回值

举例:

import numpy as np
import cv2
import matplotlib.pyplot as plt

xiaoMI = np.random.randint(0, 50, 60)
daMI = np.random.randint(200, 250, 60)
MI = np.hstack((xiaoMI, daMI))
MI = MI.reshape((120, 1)).astype(np.float32)

criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
flags = cv2.KMEANS_RANDOM_CENTERS
retval, best_Labels, centers = cv2.kmeans(MI, 2, None, criteria, 10, flags)

print(retval)
print(best_Labels)
print(centers)

XM = MI[best_Labels==0]
DM = MI[best_Labels==1]

plt.plot(XM, 'ro')
plt.plot(DM, 'bo')
plt.plot(centers[0], 'rx')
plt.plot(centers[1], 'bx')
plt.show()

在这里插入图片描述

标签:TERM,plt,22,迭代,MI,均值,cv2,centers
来源: https://blog.csdn.net/weixin_37804469/article/details/113728404