所以我必须从页面上识别手写大写字符,所以我在考虑使用SVM分类器。现在,我得到的所有字符都是40x40尺寸,并且只有黑白像素。
我在OpenCv官方site上找到了有关如何进行培训的简短教程,但是在svm.train函数中遇到了此错误。
我试图像本教程中那样在20x20上调整图像的大小,但是我认为我被所有参数所阻塞: 这是错误error
import numpy as np
import cv2 as cv
import sys
import os
bin_n = 16 # Number of bins
affine_flags = cv.WARP_INVERSE_MAP|cv.INTER_LINEAR
hogdata = []
def readImg(path):
# Load an color image in grayscale
img = cv.imread(path,0)
deskew(img, path)
hog(img)
def deskew(img, path):
SZ = 40
m = cv.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 = cv.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
cv.imwrite("out/img.png",img)
hog(img)
def hog(img):
gx = cv.Sobel(img, cv.CV_32F, 1, 0)
gy = cv.Sobel(img, cv.CV_32F, 0, 1)
mag, ang = cv.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
hogdata.append(hist)
if __name__ == "__main__":
for i in range(1,964):
readImg("out/.char_"+str(i)+".png")
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.repeat(np.arange(10),250)[:,np.newaxis]
svm = cv.ml.SVM_create()
svm.setKernel(cv.ml.SVM_LINEAR)
svm.setType(cv.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm.train(trainData, cv.ml.ROW_SAMPLE, responses)
svm.save('svm_data.dat')
我希望能够将火车数据用于识别。