Difference between revisions of "SVM (Support Vector Machine) in CSharp"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://obibohococ.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 +
----
 +
=[http://obibohococ.co.cc CLICK HERE]=
 +
----
 +
</div>
 
'''The source code of this example is contributed by Albert G. It requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
 
'''The source code of this example is contributed by Albert G. It requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
  
Line 7: Line 15:
  
 
== Source Code ==
 
== Source Code ==
<source lang="csharp">
+
&lt;source lang="csharp">
 
using System.Drawing;
 
using System.Drawing;
 
using Emgu.CV.Structure;
 
using Emgu.CV.Structure;
Line 20: Line 28:
 
#region Generate the training data and classes
 
#region Generate the training data and classes
  
Matrix<float> trainData = new Matrix<float>(trainSampleCount, 2);
+
Matrix&lt;float> trainData = new Matrix&lt;float>(trainSampleCount, 2);
Matrix<float> trainClasses = new Matrix<float>(trainSampleCount, 1);
+
Matrix&lt;float> trainClasses = new Matrix&lt;float>(trainSampleCount, 1);
  
Image<Bgr, Byte> img = new Image<Bgr, byte>(500, 500);
+
Image&lt;Bgr, Byte> img = new Image&lt;Bgr, byte>(500, 500);
  
Matrix<float> sample = new Matrix<float>(1, 2);
+
Matrix&lt;float> sample = new Matrix&lt;float>(1, 2);
  
Matrix<float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1);
+
Matrix&lt;float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1);
 
trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
 
trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
 
trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
 
trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
  
Matrix<float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1);
+
Matrix&lt;float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1);
 
trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma));
 
trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma));
  
Matrix<float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1);
+
Matrix&lt;float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1);
 
trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
 
trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma));
 
trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
 
trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));
  
Matrix<float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1);
+
Matrix&lt;float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1);
 
trainClasses1.SetValue(1);
 
trainClasses1.SetValue(1);
Matrix<float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1);
+
Matrix&lt;float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1);
 
trainClasses2.SetValue(2);
 
trainClasses2.SetValue(2);
Matrix<float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1);
+
Matrix&lt;float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1);
 
trainClasses3.SetValue(3);
 
trainClasses3.SetValue(3);
  
Line 58: Line 66:
 
   bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5);
 
   bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5);
 
    
 
    
   for (int i = 0; i < img.Height; i++)
+
   for (int i = 0; i &lt; img.Height; i++)
 
   {
 
   {
       for (int j = 0; j < img.Width; j++)
+
       for (int j = 0; j &lt; img.Width; j++)
 
       {
 
       {
 
         sample.Data[0, 0] = j;
 
         sample.Data[0, 0] = j;
Line 75: Line 83:
  
 
   int c = model.GetSupportVectorCount();
 
   int c = model.GetSupportVectorCount();
   for (int i = 0; i < c; i++)
+
   for (int i = 0; i &lt; c; i++)
 
   {
 
   {
 
       float[] v = model.GetSupportVector(i);
 
       float[] v = model.GetSupportVector(i);
Line 84: Line 92:
  
 
// display the original training samples
 
// display the original training samples
for (int i = 0; i < (trainSampleCount / 3); i++)
+
for (int i = 0; i &lt; (trainSampleCount / 3); i++)
 
{
 
{
 
   PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
 
   PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
Line 95: Line 103:
  
 
Emgu.CV.UI.ImageViewer.Show(img);
 
Emgu.CV.UI.ImageViewer.Show(img);
</source>
+
&lt;/source>
  
 
== Result ==
 
== Result ==
 
[[image:SVM.png]]
 
[[image:SVM.png]]

Revision as of 03:31, 24 November 2010



Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly


CLICK HERE


The source code of this example is contributed by Albert G. It requires Emgu CV 1.5.0.0

What is a Support Vector Machine

According to wikipedia,

Support vector machines (SVMs) are a set of related supervised learning methods used for classification and regression. Viewing input data as two sets of vectors in an n-dimensional space, an SVM will construct a separating hyperplane in that space, one which maximizes the margin between the two data sets. To calculate the margin, two parallel hyperplanes are constructed, one on each side of the separating hyperplane, which are "pushed up against" the two data sets. Intuitively, a good separation is achieved by the hyperplane that has the largest distance to the neighboring datapoints of both classes, since in general the larger the margin the lower the generalization error of the classifier.

Source Code

<source lang="csharp"> using System.Drawing; using Emgu.CV.Structure; using Emgu.CV.ML; using Emgu.CV.ML.Structure;

...

int trainSampleCount = 150; int sigma = 60;

  1. region Generate the training data and classes

Matrix<float> trainData = new Matrix<float>(trainSampleCount, 2); Matrix<float> trainClasses = new Matrix<float>(trainSampleCount, 1);

Image<Bgr, Byte> img = new Image<Bgr, byte>(500, 500);

Matrix<float> sample = new Matrix<float>(1, 2);

Matrix<float> trainData1 = trainData.GetRows(0, trainSampleCount / 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<float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma));

Matrix<float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma));

Matrix<float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1); trainClasses1.SetValue(1); Matrix<float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainClasses2.SetValue(2); Matrix<float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainClasses3.SetValue(3);

  1. endregion

using (SVM model = new SVM()) {

  SVMParams p = new SVMParams();
  p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.LINEAR;
  p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC;
  p.C = 1;
  p.TermCrit = new MCvTermCriteria(100, 0.00001);
  //bool trained = model.Train(trainData, trainClasses, null, null, p);
  bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5);
  
  for (int i = 0; i < img.Height; i++)
  {
     for (int j = 0; j < 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);
     }
  }
  int c = model.GetSupportVectorCount();
  for (int i = 0; i < c; i++)
  {
     float[] v = model.GetSupportVector(i);
     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 < (trainSampleCount / 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); </source>

Result

SVM.png