私は、cv2(python 2.7)とopencv 3.0を使ってk最近隣を走ろうとしています。私はhttp://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_knn/py_knn_understanding/py_knn_understanding.htmlようなコードを使用して、同じエラーメッセージを複製しました。私はOpenCVの3の行KNN = cv2.ml.KNearest_create()修飾が、後続のラインがエラー「はTypeErrorを生成 opencv 3.0でcv2のKNN train()
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Feature set containing (x,y) values of 25 known/training data
trainData = np.random.randint(0,100,(25,2)).astype(np.float32)
# Labels each one either Red or Blue with numbers 0 and 1
responses = np.random.randint(0,2,(25,1)).astype(np.float32)
# Take Red families and plot them
red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^')
# Take Blue families and plot them
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s')
plt.show()
newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')
#The following line is modified for OpenCV 3.0
knn = cv2.ml.KNearest_create()
knn.train(trainData,responses)
ret, results, neighbours ,dist = knn.find_nearest(newcomer, 3)
print "result: ", results,"\n"
print "neighbours: ", neighbours,"\n"
print "distance: ", dist
plt.show()
:のみ長さ1の配列はPythonのスカラーに変換することができます "と私は列車の機能のために何を使用すべきか理解できません。
それは完璧に動作しますので、私は、この答えをupvoteしたいです。しかし、私は十分な評判がないので、私はできません。ありがとうございました!私はこれを理解しようとするのに時間をかけすぎました。 –
答えが正しいとマークしている – AdityaIntwala