SVM opencv machine learning

1.SVM principle

2. Use the SVM handwritten data OCR

  • In kNN we directly use the pixel gray scale value as a feature vector. This time we use a gradient direction histogram Histogram of Oriented Gradients (HOG) as a feature vector.
  • Before calculating HOG we use the second moment of its image deskew (deskew) process.

Code shorthand:

  • cv2.ml.SVM_create()
  • svm.train()
  • svm.save()
  • svm.predict()

Actual:
second moment (1) using an image subjected to deskew (Deskew) Processing

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

(2) using a gradient direction histogram Histogram of Oriented Gradients HOG as a feature vector.

  • Sobel derivative calculation image number X direction and the Y direction. Then the calculated gradient magnitude and direction for each pixel. This gradient is converted into 16-bit integers.
  • The image is divided into four small blocks, calculate their orientation histogram (16 bin) for each of the small block, the gradient magnitude do weights.
  • So that each small box will get a vector containing 16 members. 4 vectors 4 on the formation of small blocks of the image feature vector (containing 64 members)
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

(3) use of a digital handwriting recognition SVM

def opencv_svm(self):
    img = self.img
    # 【1】切割图像
    cells = [np.hsplit(row, 100) for row in np.vsplit(img, 50)]
    # 【2】确定trainData和testData
    train_cells = [i[:50] for i in cells]
    test_cells = [i[50:] for i in cells]
    # 【3】对所有训练图像做抗扭斜处理
    deskewed = [map(deskew, row) for row in train_cells]
    # 【4】计算所有训练图像的hog
    hogdata = [map(hog, row) for row in deskewed]
    # 【5】训练数据的特征值、标签
    trainData = np.float32(hogdata).reshape(-1, 64)
    responses = np.float32(np.repeat(np.arange(10), 250)[:, np.newaxis])
    # 【6】svm训练
    svm = cv2.ml.SVM_create()  # 创建对象
    svm_params = dict(kernel_type=cv2.ml.SVM_LINEAR,
                      svm_type=cv2.ml.SVM_C_SVC,
                      C=2.67, gamma=5.383)
    svm.train(trainData, cv2.ml.ROW_SAMPLE, responses, params=svm_params)
    svm.save('svm_data.dat')
    # 【7】用同样的方法处理测试集
    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)
    # 【8】预测
    result = svm.predict(testData)
    mask = result == responses
    correct = np.count_nonzero(mask)
    print(correct * 100.0 / result.size)

报错

= np.float32 trainData (hogdata) .reshape (-1, 64)
a float or parameters should be Number String

operation result:

94%

Published 155 original articles · won praise 45 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_36622009/article/details/104836345