OCR of Hand-written Data using SVM

来源:互联网 发布:财务管家婆软件 编辑:程序博客网 时间:2024/06/05 09:16

1,OCR of Hand-written Digits

在KNN中,我们直接使用像素定义了特征向量,这里使用HOG作为特征向量。

在找到HOG 之前,我们要对图像做抗扭曲,所以首先定义一个函数


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

下面是0图片使用deskew函数的效果。左边是正常图片,右边是抗扭曲后的图片。


接下来,找到每个包的HOG描述,为此找到每个包的在X,y上的sobel描述,然后找到每个包梯度的大小和方向。该梯度是16个整数值。把图像分为4个方格子,计算每个格子的用大小量化方向的直方图(16bins),每个格子可以得到有16个数值的向量,组合起来得到一个64个数值的特征向量。

用这个特征向量训练数据。

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 cv2import numpy as npSZ=20bin_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_LINEARdef 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 testDatatrain_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==responsescorrect = np.count_nonzero(mask)print correct*100.0/result.size


这部分技术准确率可以达到94%,可以尝试不同的参数,提高准确率,后者阅读论文深入研究。


0 0