title: Emgu CV SVM example not working on version 3.0.0
body: <p>I am trying to implement the SVM example code found 
  <a href="http://www.emgu.com/wiki/index.php/SVM_%28Support_Vector_Machine%29_in_CSharp" rel="nofollow">Here</a>. It is the official example provided at the Emgu CV documentation, but it is for version 1.5 (at least). 
  Unless I have been very mistaken, a lot of the classes in this example either work differently in version 3.0.0 or don't exist at all and have been substituted.<br>
  One example is the <code>SVMParams</code> class which as shown <a href="http://www.emgu.com/wiki/files/3.0.0/document/html/b1771ec1-9e4b-1cd4-f6fb-ad89643dd50b.htm" rel="nofollow">Here</a> doesn't exist anymore.<br>
  Also, the <code>TrainData</code> class has been developed which is the new input of the <code>TrainAuto</code> method of an SVM object, having substituted the <code>Matrix&lt;single&gt;</code> class.
  I have tried to implement the example after changing what I believe needed change but the code reaches the <code>bool trained = model.TrainAuto(td, 5);</code> line and returns a Divide by zero exception.</p>
  
  <p>Maybe there are more issues after this line but this is as far as my code compiles.
  Here is what I am trying to execute:</p>
  
  <pre><code>private void Classify()
      {
          int trainingSampleCount = 150;
          int sigma = 60;
  
          #region Generate the training data and classes
  
          Matrix&lt;float&gt; trainData = new Matrix&lt;float&gt;(trainingSampleCount, 2);
          Matrix&lt;float&gt; trainClasses = new Matrix&lt;float&gt;(trainingSampleCount, 1);
  
          Image&lt;Bgr, Byte&gt; img = new Image&lt;Bgr, Byte&gt;(500, 500);
  
          Matrix&lt;float&gt; sample = new Matrix&lt;float&gt;(1, 2);
  
          Matrix&lt;float&gt; trainData1 = trainData.GetRows(0, trainingSampleCount / 3, 1);
          trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
          trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
  
  
          Matrix&lt;float&gt; trainData2 = trainData.GetRows(trainingSampleCount / 3, 2 * trainingSampleCount / 3, 1);
          trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma));
  
          Matrix&lt;float&gt; trainData3 = trainData.GetRows(2 * trainingSampleCount / 3, trainingSampleCount, 1);
          trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
          trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
  
          Matrix&lt;float&gt; trainClasses1 = trainClasses.GetRows(0, trainingSampleCount / 3, 1);
          trainClasses1.SetValue(1);
          Matrix&lt;float&gt; trainClasses2 = trainClasses.GetRows(trainingSampleCount / 3, 2 * trainingSampleCount / 3, 1);
          trainClasses2.SetValue(2);
          Matrix&lt;float&gt; trainClasses3 = trainClasses.GetRows(2 * trainingSampleCount / 3, trainingSampleCount, 1);
          trainClasses3.SetValue(3);
  
          #endregion
  
          using (SVM model = new SVM())
          {
              //changed from example
              model.SetKernel(Emgu.CV.ML.SVM.SvmKernelType.Linear);
              model.Type = SVM.SvmType.CSvc;
              model.C = 1;
              model.TermCriteria = new MCvTermCriteria(100, 0.00001);
  
              TrainData td = new TrainData(trainData, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, trainClasses);
              bool trained = model.TrainAuto(td, 5);
              //changes up to this point
  
              for (int i = 0; i &lt; img.Height; i++)
              {
                  for (int j = 0; j &lt; img.Width; j++)
                  {
                      sample.Data[0, 0] = j;
                      sample.Data[0, 1] = i;
  
                      float response = model.Predict(sample);
  
                      img[i, j] =
                          response == 1 ? new Bgr(90, 0, 0) :
                          response == 2 ? new Bgr(0, 90, 0) :
                          new Bgr(0, 0, 90);
                  }
              }
              // changed the GetSupportVectors()
              Mat supvec = model.GetSupportVectors();
              int c = supvec.Height;
  
              for (int i = 0; i &lt; c; i++)
              {
                  // The way the data is received changed as well 
                  byte[] b = supvec.GetData(i);
                  float[] v = new float[] { (float)b[0], (float)b[1] };
  
                  PointF p1 = new PointF(v[0], v[1]);
                  img.Draw(new CircleF(p1, 4), new Bgr(128, 128, 128), 2);
              }
          }
  
          //display the original training samples
          for (int i = 0; i &lt; (trainingSampleCount / 3); i++)
          {
              PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
              img.Draw(new CircleF(p1, 2.0f), new Bgr(255, 100, 100), -1);
              PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]);
              img.Draw(new CircleF(p2, 2.0f), new Bgr(100, 255, 100), -1);
              PointF p3 = new PointF(trainData3[i, 0], trainData3[i, 1]);
              img.Draw(new CircleF(p3, 2.0f), new Bgr(100, 100, 255), -1);
          }
  
          Emgu.CV.UI.ImageViewer.Show(img);
      }
  </code></pre>
  
  <p>Here is part of the stacktrace that describes the exception</p>
  
  <pre><code>at Emgu.CV.ML.MlInvoke.CvSVMTrainAuto(IntPtr model, IntPtr trainData, Int32 kFold, MCvParamGrid&amp; cGrid, MCvParamGrid&amp; gammaGrid, MCvParamGrid&amp; pGrid, MCvParamGrid&amp; nuGrid, MCvParamGrid&amp; coefGrid, MCvParamGrid&amp; degreeGrid, Boolean balanced)
  at Emgu.CV.ML.SVM.TrainAuto(TrainData trainData, Int32 kFold, MCvParamGrid cGrid, MCvParamGrid gammaGrid, MCvParamGrid pGrid, MCvParamGrid nuGrid, MCvParamGrid coefGrid, MCvParamGrid degreeGrid, Boolean balanced)
  at Emgu.CV.ML.SVM.TrainAuto(TrainData trainData, Int32 kFold)
  </code></pre>
  
  <p>I have no idea why the exception occurs as the data that is used for the method are created as per the example. </p>
  
  <p>Any help will be greatly appreciated.</p>