在本章中,将学习:
在KNN中,直接使用像素强度作为特征向量;在SVM中,将使用方向梯度直方图(HOG)作为特征向量。
在这里,在找到HOG特征的之前,使用其二阶矩来进行图像倾斜校准。所以首先定义一个函数deskew()
,输入数字图像并校准它。以下是deskew()
函数:
import cv2
import numpy as np
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
SZ = 20
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导数。然后在每个像素处找到它们的幅度和梯度方向,并将该梯度量化为16个整数值,将此图像划分为四个子方块。对于每个子正方形,计算加权方向(16个bin)的直方图。所以每个子正方形都提供包含16个值的向量。四个这样的载体(四个子方块)合在一起获得了一个包含64个值的特征向量。这是用于训练数据的特征向量。
bin_n = 16 # Number of bins
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)
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 hist
最后,类似于KNN算法中,首先将大数据集分成单个单元格。对于每个数字,预留250个单元格作为训练数据,剩余的250个数据进行测试。完整代码如下:
#!/usr/bin/env python
import cv2
import numpy as np
SZ = 20
bin_n = 16 # Number of bins
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
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)
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 hist
img = 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]
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)
responses = 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, responses)
# save train model
svm.save('svm_data.dat')
# predict test data and calc accuracy
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)
result = svm.predict(testData)[1]
mask = result==responses
correct = np.count_nonzero(mask)
print("acc:", correct*100.0/result.size)
# predict single
predict_flag = svm.predict(testData[0].reshape(-1, 64)) #(0.0, array([[0.]], dtype=float32))
label_flag = responses[0] # array([0])
predict_flag == label_flag # array([ True, True])