Python+OpenCV:基于SVM手写数据OCR(OCR of Hand-written Data using SVM)
dsize = 20
affine_flags = lmc_cv.WARP_INVERSE_MAP | lmc_cv.INTER_LINEAR
bin_number = 16
####################################################################################################
# deskew the image using its second order moments.
def lmc_cv_deskew_second_order_moment(image):
moment = lmc_cv.moments(image)
if abs(moment['mu02']) < 1e-2:
return image.copy()
skew = moment['mu11'] / moment['mu02']
transformation_matrix = np.float32([[1, skew, -0.5 * dsize * skew], [0, 1, 0]])
image = lmc_cv.warpAffine(image, transformation_matrix, (dsize, dsize), flags=affine_flags)
return image
####################################################################################################
# HOG Descriptor.
def lmc_cv_hog_svm(image):
"""
函数功能: We find Sobel derivatives of each cell in X and Y direction.
Then find their magnitude and direction of gradient at each pixel.
This gradient is quantized to 16 integer values. Divide this image to four sub-squares.
For each sub-square, calculate the histogram of direction (16 bins) weighted with their magnitude.
So each sub-square gives you a vector containing 16 values.
Four such vectors (of four sub-squares) together gives us a feature vector containing 64 values.
This is the feature vector we use to train our data.
"""
gx = lmc_cv.Sobel(image, lmc_cv.CV_32F, 1, 0)
gy = lmc_cv.Sobel(image, lmc_cv.CV_32F, 0, 1)
mag, ang = lmc_cv.cartToPolar(gx, gy)
# quantizing binvalues in (0...16)
bins = np.int32(bin_number * 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_number) for b, m in zip(bin_cells, mag_cells)]
# hist is a 64 bit vector
hist = np.hstack(hists)
return hist
####################################################################################################
# 基于SVM手写数据OCR(OCR of Hand-written Data using SVM)
def lmc_cv_svm_ocr_handwritten_digits():
"""
函数功能: 基于KNN手写数据OCR(OCR of Hand-written Data using kNN).
"""
image = lmc_cv.imread('D:/99-Research/TestData/image/digits.png', lmc_cv.IMREAD_GRAYSCALE)
if image is None:
raise Exception("we need the digits.png image from samples/data here !")
cells = [np.hsplit(row, 50) for row in np.vsplit(image, 50)]
# First half is train_data, remaining is test_data
train_cells = [i[:25] for i in cells]
test_cells = [i[25:] for i in cells]
deskewed = [list(map(lmc_cv_deskew_second_order_moment, row)) for row in train_cells]
hogdata = [list(map(lmc_cv_hog_svm, row)) for row in deskewed]
train_data = np.float32(hogdata).reshape(-1, 64)
responses = np.repeat(np.arange(10), 125)[:, np.newaxis]
svm = lmc_cv.ml.SVM_create()
svm.setKernel(lmc_cv.ml.SVM_LINEAR)
svm.setType(lmc_cv.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm.train(train_data, lmc_cv.ml.ROW_SAMPLE, responses)
svm.save('svm_data.dat')
deskewed = [list(map(lmc_cv_deskew_second_order_moment, row)) for row in test_cells]
hogdata = [list(map(lmc_cv_hog_svm, row)) for row in deskewed]
test_data = np.float32(hogdata).reshape(-1, bin_number * 4)
result = svm.predict(test_data)[1]
mask = result == responses
correct = np.count_nonzero(mask)
print("accuracy: {}\n".format(correct * 100.0 / result.size))
【结果】
accuracy: 92.48
This particular technique gave me nearly 93% accuracy.
You can try different values for various parameters of SVM to check if higher accuracy is possible.
Below image shows above deskew function applied to an image of zero.
Left image is the original image and right image is the deskewed image.