2016-05-04 2 views
0

ich das Tutorial http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_ml/py_knn/py_knn_opencv/py_knn_opencv.html und ersetzt KNearest mit cv2.m1.KNearest_create() zu folgen versuche umgewandelt werden, aber ich bin immer TypeError: only length-1 arrays can be converted to Python scalarsopencv knn Typeerror: nur Länge-1-Arrays können zu Python Skalare

import numpy as np 
import cv2 
from matplotlib import pyplot as plt 

img = cv2.imread('digits.png') 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

# Now we split the image to 5000 cells, each 20x20 size 
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)] 

# Make it into a Numpy array. It size will be (50,100,20,20) 
x = np.array(cells) 

# Now we prepare train_data and test_data. 
train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400) 
test = x[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400) 

# Create labels for train and test data 
k = np.arange(10) 
train_labels = np.repeat(k,250)[:,np.newaxis] 
test_labels = train_labels.copy() 

# Initiate kNN, train the data, then test it with test data for k=1 
cv2.m1.KNearest_create() 
knn.train(train,train_labels) 
ret,result,neighbours,dist = knn.find_nearest(test,k=5) 

# Now we check the accuracy of classification 
# For that, compare the result with test_labels and check which are wrong 
matches = result==test_labels 
correct = np.count_nonzero(matches) 
accuracy = correct*100.0/result.size 
print accuracy 

(i eine Himbeere pi bin mit und folgte diesem Tutorial offen cv zu installieren http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ anschließend pip installiert matplotlib) i

Antwort

-1

doc von opencv sagte, dass:

findNearest(...) | findNearest(samples, k[, results[, neighborResponses[, dist]]]) -> retval, results, neighborResponses, ...


nicht knn.find_nearest(test,k=5)

Sie

laufen

help(cv2.ml.KNearest_create())

dann wirst du sehen. übrigens, es gibt losros eros auf opencv website

+1

Bitte formatieren Sie Ihren Code und klären Sie Ihre Antwort. Diese Antwort ist sehr schwer zu lesen. –

Verwandte Themen