Use SVM to solve the problem of Level 3 Classification of 2D Spatial Vectors

Source: Internet
Author: User
Tags svm
[Original: blog. csdn. netfirefightarticledetails6400060] To Learn OPENCVSVM classifier, refer to the use of SVM on the Internet to solve the problem of binary vector classification and change it to C code, for reference only: OPENCV2.2VS2008 step: 1, generate random points and classify them according to a certain spatial distribution. 2,

[Original article: http://blog.csdn.net/firefight/article/details/6400060] to learn the opencv svm classifier, use SVM on the Internet to solve the classification problem of 2D Spatial vectors and change it to C code. For reference only: Step 1, generate random points and classify them according to a certain spatial distribution. 2,

[Original article: http://blog.csdn.net/firefight/article/details/6400060]

To learn the opencv svm classifier, refer to "Using SVM to solve the classification problem of 2D Spatial vectors" on the Internet and change it to C ++ code for reference only.

Environment: OPENCV2.2 + VS2008

Steps:
1. generate random points and classify them according to a certain spatial distribution.
2. Create SVM and use random vertex samples for training
3. Divide the entire space by SVM classification results and display the Support Vector

[Cpp]View plaincopy

  1. # Include "stdafx. h"
  2. # Include
  3. Void drawCross (Mat & img, Point center, Scalar color)
  4. {
  5. Int col = center. x> 2? Center. x: 2;
  6. Int row = center. y> 2? Center. y: 2;
  7. Line (img, Point (col-2, row-2), Point (col + 2, row + 2), color );
  8. Line (img, Point (col + 2, row-2), Point (col-2, row + 2), color );
  9. }
  10. Int newSvmTest (int rows, int cols, int testCount)
  11. {
  12. If (testCount> rows * cols)
  13. Return 0;
  14. Mat img = Mat: zeros (rows, cols, CV_8UC3 );
  15. Mat testPoint = Mat: zeros (rows, cols, CV_8UC1 );
  16. Mat data = Mat: zeros (testCount, 2, CV_32FC1 );
  17. Mat res = Mat: zeros (testCount, 1, CV_32SC1 );
  18. // Create random test points
  19. For (int I = 0; I <testCount; I ++)
  20. {
  21. Int row = rand () % rows;
  22. Int col = rand () % cols;
  23. If (testPoint. (Row, col) = 0)
  24. {
  25. TestPoint. (Row, col) = 1;
  26. Data. (I, 0) = float (col)/cols;
  27. Data. (I, 1) = float (row)/rows;
  28. }
  29. Else
  30. {
  31. I --;
  32. Continue;
  33. }
  34. If (row> (50 * cos (col * CV_PI/100) + 200 ))
  35. {
  36. DrawCross (img, Point (col, row), CV_RGB (255, 0, 0 ));
  37. Res. (I, 0) = 1;
  38. }
  39. Else
  40. {
  41. If (col> 200)
  42. {
  43. DrawCross (img, Point (col, row), CV_RGB (0,255, 0 ));
  44. Res. (I, 0) = 2;
  45. }
  46. Else
  47. {
  48. DrawCross (img, Point (col, row), CV_RGB (0, 0,255 ));
  49. Res. (I, 0) = 3;
  50. }
  51. }
  52. }
  53. // Show test points
  54. Imshow ("dst", img );
  55. WaitKey (0 );
  56. //// // Start svm trainning //////////////////
  57. CvSVM svm = CvSVM ();
  58. CvSVMParams param;
  59. CvTermCriteria criteria;
  60. Criteria = cvTermCriteria (CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
  61. /* SVM type: CvSVM: C_SVC
    Types of Kernel: CvSVM: RBF
    Degree: 10.0 (not used this time)
    Gamma: 8.0
    Coef0: 1.0 (not used this time)
    C: 10.0
    Nu: 0.5 (not used this time)
    P: 0.1 (not used this time)
    Then the training data is normalized and placed in an array of the CvMat type. */
  62. Param = CvSVMParams (CvSVM: C_SVC, CvSVM: RBF, 10.0, 8.0, 1.0, 10.0, 0.5, NULL, criteria );
  63. Svm. train (data, res, Mat (), Mat (), param );
  64. For (int I = 0; I <rows; I ++)
  65. {
  66. For (int j = 0; j <cols; j ++)
  67. {
  68. Mat m = Mat: zeros (1, 2, CV_32FC1 );
  69. M. (0, 0) = float (j)/cols;
  70. M. (0, 1) = float (I)/rows;
  71. Float ret = 0.0;
  72. Ret = svm. predict (m );
  73. Scalar rcolor;
  74. Switch (int) ret)
  75. {
  76. Case 1: rcolor = CV_RGB (100, 0, 0); break;
  77. Case 2: rcolor = CV_RGB (0,100, 0); break;
  78. Case 3: rcolor = CV_RGB (0, 0,100); break;
  79. }
  80. Line (img, Point (j, I), Point (j, I), rcolor );
  81. }
  82. }
  83. Imshow ("dst", img );
  84. WaitKey (0 );
  85. // Show support vectors
  86. Int sv_num = svm. get_support_vector_count ();
  87. For (int I = 0; I <sv_num; I ++)
  88. {
  89. Const float * support = svm. get_support_vector (I );
  90. Circle (img, Point (int) (support [0] * cols), (int) (support [1] * rows), 5, CV_RGB (200,200,200 ));
  91. }
  92. Imshow ("dst", img );
  93. WaitKey (0 );
  94. Return 0;
  95. }
  96. Int main (int argc, char ** argv)
  97. {
  98. Return newSvmTest (400,600,100 );
  99. }

Learning samples:

Category:

Support Vector:

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.