尝试训练SVM时出错(无法正确初始化标签)

时间:2015-05-27 23:29:21

标签: android opencv svm

我正在尝试用4张图像训练我的svm。我的所有图像都是300 * 400。我将它们调整为304 * 400,因此我可以获得我的图像的HOGDescriptor,因为16 * 16块。然后我使用Core.hconcat(mats,trainData)将我的所有图像收集到一个Mat中。之后,当我尝试为我的trainData设置标签时,在火车部分我得到低于错误。我是openCV的新手。怎么了?

    Mat rose1 = new Mat();
    Mat rose2 = new Mat();
    Mat rose3 = new Mat();
    Mat rose4 = new Mat();
    Mat rose5 = new Mat();

    try {
        rose1 = org.opencv.android.Utils.loadResource(
                getApplicationContext(), R.drawable.rose1);
        rose2 = org.opencv.android.Utils.loadResource(
                getApplicationContext(), R.drawable.rose2);
        rose3 = org.opencv.android.Utils.loadResource(
                getApplicationContext(), R.drawable.rose3);
        rose4 = org.opencv.android.Utils.loadResource(
                getApplicationContext(), R.drawable.rose4);
        rose5 = org.opencv.android.Utils.loadResource(
                getApplicationContext(), R.drawable.rose5);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Mat rose1Resized = new Mat();
    Mat rose2Resized = new Mat();
    Mat rose3Resized = new Mat();
    Mat rose4Resized = new Mat();

    Size sz = new Size(304, 400);

    Imgproc.resize(rose1, rose1Resized, sz);
    Imgproc.resize(rose2, rose2Resized, sz);
    Imgproc.resize(rose3, rose3Resized, sz);
    Imgproc.resize(rose4, rose4Resized, sz);

    // HOG
    MatOfFloat rose1Float = new MatOfFloat();
    MatOfFloat rose2Float = new MatOfFloat();
    MatOfFloat rose3Float = new MatOfFloat();
    MatOfFloat rose4Float = new MatOfFloat();

    HOGDescriptor hog = new HOGDescriptor(new Size(304, 400), new Size(16,
            16), new Size(new Point(8, 8)), new Size(new Point(8, 8)), 9);
    hog.compute(rose1Resized, rose1Float);
    hog.compute(rose2Resized, rose2Float);
    hog.compute(rose3Resized, rose3Float);
    hog.compute(rose4Resized, rose4Float);

    ArrayList<Mat> mats = new ArrayList<>();
    mats.add(rose1Float);
    mats.add(rose2Float);
    mats.add(rose3Float);
    mats.add(rose4Float);

    // SVM
    Mat trainData = new Mat();
    Core.hconcat(mats, trainData);

    float[] lableFloat = { 1, 1, 1, 1 };
    Mat lables = new Mat(1, 4, CvType.CV_32FC1);
    lables.put(0, 0, lableFloat);

    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(trainData, lables, new Mat(), new Mat(), params);

错误是: E / AndroidRuntime(27347):CvException [org.opencv.core.CvException:cv :: Exception:/home/reports/ci/slave_desktop/50-SDK/opencv/modules/ml/src/inner_functions.cpp:671:error :( - 209)响应数组必须包含与函数cvPreprocessCategoricalResponses中的样本总数一样多的元素

1 个答案:

答案 0 :(得分:1)

首先,我在获得HOG后重塑了MatOfFloat。因为rose1Float是65268 * 1,我需要它在一行Mat。

    Mat roseReshaped1 = rose1Float.reshape(1, 1);
    Mat roseReshaped2 = rose2Float.reshape(1, 1);
    Mat roseReshaped3 = rose3Float.reshape(1, 1);
    Mat roseReshaped4 = rose4Float.reshape(1, 1);

然后我使用了push_back而不是“Core.hconcat(mats,trainData)”

    Mat trainData = new Mat(0, sizeOfCols, CvType.CV_32FC1);
    trainData.push_back(roseReshaped1);
    trainData.push_back(roseReshaped2);
    trainData.push_back(roseReshaped3);
    trainData.push_back(roseReshaped4);

我的trainData将是4 * 65268,这是我的标签。或者作为opencv说,回应!

    int[] l = { 1, 2, 3, 4 };
    Mat lables = new Mat(4, 1, CvType.CV_32SC1);
    lables.put(0, 0, l);

现在一切都很好。感谢@berak。