将OpenCV图像传递给KNearest的find_nearest [英] Pass in OpenCV image to KNearest's find_nearest

查看:541
本文介绍了将OpenCV图像传递给KNearest的find_nearest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在跟踪示例此处通过使用kNN分类训练OpenCV来为OCR设置Python.我遵循第一个示例,并生成了一个knn_data.npz,用于存储训练数据和训练标签以供以后使用.我现在想做的是回忆训练数据并将其应用于其中具有单个字符的OpenCV图像:

I've been following the examples here on setting up Python for OCR by training OpenCV using kNN classification. I followed the first example and generated a knn_data.npz that stores the training data and the training labels for later. What I'm trying to do now is to recall that training data and apply it to an OpenCV image that has a single character inside of it:

# Load training data
trainingData = np.load('knn_data.npz')
train = trainingData['train'] 
trainLabels = trainingData['train_labels']

knn = cv2.KNearest()
knn.train(train, trainLabels)

letter = cv2.imread('letter.png')
letter = cv2.cvtColor(letter, cv2.COLOR_BGR2GRAY)
print letter.shape
letter = letter.reshape((1,100))
letter = np.float32(letter)
print letter.shape

ret, result, neighbors, dist = knn.find_nearest(letter, k=5)
print result

'letter.png'图像是10x10图像,因此调整大小是非常安全的,并且numpy成功将图像调整为一维形状的数组(1,100).但是,当我尝试将其传递给knn.find_nearest(...)函数时,出现一条错误,提示使用浮点矩阵:

The 'letter.png' image is a 10x10 image so it's perfect safe to resize and numpy successfully resizes the image to a 1-dimensional array of shape (1, 100). However, when I try to pass this into the knn.find_nearest(...) function, I get an error that says to use float-point matrices:

OpenCV Error: Bad argument (Input samples must be floating-point matrix (<num_samples>x<var_count>)) in find_nearest, file /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/knearest.cpp, line 370
Traceback (most recent call last):
  File "sudoku.py", line 103, in <module>
    ret, result, neighbors, dist = knn.find_nearest(letter, k=5)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/knearest.cpp:370: error: (-5) Input samples must be floating-point matrix (<num_samples>x<var_count>) in function find_nearest

但是,我重塑了我的图像,使其占据了一行并转换为浮动图像,因此我不确定为什么会出现此错误.有什么建议?

However, I reshaped my image so that it occupies a single row and converted it into a float so I'm not entirely sure why this error is coming up. Any suggestions?

推荐答案

我刚刚意识到了为什么会这样.为了使kNN分类起作用,测试数据(在这种情况下为单个字母)必须具有与训练数据完全相同的特征数量.在这种情况下,我的训练数据使用20x20图像,因此行向量的长度为400,但我的字母仅为10x10.

I just realized why this is happening. For the kNN classification to work, the test data (or single letter in this case) needs to have the exact same number of features as the training data. In this case, my training data used 20x20 images so the row vector had a length of 400, but my letter is only 10x10.

我通过将字母放大到20x20并将其展平为大小为400(20 ^ 2)的行向量来解决此问题.

I fixed this by scaling up my letter to 20x20 and flattening it into a row vector of size 400 (20^2).

这也不一定必须按行向量进行.可以将测试数据格式化为与培训数据完全相同的矩阵,其中每行包含一个样本,在这种情况下为字母.然后find_nearest将返回一个矩阵,其中每一行对应于测试数据.

This doesn't have to work by row vectors necessarily either. The test data can be formatted as a matrix exactly like the training data, where each row contains a sample, in this case a letter. Then find_nearest will return a matrix where each row corresponds to the test data.

这篇关于将OpenCV图像传递给KNearest的find_nearest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆