当运行下面的代码时,我得到一个TypeError,它说:
“文件”_vq.pyx公司“,第342行,英寸scipy.cluster._vq.update_cluster_表示 TypeError:不支持float或double以外的类型“
from PIL import Image
import scipy, scipy.misc, scipy.cluster
NUM_CLUSTERS = 5
im = Image.open('d:/temp/test.jpg')
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
vecs, dist = scipy.cluster.vq.vq(ar, codes)
counts, bins = scipy.histogram(vecs, len(codes))
peak = codes[scipy.argmax(counts)]
print 'Most frequent color: %s (#%s)' % (peak, ''.join(chr(c) for c in peak).encode('hex'))
我不知道怎么解决这个问题。在
更新:
完全回溯:
Traceback (most recent call last):
File "...\temp.py", line 110, in <module>
codes, dist = scipy.cluster.vq.kmeans2(ar, NUM_CLUSTERS)
File "...\site-packages\scipy\cluster\vq.py", line 642, in kmeans2
new_code_book, has_members = _vq.update_cluster_means(data, label, nc)
File "_vq.pyx", line 342, in scipy.cluster._vq.update_cluster_means
TypeError: type other than float or double not supported
正在做:
您将看到,您调用kmeans的数据类型为uint8。在
由于kmeans在理论上是定义在d维的实向量上,scipy也不喜欢它(如错误中所示)!在
所以就这么做吧:
^{pr2}$这样的强制转换使我的示例一直运行到打印,这也需要更改以反映给定的类型。在
相关问题 更多 >
编程相关推荐