如何基于一组图像训练带有opencv的SVM?

Joa*_*les 6 opencv svm

我有一个积极的文件夹和JPG格式的另一个负面图像,我想基于该图像训练SVM,我做了以下但我收到一个错误:

Mat classes = new Mat();
Mat trainingData = new Mat();

Mat trainingImages = new Mat();
Mat trainingLabels = new Mat();

CvSVM clasificador;

for (File file : new File(path + "positives/").listFiles()) {
        Mat img = Highgui.imread(file.getAbsolutePath());
        img.reshape(1, 1);

        trainingImages.push_back(img);
        trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32FC1));
    }

    for (File file : new File(path + "negatives/").listFiles()) {
        Mat img = Highgui.imread(file.getAbsolutePath());
        img.reshape(1, 1);

        trainingImages.push_back(img);
        trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));
    }

    trainingImages.copyTo(trainingData);
    trainingData.convertTo(trainingData, CvType.CV_32FC1);
    trainingLabels.copyTo(classes);

    CvSVMParams params = new CvSVMParams();
    params.set_kernel_type(CvSVM.LINEAR);

    clasificador = new CvSVM(trainingData, classes, new Mat(), new Mat(), params);
Run Code Online (Sandbox Code Playgroud)

当我尝试运行时,我获得:

OpenCV Error: Bad argument (train data must be floating-point matrix) in cvCheckTrainData, file ..\..\..\src\opencv\modules\ml\src\inner_functions.cpp, line 857
Exception in thread "main" CvException [org.opencv.core.CvException: ..\..\..\src\opencv\modules\ml\src\inner_functions.cpp:857: error: (-5) train data must be floating-point matrix in function cvCheckTrainData
]
    at org.opencv.ml.CvSVM.CvSVM_1(Native Method)
    at org.opencv.ml.CvSVM.<init>(CvSVM.java:80)
Run Code Online (Sandbox Code Playgroud)

我无法设法训练SVM,任何想法?谢谢

Bee*_*Bee 11

假设您通过重塑图像并使用它来训练SVM来了解您正在做什么,最可能的原因是您的

Mat img = Highgui.imread(file.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)

无法实际读取图像,生成img具有null data属性的矩阵,最终将在OpenCV代码中触发以下内容:

// check parameter types and sizes
if( !CV_IS_MAT(train_data) || CV_MAT_TYPE(train_data->type) != CV_32FC1 )
    CV_ERROR( CV_StsBadArg, "train data must be floating-point matrix" );
Run Code Online (Sandbox Code Playgroud)

基本上train_data没有第一个条件(作为有效矩阵)而不是第二个条件失败(类型为CV_32FC1).

此外,即使重塑在*this对象上工作,它也像过滤器一样,其效果不是永久性的.如果它在单个语句中使用而没有立即使用或分配给另一个变量,那么它将是无用的.更改代码中的以下行:

img.reshape(1, 1);
trainingImages.push_back(img);
Run Code Online (Sandbox Code Playgroud)

至:

trainingImages.push_back(img.reshape(1, 1));
Run Code Online (Sandbox Code Playgroud)