训练 SVM 时出现 OpenCV 错误(错误的论点)

OpenCV error while training SVM (bad argument)

我已经用训练图像(150 张图像大小为 144x33)创建了垫子,所以我的垫子宽度为 4752,高度为 150。另一个带有标签的垫子是 1 宽度和 150 高度。现在当我用这两个 Mat 尝试 svm.train() 时,出现以下错误:

OpenCV Error: Bad argument (response #2 is not integral) in cvPreprocessCategoricalResponses, file ..\..\..\..\opencv\modules\ml\src\inner_functions.cpp, line 715
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\ml\src\inner_functions.cpp:715: error: (-5) response #2 is not integral in function cvPreprocessCategoricalResponses]

这是我的一段代码,有人能告诉我哪里出了问题吗?

Mat trainingImages = new Mat(0, imageWidth * imageHeight, CvType.CV_32FC1);
    Mat labels = new Mat(amountOfPlates + amountOfNoPlates, 1, CvType.CV_32FC1);
    List<Integer> trainingLabels = new ArrayList<>();

    for (int i = 0; i < amountOfPlates; i++) {
        int index = i + 1;
        String file = pathPlates + index + ".jpg";

        Mat img = Highgui.imread(file, 0);
        img.convertTo(img, CvType.CV_32FC1);
        img = img.reshape(1, 1);
        trainingImages.push_back(img);
        trainingLabels.add(1);
    }

    for (int i = 0; i < amountOfNoPlates; i++) {
        int index = i + 1;
        String file = pathNoPlates + index + ".jpg";
        Mat img = Highgui.imread(file, 0);
        img.convertTo(img, CvType.CV_32FC1);
        img = img.reshape(1, 1);
        trainingImages.push_back(img);
        trainingLabels.add(0);
    }

    Integer[] array = trainingLabels.toArray(new Integer[trainingLabels.size()]);

    int[] trainLabels = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        trainLabels[i] = array[i];
    }

    for (int i = 0; i < trainingLabels.size(); i++) {
        labels.put(i, 1, trainLabels[i]);
    }

    CvSVMParams params = new CvSVMParams();
    params.set_svm_type(CvSVM.C_SVC);
    params.set_kernel_type(CvSVM.LINEAR);
    params.set_degree(0);
    params.set_gamma(1);
    params.set_coef0(0);
    params.set_C(1);
    params.set_nu(0);
    params.set_p(0);
    TermCriteria tc = new TermCriteria(opencv_core.CV_TERMCRIT_ITER, 1000, 0.01);
    params.set_term_crit(tc);

    Size data = trainingImages.size();
    Size label = labels.size();


    CvSVM svmClassifier = new CvSVM();
    svmClassifier.train(trainingImages, labels, new Mat(), new Mat(), params);
    svmClassifier.save("test.xml");

尺寸数据显示:宽度 = 4752,高度 = 150

尺寸标签显示:宽度 = 1,高度 = 150

我做错了什么?

Mat labels 被定义为 CV_32FC1,但您用 int[] trainLabels.
中的整数扩展它 您应该改用浮点数 trainLabelsCV_32SC1 类型 labels