【问题标题】:opencv knn TypeError: only length-1 arrays can be converted to Python scalarsopencv knn TypeError:只有长度为1的数组可以转换为Python标量
【发布时间】:2019-02-06 16:05:03
【问题描述】:

我正在尝试按照教程http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_ml/py_knn/py_knn_opencv/py_knn_opencv.html 并用cv2.m1.KNearest_create() 替换KNearest,但我得到TypeError: only length-1 arrays can be converted to Python scalars

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

(我正在使用树莓派并按照本教程安装 open cv http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ 随后我 pip 安装了 matplotlib)

【问题讨论】:

    标签: python raspberry-pi opencv3.0 knn


    【解决方案1】:

    参数 cv2.ml.ROW_SAMPLE 丢失,将 knn.find_nearest(test,k=5) 改成下面的代码。这是 openCv3 中新增的,请参考 openCv 官网http://docs.opencv.org/3.0.0/dd/de1/classcv_1_1ml_1_1KNearest.html

        ` knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
          ret, result, neighbours, dist = knn.findNearest(test, k=5)`
    

    【讨论】:

      【解决方案2】:

      您只是缺少一个参数,但我注意到很多人对本教程的这一部分有疑问,所以这里是调整后的整个最后一部分以使用 python3 和现代 openCV 库。

      knn = cv2.ml.KNearest_create()
      knn.train(trainData, cv2.ml.ROW_SAMPLE, responses)
      ret, results, neighbours, dist = knn.findNearest(newcomer, k=5)
      
      print("result: ", results,"\n")
      print("neighbours: ", neighbours,"\n")
      print("distance: ", dist)
      
      plt.show()
      

      【讨论】:

        【解决方案3】:
        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
        knn = cv2.ml.KNearest_create()
        knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
        ret, results, neighbours, dist = knn.findNearest(test, k=5)
        
        #print("result: ", results,"\n")
        #print("neighbours: ", neighbours,"\n")
        #print("distance: ", dist)
        matches = result=test_labels
        correct = np.count_nonzero(matches)
        accuracy = correct*100.0/result.size
        print(accuracy)
        

        【讨论】:

        • 嗨!你有什么问题? Here 一些关于提出好问题的指导。为了让人们可以运行你的代码,你需要提供一份digits.png
        【解决方案4】:

        opencv 的文档说:

        findNearest(...) |找到最近的(样本,k[,结果[, 邻居响应 [, dist]]]) -> retval,结果,邻居响应, ...


        不是knn.find_nearest(test,k=5)

        你可以运行

        help(cv2.ml.KNearest_create())

        然后你会看到。 顺便说一下,opencv 网站上有 loss 错误

        【讨论】:

        • 请格式化您的代码并澄清您的答案。这个答案很难阅读。
        猜你喜欢
        • 2014-09-27
        • 2013-03-15
        • 1970-01-01
        • 2016-08-06
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        相关资源
        最近更新 更多