OpenCV for Android - training SVM with SURF descriptors

时间秒杀一切 提交于 2019-12-04 11:14:31

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);

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());

then pull it from the SD card and store it in your app's /res/raw folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!