如果您已经在使用 opencv,我建议您使用内置的 svm 实现,python 中的训练/保存/加载如下。 c++ 有相应的 api 在大约相同数量的代码中执行相同的操作。它还有“train_auto”来寻找最佳参数
import numpy as np
import cv2
samples = np.array(np.random.random((4,5)), dtype = np.float32)
labels = np.array(np.random.randint(0,2,4), dtype = np.float32)
svm = cv2.SVM()
svmparams = dict( kernel_type = cv2.SVM_LINEAR,
svm_type = cv2.SVM_C_SVC,
C = 1 )
svm.train(samples, labels, params = svmparams)
testresult = np.float32( [svm.predict(s) for s in samples])
print samples
print labels
print testresult
svm.save('model.xml')
loaded=svm.load('model.xml')
并输出
#print samples
[[ 0.24686454 0.07454421 0.90043277 0.37529686 0.34437731]
[ 0.41088378 0.79261768 0.46119651 0.50203663 0.64999193]
[ 0.11879266 0.6869216 0.4808321 0.6477254 0.16334397]
[ 0.02145131 0.51843268 0.74307418 0.90667248 0.07163303]]
#print labels
[ 0. 1. 1. 0.]
#print testresult
[ 0. 1. 1. 0.]
所以你提供了 n 个扁平形状模型作为样本和 n 个标签,你就可以开始了。你可能甚至不需要 asm 部分,只需应用一些对方向敏感的过滤器,如 sobel 或 gabor 并连接矩阵并将它们展平,然后将它们直接馈送到 svm。您可能可以获得 70-90% 的准确率。
正如有人说 cnn 是 svms 的替代品。这里有一些实现 lenet5 的链接。到目前为止,我发现 svm 上手起来要简单得多。
https://github.com/lisa-lab/DeepLearningTutorials/
http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi
-编辑-
地标只是 n (x,y) 个向量,对吗?那么为什么不尝试将它们放入一个大小为 2n 的数组中,然后直接将它们提供给上面的代码呢?
例如,4个地标(0,0),(10,10),(50,50),(70,70)的3个训练样本
samples = [[0,0,10,10,50,50,70,70],
[0,0,10,10,50,50,70,70],
[0,0,10,10,50,50,70,70]]
labels=[0.,1.,2.]
0=开心
1=生气
2=厌恶