本文介绍了带有OpenCV 3.0的CV2中的KNN train()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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 :

I'm trying to run k-nearest neighbours using cv2 (python 2.7) and opencv 3.0. I've replicated the same error message using code like http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_knn/py_knn_understanding/py_knn_understanding.html:

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()

我为OpenCV 3修改了行 knn = cv2.ml.KNearest_create(),但随后的行产生了错误"TypeError:只有length-1数组可以转换为Python标量"和我不知道应该使用什么训练功能.

I modified the line knn = cv2.ml.KNearest_create() for OpenCV 3, but the subsequent line produces an error "TypeError: only length-1 arrays can be converted to Python scalars" and I can't figure out what I should be using for the train function.

推荐答案

您正在为KNN算法传递错误的数组长度....瞥了一眼您的代码,我发现您错过了cv2.ml.ROW_SAMPLE参数knn.train函数,传递此参数会将整个行的数组长度视为1.因此,您的更正代码如下:

You are passing wrong length of array for KNN algorithm....glancing at your code, i found that you have missed the cv2.ml.ROW_SAMPLE parameter in knn.train function, passing this parameter considers the length of array as 1 for entire row. thus your corrected code would be as below:

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

trainData = np.random.randint(0,100,(51,2)).astype(np.float32)
responses = np.random.randint(0,2,(51,1)).astype(np.float32)

red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^')
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s')


newcomer = np.random.randint(0,100,(5,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')

knn = cv2.ml.KNearest_create()
knn.train(trainData,cv2.ml.ROW_SAMPLE,responses)
ret, results, neighbours, dist = knn.findNearest(newcomer, 3)

print ("results: ", results,"\n")
print ("neighbours: ", neighbours,"\n")
print ("distances: ", dist)

plt.show()

这是我从中得到的结果....

Here is the result which i got from it....

这篇关于带有OpenCV 3.0的CV2中的KNN train()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 03:56