【问题标题】:OpenCV kmeans error: (-215) pythonOpenCV kmeans 错误:(-215) python
【发布时间】:2017-11-07 16:09:17
【问题描述】:

错误:错误:(-215) (unsigned)labels[i]

self.cluster[i] 表示一些计算的像素位置。

img = numpy.asarray(img)
Z = img.reshape((-1,3))
Z = numpy.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
#prepare centers
labels = numpy.zeros(k)
for i in range(k):
    labels[i] = self.cluster[i].j * img.shape[0] + self.cluster[i].i
print(Z)
ret, label, center = cv2.kmeans(Z, k, None, criteria, 10, cv2.KMEANS_USE_INITIAL_LABELS, labels)

我已经阅读了其他关于 c++ 中相同错误的帖子,但我无法让它工作。请帮忙!

EDIT1:

import numpy as np
import sys
sys.path.append('/usr/local/lib/python3.5/site-packages')
import cv2

img = cv2.imread('image.jpg')
img = cv2.resize(img, (90,90))
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 4

labels = np.zeros(K)
labels[0] = 1
labels[1] = 100
labels[2] = 500
labels[3] = 1000
print(labels)
print(Z.shape)
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_USE_INITIAL_LABELS, labels)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

您可以单独运行此代码来检查问题所在。通过将图像大小调整为小于 90x90 会导致其崩溃。任何大于 91x91 的作品都可以。谁能解释一下为什么以及如何解决它?

【问题讨论】:

  • 请提供Minimal, Complete, and Verifiable example。我不能直接复制粘贴你的 sn-p 来尝试:它遗漏了一些定义(意思是 kself 等)。提供一个失败图像的例子也会很有帮助。

标签: python opencv k-means


【解决方案1】:

使用cv2.KMEANS_PP_CENTERScv2.KMEANS_RANDOM_CENTERS 而不是cv2.KMEANS_USE_INITIAL_LABELS 时我的问题解决了。

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2016-06-20
    • 2018-04-30
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多