[OpenCV2.4] SVM processing linear inseparable example

Source: Internet
Author: User
[Original article: www.cnblogs.comjustanyarchive201211262788509.html] Not all things in the actual object model are linearly segmented. We need to find a way to divide Linear Non-sharding data. Principle, we can deduce that for Linearly partitioned data, the optimal Super Plane should meet the following requirements: now we want to introduce

[Original article: http://www.cnblogs.com/justany/archive/2012/11/26/2788509.html] in the actual thing model, not all things are linearly segmented. We need to find a way to divide Linear Non-sharding data. Principle, we can deduce that for Linearly partitioned data, the optimal Super Plane should meet the following requirements: now we want to introduce

[Original: http://www.cnblogs.com/justany/archive/2012/11/26/2788509.html]

Purpose

  • In the actual thing model, not all things are linearly segmented.
  • We need to find a way to divide Linear Non-sharding data.

Principle

, We can export linear data that can be divided into super planes to meet the following requirements:

    

Now we want to introduce something to indicate the impact of the data points (such as noise) that are divided by mistake on the division.

How to express these effects?

The farther the point to which the score is divided, the more serious the error is.

Therefore, we introduce the distance between the corresponding sample and the similar region.

The next question is, how can we convert the degree of this error to the same measurement as the original model?

A constant C is introduced to represent the conversion relationship with the original model measurement. C pairs are weighted and used to represent the impact of the error points on the original model, in this way, we get a new optimization problem model:

    

The selection of parameter C depends on the distribution of training samples. Although there is no general answer, it is useful to remember the following rules:

  • When C is large, the classification error rate is small, but the interval is also small. In this case, the error classification has a great impact on the model function. Since the purpose of optimization is to minimize this model function, the error classification situation will inevitably be restrained.
  • C has a large interval of hours, but the classification error rate is also large. In this case, the sum of error classifications in model functions has less impact on the optimization process. The optimization process is more focused on finding a hyperplane that can produce large intervals.

To put it bluntly, the size of C represents the extent to which the error score data affects the original model. The larger the value of C, the more attention the error score during optimization. On the other hand, the more concerned about whether a large interval hyperplane can be generated.

Start to use

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Define NTRAINING_SAMPLES 100 // number of training samples for each type # define FRAC_LINEAR_SEP 0.9f // The proportion of linearly segmented samples using namespace cv; using namespace std; int main () {// The data used to display const int WIDTH = 512, HEIGHT = 512; Mat I = Mat: zeros (HEIGHT, WIDTH, CV_8UC3);/* 1. training data is generated immediately */Mat trainData (2 * NTRAINING_SAMPLES, 2, CV_32FC1); Mat labels (2 * NTRAINING_SAMPLES, 1, CV_32FC1); RNG rng (100 ); // generate the random number. // set the training data int nLinearSamples = (int) (FRAC_LINEAR_SEP * NTRAINING_SAMPLES). // generate the random point Mat trainClass = trainData for Category 1. rowRange (0, nLinearSamples); // The x coordinate of the vertex is between [0, 0.4). Mat c = trainClass. colRange (0, 1); rng. fill (c, RNG: UNIFORM, Scalar (1), Scalar (0.4 * WIDTH); // The y coordinate of the point is between [0, 1) c = trainClass. colRange (1, 2); rng. fill (c, RNG: UNIFORM, Scalar (1), Scalar (HEIGHT); // generate a random point trainClass = trainData for Category 2. rowRange (2 * NTRAINING_SAMPLES-nLinearSamples, 2 * NTRAINING_SAMPLES); // The x coordinate of the point is between [0.6, 1] c = trainClass. colRange (0, 1); rng. fill (c, RNG: UNIFORM, Scalar (0.6 * WIDTH), Scalar (WIDTH); // The y coordinate of the point is between [0, 1) c = trainClass. colRange (1, 2); rng. fill (c, RNG: UNIFORM, Scalar (1), Scalar (HEIGHT )); /* set non-linear training data. * // generate a random vertex trainClass = trainData for Classification 1 and Classification 2. rowRange (nLinearSamples, 2 * NTRAINING_SAMPLES-nLinearSamples); // The x coordinate of the point between [0.4, 0.6) c = trainClass. colRange (0, 1); rng. fill (c, RNG: UNIFORM, Scalar (0.4 * WIDTH), Scalar (0.6 * WIDTH); // The y coordinate of the point is in [0, 1) between c = trainClass. colRange (1, 2); rng. fill (c, RNG: UNIFORM, Scalar (1), Scalar (HEIGHT);/**/labels. rowRange (0, NTRAINING_SAMPLES ). setTo (1); // Class 1 labels. rowRange (NTRAINING_SAMPLES, 2 * NTRAINING_SAMPLES ). setTo (2); // Class 2/* Set SVM parameters */CvSVMParams params; params. svm_type = SVM: C_SVC; params. C = 0.1; params. kernel_type = SVM: LINEAR; params. term_crit = TermCriteria (CV_TERMCRIT_ITER, (int) 1e7, 1e-6);/* 3. training Support Vector Machine */cout <"Starting training process" <endl; CvSVM; svm. train (trainData, labels, Mat (), Mat (), params); cout <"Finished training process" <endl;/* 4. show partition areas */Vec3b green (0,100, 0), blue (, 0, 0); for (int I = 0; I <I. rows; ++ I) for (int j = 0; j <I. cols; ++ j) {Mat sampleMat = (Mat _
     
      
(1, 2) <I, j); float response = svm. predict (sampleMat); if (response = 1) I.
      
        (J, I) = green; else if (response = 2) I.
       
         (J, I) = blue;}/* 5. show training data */int thick =-1; int lineType = 8; float px, py; // Classification 1 for (int I = 0; I <NTRAINING_SAMPLES; ++ I) {px = trainData. at
        
          (I, 0); py = trainData.
         
           (I, 1); circle (I, Point (int) px, (int) py), 3, Scalar (0,255, 0), thick, lineType );} // Category 2 for (int I = NTRAINING_SAMPLES; I <2 * NTRAINING_SAMPLES; ++ I) {px = trainData. at
          
            (I, 0); py = trainData.
           
             (I, 1); circle (I, Point (int) px, (int) py), 3, Scalar (255, 0, 0), thick, lineType );} /* 6. display Support Vector */thick = 2; lineType = 8; int x = svm. get_support_vector_count (); for (int I = 0; I <x; ++ I) {const float * v = svm. get_support_vector (I); circle (I, Point (int) v [0], (int) v [1]), 6, Scalar (128,128,128), thick, lineType);} imwrite ("result.png", I); // Save the image imshow ("SVM linear division of unpartitioned data", I ); // display the user waitKey (0 );}
           
          
         
        
       
      
     
    
   
  
 

Set SVM Parameters

For parameter settings, refer to the API.

CvSVMParams params;params.svm_type    = SVM::C_SVC;params.C              = 0.1;params.kernel_type = SVM::LINEAR;params.term_crit   = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);

We can see that class c svm is used this time. The value of parameter C is 0.1.

Result

  • The program creates an image in which the training sample is displayed. One Class is displayed as a light green circle, and the other class is displayed as a light blue circle.
  • Train to obtain SVM and classify each pixel of the image. The classification result divides the image into two parts: blue-green, and the center line is the optimal multi-plane segmentation. Due to the non-linear differentiation of samples, there are naturally some samples that are incorrectly classified. Some green points are divided into blue areas, and some blue points are divided into green areas.
  • Finally, the support vector is highlighted by a gray border.

Original copied text

Support Vector Machines for Non-Linearly Separable Data. OpenCV.org

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.