适用于Android的OpenCV-使用SURF描述符训练SVM [英] OpenCV for Android - training SVM with SURF descriptors
问题描述
在培训适用于Android应用程序的SVM时,我需要一些帮助. 我有一组不同类别(12个类别)的图像,并从中获取了所有描述符.我设法为每个图像获取相同数量的描述符.我需要的是使用这些描述符为我的android应用程序训练一个SVM. 我不确定是否应该在Android模拟器中训练它还是编写C ++程序来训练SVM,然后将其加载到我的应用程序中(如果我使用Windows的OpenCV的lib来训练SVM,然后保存它,我用于Android的lib是否可以识别已保存的SVM文件?).我想我不应该在模拟器中使用如此大的数据集来训练SVM. 我已经在Weka的SMO( http://www .cs.waikato.ac.nz/ml/weka/)并获得了良好的结果,但是我需要实现(或使用openCV的)SVM并保存它的训练以备将来分类.
I need some help in training a SVM for an Android app. I have a set of images in different classes (12 classes) and got all descriptors from them. I managed to get the same amount of descriptors for each image. What I need is to train a SVM for my android application with those descriptors. I'm not sure if I should train it in the Android emulator or write a C++ program to train the SVM and then load it in my app (if I use the OpenCV's lib for windows to train the SVM and then save it, will the lib I'm using for Android recognize the saved SVM file?). I guess I shouldn't train the SVM with such big dataset in the emulator. I've already tested my descriptor's dataset on the SMO of Weka (http://www.cs.waikato.ac.nz/ml/weka/) and got good results, but I need to implement (or use the openCV's) SVM and save it trained for future classification.
推荐答案
以下是在OpenCV4Android中训练SVM的示例. trainData
是MatOfFloat
,其形式将取决于您用于获取特征向量的方法.为了制作trainData
,我使用Core.hconcat()
将数据集每个元素的特征向量连接到单个Mat
中.
Here is an example for training your SVM in OpenCV4Android. trainData
is a MatOfFloat
, the form of which will depend on the method you're using to get feature vectors. To make trainData
, I used Core.hconcat()
to concatenate the feature vectors for each element of the dataset into a single Mat
.
Mat responses = new Mat(1, sizeOfDataset, CvType.CV_32F);
responses.put(0, 0, labelArray); // labelArray is a float[] of labels for the data
CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_svm_type(CvSVM.C_SVC);
params.set_kernel_type(CvSVM.LINEAR);
params.set_term_crit(new TermCriteria(TermCriteria.EPS, 100, 1e-6)); // use TermCriteria.COUNT for speed
svm.train_auto(trainData, responses, new Mat(), new Mat(), params);
我非常确定OpenCV使用相同的格式在Android和C ++接口中保存SVM.当然,您始终可以在Android上训练SVM,并使用类似
I'm fairly sure OpenCV uses the same format to save SVMs in both the Android and C++ interfaces. Of course, you can always train the SVM in Android and save the XML file to your emulator's SD card using something like
File datasetFile = new File(Environment.getExternalStorageDirectory(), "dataset.xml");
svm.save(datasetFile.getAbsolutePath());
然后将其从SD卡中拉出,并将其存储在应用程序的/res/raw
文件夹中.
then pull it from the SD card and store it in your app's /res/raw
folder.
这篇关于适用于Android的OpenCV-使用SURF描述符训练SVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!