training SVM algorithm in android

2019-08-28 20:42发布

问题:

hi every one i have a little problem training my SVM algorithm in android.here is my code

Imgproc.calcHist(list, channels, new Mat(), hist, histSize, ranges);
          Imgproc.calcHist(lis, channels, new Mat(), his, histSize, ranges);
         // tl.setText(his.toString());
          Mat train_data = new Mat(2, 0, CvType.CV_32FC1);
          train_data.push_back(hist);
          train_data.push_back(his);
          Mat train_dat = new Mat(2, 0, CvType.CV_32FC1);
          train_dat.put(2, 0, labelArray);
           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));
          svm.train_auto(train_data, train_dat, new Mat(), new Mat(), params);

when i run it i get errors of this sort

03-15 13:08:30.065: E/cv::error()(381): OpenCV Error: Bad argument (Invalid response array) in cvPreprocessCategoricalResponses, file /home/reports/ci/slave/50-SDK/opencv/modules/ml/src/inner_functions.cpp, line 664
03-15 13:08:30.120: E/AndroidRuntime(381): CvException [org.opencv.core.CvException: /home/reports/ci/slave/50-SDK/opencv/modules/ml/src/inner_functions.cpp:664: error: (-5) Invalid response array in function cvPreprocessCategoricalResponses
03-15 13:08:30.120: E/AndroidRuntime(381):  at org.opencv.ml.CvSVM.train_auto_1(Native Method)
03-15 13:08:30.120: E/AndroidRuntime(381):  at org.opencv.ml.CvSVM.train_auto(CvSVM.java:395)

i need your help pliz.thnx

回答1:

You're creating two Mats with 2 rows and zero columns, that is highly suspicious. In the end OpenCV is complaining that the second array passed into train_auto has an incorrect shape. With what you're showing this is all than can be said. You need to understand the expected shapes of the Mats passed into train_auto.

Here is a good example that shows what the size of the two parameters passed into train auto should be:

http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html

Also it is unclear if there is a good reason to be using such an exotic setup (training an SVM in Android) it may be useful for you to try to do the programming in a real machine until you fully understand things? I'm pretty sure the errors will be more helpful there.