如何基于一组图像使用opencv训练SVM? [英] How to train an SVM with opencv based on a set of images?

查看:231
本文介绍了如何基于一组图像使用opencv训练SVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPG格式的正片图像和另一个负片图像文件夹,并且我想基于该图像训练SVM,我做了以下操作,但收到错误消息:

I have a folder of positives and another of negatives images in JPG format, and I want to train an SVM based on that images, I've done the following but I receive an error:

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

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

When I try to run that I obtain:

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)

我无法设法训练SVM,知道吗?谢谢

I can't manage to train the SVM, any idea? Thanks

推荐答案

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

Assuming that you know what you are doing by reshaping an image and using it to train SVM, the most probable cause of this is that your

Mat img = Highgui.imread(file.getAbsolutePath());

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

fails to actually read an image, generating a matrix img with null data property, which will eventually trigger the following in the OpenCV code:

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

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

Basically train_data fails the first condition (being a valid matrix) rather than failing the second condition (being of type CV_32FC1).

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

In addition, even though reshape works on the *this object, it acts like a filter and its effect is not permanent. If it's used in a single statement without immediately being used or assigned to another variable it will be useless. Change the following lines in your code:

img.reshape(1, 1);
trainingImages.push_back(img);

收件人:

trainingImages.push_back(img.reshape(1, 1));

这篇关于如何基于一组图像使用opencv训练SVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆