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

这篇博客介绍了使用Python和OpenCV实现KNN(K-最近邻)算法对手写体字符进行识别的过程。包括读取字符图像,将图像分割为特征集,划分训练集和测试集,创建并训练KNN模型,以及评估模型的识别准确率,最终达到约91%的准确度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤

1、  读入字符图像

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

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

4、  生成标记

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

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

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

代码

<span style="font-family:Courier New;font-size:12px;"># -*- 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,'%'</span>

结果

<span style="font-family:Courier New;font-size:12px;">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 %</span>

图像




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值