Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cv2.kmeans在Python中的用法_Python_Opencv - Fatal编程技术网

cv2.kmeans在Python中的用法

cv2.kmeans在Python中的用法,python,opencv,Python,Opencv,我正在考虑使用OpenCV的Kmeans实现,因为它说要更快 现在我使用的是cv2包和函数kmeans 我无法理解参考资料中的参数说明: Python: cv2.kmeans(data, K, criteria, attempts, flags[, bestLabels[, centers]]) → retval, bestLabels, centers samples – Floating-point matrix of input samples, one row per sample. c

我正在考虑使用OpenCV的Kmeans实现,因为它说要更快

现在我使用的是cv2包和函数kmeans

我无法理解参考资料中的参数说明:

Python: cv2.kmeans(data, K, criteria, attempts, flags[, bestLabels[, centers]]) → retval, bestLabels, centers
samples – Floating-point matrix of input samples, one row per sample.
clusterCount – Number of clusters to split the set by.
labels – Input/output integer array that stores the cluster indices for every sample.
criteria – The algorithm termination criteria, that is, the maximum number of iterations and/or the desired accuracy. The accuracy is specified as criteria.epsilon. As soon as each of the cluster centers moves by less than criteria.epsilon on some iteration, the algorithm stops.
attempts – Flag to specify the number of times the algorithm is executed using different initial labelings. The algorithm returns the labels that yield the best compactness (see the last function parameter).
flags –
Flag that can take the following values:
KMEANS_RANDOM_CENTERS Select random initial centers in each attempt.
KMEANS_PP_CENTERS Use kmeans++ center initialization by Arthur and Vassilvitskii [Arthur2007].
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.
centers – Output matrix of the cluster centers, one row per each cluster center.
参数
flags[,bestLabels[,centers]])
的意思是什么?那么他的一个呢:
→ 返回、最佳标签、中心

这是我的密码:

import cv, cv2
import scipy.io
import numpy

# read data from .mat file
mat = scipy.io.loadmat('...')
keys = mat.keys()
values = mat.viewvalues()

data_1 = mat[keys[0]]
nRows = data_1.shape[1] 
nCols = data_1.shape[0]
samples = cv.CreateMat(nRows, nCols, cv.CV_32FC1)
labels = cv.CreateMat(nRows, 1, cv.CV_32SC1)
centers = cv.CreateMat(nRows, 100, cv.CV_32FC1)
#centers = numpy.

for i in range(0, nCols):
    for j in range(0, nRows):
        samples[j, i] = data_1[i, j]


cv2.kmeans(data_1.transpose,
                              100,
                              criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 0.1, 10),
                              attempts=cv2.KMEANS_PP_CENTERS,
                              flags=cv2.KMEANS_PP_CENTERS,
)
我遇到这样的错误:

flags=cv2.KMEANS_PP_CENTERS,
TypeError: <unknown> is not a numpy array
flags=cv2.KMEANS\u PP\u CENTERS,
TypeError:不是numpy数组

我应该如何理解参数列表和cv2.kmeans的用法?谢谢

关于此功能的文档几乎找不到。我有点匆忙地编写了下面的Python代码,但它可以在我的机器上运行。它生成两个具有不同均值的多变量高斯分布,然后使用cv2.kmeans()对其进行分类。您可以参考以了解一些参数

处理进口:

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()
在这里您可以看到原始点:

以下是分类后的要点:


我希望这个答案可以帮助你,它不是一个完整的k-means指南,但它至少会告诉你如何将参数传递给OpenCV。

这里的问题是你的
数据。transpose
不是一个numpy数组

OpenCV 2.3.1和更高版本的python绑定不会将除
numpy数组
之外的任何内容作为图像/数组参数。因此,
data\u 1.transpose
必须是一个numpy数组

通常,OpenCV中的所有点的类型都是
numpy.ndarray

例如

其中数组的每个元素都是

array([[100., 433.]], dtype=float32)
这个数组的元素是

array([100., 433.], dtype=float32)

值得注意的是,该示例似乎比OpenCV文档中提供的python示例工作得更好。提供的链接已断开,仅供更新。谢谢
array([100., 433.], dtype=float32)