How to train an SVM with opencv based on a set of images?

后端 未结 2 572
日久生厌
日久生厌 2021-02-04 18:13

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:

2条回答
  •  执念已碎
    2021-02-04 18:52

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

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

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

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

    to:

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

提交回复
热议问题