本章节的主要内容是:基于Python和OpenCV的机器学习部分中的支持向量机(SVM)和最近邻算法(KNN)进行手写数据训练测试识别。
以下代码均在python3.6,opencv4.2.0环境下试了跑一遍,可直接运行。
1、基于OpenCV使用SVM进行手写数据训练测试识别
代码语言:javascript复制# encoding:utf-8
import cv2
import numpy as np
SZ = 20
bin_n = 16
affine_flags = cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR
def deskew(img):
# 计算图像中的中心矩(最高到三阶)
m = cv2.moments(img)
if abs(m['mu02']) < 1e-2:
return img.copy()
skew = m['mu11'] / m['mu02']
M = np.float32([[1, skew, -0.5 * SZ * skew], [0, 1, 0]])
# 图像的平移,参数:输入图像、变换矩阵、变换后的大小
img = cv2.warpAffine(img, M, (SZ, SZ), flags=affine_flags)
return img
# 使用方向梯度直方图HOG作为特征向量,计算图像的 X 方向和 Y 方向的 Sobel 导数
def hog(img):
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy) # 笛卡尔坐标转换为极坐标, → magnitude, angle
bins = np.int32(bin_n * ang / (2 * np.pi))
bin_cells = bins[:10, :10], bins[10:, :10], bins[:10, 10:], bins[10:, 10:]
mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
return hist
# 将大图分割为小图,使用每个数字的前250个作为训练数据,后250个作为测试数据
img = cv2.imread('./digits.png', 0)
cells = [np.hsplit(row, 100) for row in np.vsplit(img, 50)]
# 第一部分是训练数据,剩下的是测试数据
train_cells = [i[:50] for i in cells]
test_cells = [i[50:] for i in cells]
deskewed = [list(map(deskew, row)) for row in train_cells]
hogdata = [list(map(hog, row)) for row in deskewed]
print(hogdata)
trainData = np.float32(hogdata).reshape(-1, 64)
labels = np.repeat(np.arange(10), 250)[:, np.newaxis]
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm.train(trainData, cv2.ml.ROW_SAMPLE, labels)
svm.save('svm_data.dat')
deskewed = [list(map(deskew, row)) for row in test_cells]
hogdata = [list(map(hog, row)) for row in deskewed]
testData = np.float32(hogdata).reshape(-1, bin_n * 4)
ret, result = svm.predict(testData)
mask = result == labels
correct = np.count_nonzero(mask)
print(correct * 100.0 / len(result),'%')
测试识别率:93.8 %
2、基于OpenCV使用KNN进行手写数据训练测试识别
代码语言:javascript复制# encoding:utf-8
import cv2 as cv
import numpy as np
img = cv.imread('./digits.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# 将图像分割为5000个单元格,每个单元格为20x20
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]
# 使其成为一个Numpy数组,大小将是(50,100,20,20)
x = np.array(cells)
# 准备train_data和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)
# 为训练和测试数据创建标签
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()
# 初始化kNN,训练数据,然后使用k=5的测试数据对其进行测试
knn = cv.ml.KNearest_create()
knn.train(train, cv.ml.ROW_SAMPLE, train_labels)
knn.save('knn_data.dat')
ret,result,neighbours,dist = knn.findNearest(test, k=5)
print( test )
print( result )
# 检查分类的准确性,将结果与test_labels进行比较
matches = result == test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size
print( accuracy,'%' )
测试识别率:91.76 %
3、基于OpenCV使用KNN进行手写英文字母训练测试识别
代码语言:javascript复制# encoding:utf-8
import cv2 as cv
import numpy as np
# 加载数据,转换器将字母转换为数字
data= np.loadtxt('letter-recognition.data',
dtype= 'float32',
delimiter = ',',
converters= {0: lambda ch: ord(ch)-ord('A')})
# 将数据分为训练数据和测试数据
train, test = np.vsplit(data,2)
# 将训练数据和测试数据分别拆分为特征和标签
train_labels, trainData = np.hsplit(train,[1])
test_labels, testData = np.hsplit(test,[1])
# 初始化KNN, 记性训练测试
knn = cv.ml.KNearest_create()
knn.train(trainData, cv.ml.ROW_SAMPLE, train_labels)
knn.save('knn_english.dat')
ret, result, neighbours, dist = knn.findNearest(testData, k=5)
print( testData )
print( result )
correct = np.count_nonzero(result == test_labels)
accuracy = correct*100.0/10000
print( accuracy,'%')
测试识别率:93.06 %
digits.png:
以上内容如有错误或者需要补充的,请留言!