2017-08-10 3 views
1

私はhereのコードを見ています。私はそれを実行したり、開発者と連絡をとることができません。私はそれを動作させるためにいくつかの変更を行いましたが、私はエラーを取得していますなぜ私は把握カント:リストのインデックスとしてcv2.kmeansのラベルを使用するにはどうすればよいですか?

TypeError: only integer scalar arrays can be converted to a scalar index

私は種類を印刷していると私はそのタイプが<class 'list'>であることに気付きました。私はさまざまなリソースを検索しようとしましたが、問題の原因を理解できません。

# applying kmeans on colwise white pixel counts 
z = np.float32(a) 
# Define criteria = (type, max_iter = 10 , epsilon = 1.0) 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 12, 0.0) 
# Set flags (Just to avoid line break in the code) 
flags = cv2.KMEANS_RANDOM_CENTERS 
K = 5 
# Apply KMeans 
compactness,labels,centers = cv2.kmeans(z,K,None,criteria,10,flags) 

# define colors to be used 
blue = np.array([255,0,0]) 
green = np.array([0,255,0]) 
red = np.array([0,0,255]) 
tur = np.array([255,255,0]) 
purple = np.array([255,0,255]) 
yellow = np.array([0,255,255]) 

colors = [red,green,blue,purple,tur] 
imcpy = np.empty_like(img) 
np.copyto(imcpy,img) 

# draw colors according to cluster labels 
for i in range(len(a)): 
     row,col = a[i][0],a[i][1] 
     thisColor = colors[labels[i]] 
     imcpy[row][col] = thisColor 

for c in range(len(centers)): 
     Crow,Ccol = centers[c][0],int(centers[c][1]) 
     for row in range(height): 
       imcpy[row][Ccol] = np.array([255,255,255]) 

# characters 
charHeight, charWidth = height,45 
char_imgs = [] 
centers = sorted(centers,key=lambda x: x[1]) 
for c in range(len(centers)): 
     Crow,Ccol = centers[c][0],int(centers[c][1]) 
     x1 = (Ccol-int(charWidth/2)) if ((Ccol-int(charWidth/2)) > 0) else 0 
     x2 = (Ccol+int(charWidth/2)) if ((Ccol+int(charWidth/2)) < width) else width 
     char_imgs.append(charcpy[0:height , x1:x2]) 

for x in range(len(char_imgs)): 
     opPath = outDIR+"/"+ chars[x] 
     if not os.path.exists(opPath): 
       os.makedirs(opPath) 
     cv2.imwrite(opPath +"/" + str(x) +"_" + file, char_imgs[x]) 

エラー:あなたがインデックスにPythonのリストをしようとした場合、この例外が発生し

/prep# python3 prepdata.py ../images/train_120.png 
Traceback (most recent call last): File "prepdata.py", line 95, in <module> thisColor = colors[labels[i]] 
TypeError: only integer scalar arrays can be converted to a scalar index 
+0

エラーメッセージ全体を印刷できますか。 – Hannebambel

答えて

0

(あなたcolors)numpyの配列(それはちょうど、整数だ場合でも)と:

import numpy as np 

color = ['red', 'blue', 'yellow'] 
labels = np.array([[1, 2], [2, 3]]) 
color[labels[1]] 

TypeError: only integer scalar arrays can be converted to a scalar index

cv2.kmeansの例を見ると、平らにする必要があることが明確になります(ravel

関連する問題