【Python-Opencv】KNN手写体字符识别

步骤

 
  

1、  读入字符图像

2、  将图像中的5000个字符数据分割,保存在numpy数组中。利用像素值作为特征集

3、  将分割后的数组前50列为训练数据,后面50列为测试数据

4、  生成标记

5、  初始化knn训练器,并利用训练数据进行训练

6、  对训练生成的训练器进行测试数据测试

7、  np.savetxt,np.load保存数据

代码

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 17 11:45:19 2015

@author: carp
"""

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

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

print 'split the gray image to 5000 cells, each 20*20 size'
print 'start data..........'
print 'split 100 rows 50 coloumn'
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]
print np.vsplit(gray,50)
#print cells.shape
print 'Make it into a Numpy array. It size will be (50,100,20,20)'
x = np.array (cells)
print x.shape
print 'train data.........'
train = x[:,:50].reshape(-1,400).astype(np.float32)
print train.shape
test = x[:,50:100].reshape(-1,400).astype(np.float32)
print test.shape

print 'create labels for train and test data'
k = np.arange(10)
print k
#print np.repeat(k,250).reshape(2500,1)
train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()
print train_labels.shape
print test_labels.shape

print 'Initiate knn,train data'
knn = cv2.KNearest()
knn.train(train,train_labels)
print 'then test it with test data for k = 5'
ret,result,neigobours,dist = knn.find_nearest(test,k = 5)

print 'check the accuracy of classification'
matches = result == test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100/result.size
print accuracy,'%'

结果

gray
split the gray image to 5000 cells, each 20*20 size
start data..........
split 100 rows 50 coloumn
Make it into a Numpy array. It size will be (50,100,20,20)
(50, 100, 20, 20)
train data.........
(2500, 400)
(2500, 400)
create labels for train and test data
[0 1 2 3 4 5 6 7 8 9]
(2500, 1)
(2500, 1)
Initiate knn,train data
then test it with test data for k = 5
check the accuracy of classification
91 %

图像




你可能感兴趣的:(【Python-Opencv】)