Train SVM and save it with OpenCV 3.0
<p>I am using Visual Studio 2010, with OpenCV 3.0. I'm trying to train a SVM and to save it into a file, but I am having problems.</p>

<p>My purpose is to extract the HOG features of some images and train a SVM with them. All seems to be right, but when I try to save the model in a xml file I obtain the following error:</p>

<blockquote>
  <p>Unhandled exception in 0x000007fefd9bb16d (KernelBase.dll) in TrainSVM.exe: Exception de MICROSOFT C++: cv::Exception at memory location 0x0026e1b0.</p>
</blockquote>

<p>And then this is showed in console:</p>

<blockquote>
  <p>OpenCV Error: Parsing error (SVM model data is invalid, check sv_count, var_* an
  d class_count tags) in cv::ml::SVMImpl::write, file C:\builds\master_PackSlave-w
  in64-vc12-shared\opencv\modules\ml\src\svm.cpp, line 2027</p>
</blockquote>

<p>The error seems to appear when the SVM has not been trained properly, but I don't understand where I have failed, because the line</p>

<blockquote>
  <p>svm->train(auxResult)</p>
</blockquote>

<p>has "true" as result.</p>

<p>I have checked the images and they are loaded properly, anybody could help me?</p>

<p>Thanks in advance.</p>

<p>Here is the code:     </p>

<pre><code>    String imagesPathPos = "Positivas/*.jpg"; // it has filters, too !
    vector&lt;String&gt; fp;
    glob(imagesPathPos, fp); 
    int tamaño = fp.size();

    std::vector&lt;cv::Point&gt; positions;
    positions.push_back(cv::Point(0,0));
    std::vector&lt;float&gt; descriptor;
    Ptr&lt;TrainData&gt; auxResult;

    for (size_t i=0; i&lt;fp.size(); ++i)
    {
        string nameFile = fp[i];
        Mat img = imread(fp[i]);     
        cv::Mat grayImg;
        cvtColor( img, grayImg, COLOR_BGR2GRAY );           

        hog.compute(grayImg,descriptor,winStride,trainingPadding,positions);

        Mat auxDescriptor = cv::Mat(descriptor);
        Mat descriptorMat(1,auxDescriptor.rows,CV_32FC1);
        transpose(auxDescriptor, descriptorMat);
        trainingData.push_back(descriptorMat);          
        trainingLabels.push_back(labelPositive);        
    }

    String imagesPathNeg = "Negativas/*.jpg";
    vector&lt;String&gt; fn;
    glob(imagesPathNeg, fn, true); 
    for (size_t i=0; i&lt;fn.size(); i++)
    {
        Mat img = imread(fn[i]);
        cv::Mat grayImg;
        cvtColor( img, grayImg, COLOR_BGR2GRAY );
        hog.compute(grayImg,descriptor,Size(),Size(),positions);

        Mat auxDescriptor = cv::Mat(descriptor);
        Mat descriptorMat(1,auxDescriptor.rows,CV_32FC1);
        transpose(auxDescriptor, descriptorMat);
        trainingData.push_back(descriptorMat);
        trainingLabels.push_back(labelPositive);
    }

    auxResult = TrainData::create(trainingData, type, trainingLabels);

    svm-&gt;train(auxResult);

    svm-&gt;save("output.xml");
</code></pre>