我正在尝试在 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 参数的正确方法是什么?
最佳答案
您犯的错误是形成了错误维度的矩阵。改变这个
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 – Floating-point matrix of input samples, one row per sample.
并且在每一列中您都有这些特征。
关于python - 超过 4 个 channel 的 Opencv 聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376974/