1

我正在尝试在 opencv 库 (kmeans2) 中应用 kmeans 聚类算法,但每个样本点都有超过 4 个值(超过 4 个特征)

当我尝试使用 cvMat 为 kmeans2 准备参数时,每当我打印或为其分配任何值时,它都会引发异常。

这是我尝试过的两种方法

第一种方法

import cv
from numpy import *
a = zeros([20,1,6])
b = cv.fromarray(a)
print b[0,0]
OpenCV Error: One of arguments' values is out of range (The number of channels must be 1, 2, 3 or 4) in cvRawDataToScalar, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 1531
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: The number of channels must be 1, 2, 3 or 4

第二种方法

import cv
from numpy import *
a = cv.CreateMat(20,1,cv.MAKETYPE(cv.CV_32F,6))
print a[0,0]
OpenCV Error: One of arguments' values is out of range (The number of channels must be 1, 2, 3 or 4) in cvRawDataToScalar, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 1531
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: The number of channels must be 1, 2, 3 or 4

为 kmeans 算法创建和操作 cvMat 参数的正确方法是什么?

4

1 回答 1

2

你犯的错误是形成错误维度的矩阵。改变这个

a = zeros([20,1,6])

对此

a = zeros([20,6,1])

还有这个

a = cv.CreateMat(20,1,cv.MAKETYPE(cv.CV_32F,6))

对此

a = cv.CreateMat(20, 6, cv.CV_32F)

看来您误解了文档

samples – 输入样本的浮点矩阵,每个样本一行。

并且在每一列中都有功能。

于 2012-04-30T06:56:36.423 回答