手写数字的OCR
在kNN里,我们直接使用像素亮度来作为特征向量,这次我们会使用方向梯度的直方图(HOG)作为特征向量。
这里,在找HOG之前,我们使用图像的二阶矩模型来抗色偏。所以我们首先定义一个函数deskew()取一个数字图像并对他抗色偏。下面是deskew()函数:
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
下面的图片显示了上面的deskew函数应用在数字0的图像上。左边的图像是原始图像,右边的图像是抗色偏之后的图像
接着我们得找到每个单元的HOG描述子。为了这个,我们找每个单元在X和Y方向的Sobel导数。然后找他们在每个像素的等级和方向的梯度。这个梯度是量化到16整数值得。把这个图像分成四个子部分。对于每个子部分,计算他们级别权重的方向直方图(16bins)。所以每个子块给你一个包含16值得向量,4个这样的向量(四个子块)在一起给我们包含了64个值的特征向量。这个是我们用来训练我们数据的特征向量。
def hog(img):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)# quantizing binvalues in (0...16)
bins = np.int32(bin_n*ang/(2*np.pi))# Divide to 4 sub-squares
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个做测试,全部代码如下:
import cv2
import numpy as npSZ=20
bin_n = 16 # Number of binssvm_params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC, C=2.67, gamma=5.383 )
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 imgdef 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)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
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) # hist is a 64 bit vector
return histimg = cv2.imread('digits.png',0)
cells = [np.hsplit(row,100) for row in np.vsplit(img,50)]
# First half is trainData, remaining is testData
train_cells = [ i[:50] for i in cells ]
test_cells = [ i[50:] for i in cells]###### Now training ########################
deskewed = [map(deskew,row) for row in train_cells]
hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')###### Now testing ########################
deskewed = [map(deskew,row) for row in test_cells]
hogdata = [map(hog,row) for row in deskewed]
testData = np.float32(hogdata).reshape(-1,bin_n*4)
result = svm.predict_all(testData)####### Check Accuracy ########################
mask = result==responses
correct = np.count_nonzero(mask)
print correct*100.0/result.size
这个技术可以给我们94%的准确率,你可以尝试不同的数据用不同的SVM参数来检查是否可能更高准确率。或者可以阅读者个领域的论文来自己尝试实现他们。