OpenCV : Training SVM Error - Assertion failed
<p>Im writing program to classify objects using SVM and BoW. I am getting the following error when I try use the TrainData::create() member function to create the data necessary to train SVM classifier.</p>

<blockquote>
  <p>OpenCV Error: Assertion failed (responses.type() == CV_32F || responses.type() == CV_32S) in setData</p>
</blockquote>

<p>This is my function to read the train data from a director, compute BoW histogram for each train image, create a matrix of all descriptors of all train images in a matrix and the create the train data, labels and then train the SVM</p>

<pre><code>void trainClassifier(string dictionaryPath, string trainDataPath, string saveClassifierPath, int samples){

//Write file
FileStorage readFile(dictionaryPath, FileStorage::READ);

//Load into Dictionary matrix
readFile["Data"] &gt;&gt; dictionary;

if(dictionary.empty() == false)
{
    cout &lt;&lt; "Error loading visual vocalbulary" &lt;&lt; endl;
}

//Set the Bow descripter with the dictionary
testBOW.setVocabulary(dictionary);

//Inititate variables
vector&lt;KeyPoint&gt; keypointTrain;
vector&lt;DMatch&gt; matchTrain;
Mat descriptorTrain;

//inputTrain -&gt; input images, inputFeatures -&gt; BoW descriptor output
Mat inputTrain;
Mat inputFeatures;

//Label array
vector&lt;string&gt; label;

//Create a string to read files from directory
string updatedDataPath;

for(int i = 1; i &lt;= samples; i++)
{
    //Update the string updateDataPath to correspond the image FILENAME with each iteration
    updatedDataPath.append(trainDataPath);
    updatedDataPath += to_string(i);
    updatedDataPath.append(".JPEG");

    //Read FILE from the updated datapath
    inputTrain = imread(updatedDataPath);

    //Convert to single channel, since classifier takes only single channel data
    cvtColor(inputTrain, inputTrain, CV_BGR2GRAY);

    //Generate BoW features/histogram for the train image
    testBOW.compute(inputTrain, keypointTrain, inputFeatures);

    //Load the data in the descriptor Matrix
    descriptorTrain.push_back(inputFeatures);

    //Generate label according to the sample
    if(samples &gt; 1 &amp;&amp; samples &lt;= 10)
    {
        label.push_back("OBJ1 POSSITIVE");
    }
    else if (samples &gt; 11 &amp;&amp; samples &lt;= 20)
    {
        label.push_back("OBJ1 NEGATIVE");
    }

    //Reset data path
    updatedDataPath.clear();
}

//Convert the descriptor matrix into 32-pt float to make it compatible with classifier
if(descriptorTrain.type() != CV_32F)
{
    descriptorTrain.convertTo(descriptorTrain, CV_32F);
}

//Create train data using TrainData::create()
Ptr&lt;TrainData&gt; trainData = TrainData::create(descriptorTrain, ROW_SAMPLE, label);
//Iniitialize Support vector based classifier (SVM) to classify and detect object
Ptr&lt;SVM&gt;SVM = SVM::create();
SVM-&gt;setType(SVM::C_SVC);
SVM-&gt;setKernel(SVM::LINEAR);
SVM-&gt;setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

//Now train the SVM
SVM-&gt;trainAuto(trainData);
SVM-&gt;save(saveClassifierPath);

cout &lt;&lt; "Classifier training status: SUCCESSFUL" &lt;&lt; endl;}
</code></pre>

<p>Any help is appreciated. Thanks and cheers :)</p>