[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
- # Include "stdafx. h"
- # Include
-
- Void drawCross (Mat & img, Point center, Scalar color)
- {
- Int col = center. x> 2? Center. x: 2;
- Int row = center. y> 2? Center. y: 2;
-
- Line (img, Point (col-2, row-2), Point (col + 2, row + 2), color );
- Line (img, Point (col + 2, row-2), Point (col-2, row + 2), color );
- }
-
- Int newSvmTest (int rows, int cols, int testCount)
- {
- If (testCount> rows * cols)
- Return 0;
-
- Mat img = Mat: zeros (rows, cols, CV_8UC3 );
- Mat testPoint = Mat: zeros (rows, cols, CV_8UC1 );
- Mat data = Mat: zeros (testCount, 2, CV_32FC1 );
- Mat res = Mat: zeros (testCount, 1, CV_32SC1 );
-
- // Create random test points
- For (int I = 0; I <testCount; I ++)
- {
- Int row = rand () % rows;
- Int col = rand () % cols;
-
- If (testPoint. (Row, col) = 0)
- {
- TestPoint. (Row, col) = 1;
- Data. (I, 0) = float (col)/cols;
- Data. (I, 1) = float (row)/rows;
- }
- Else
- {
- I --;
- Continue;
- }
-
- If (row> (50 * cos (col * CV_PI/100) + 200 ))
- {
- DrawCross (img, Point (col, row), CV_RGB (255, 0, 0 ));
- Res. (I, 0) = 1;
- }
- Else
- {
- If (col> 200)
- {
- DrawCross (img, Point (col, row), CV_RGB (0,255, 0 ));
- Res. (I, 0) = 2;
- }
- Else
- {
- DrawCross (img, Point (col, row), CV_RGB (0, 0,255 ));
- Res. (I, 0) = 3;
- }
- }
-
- }
-
- // Show test points
- Imshow ("dst", img );
- WaitKey (0 );
-
- //// // Start svm trainning //////////////////
- CvSVM svm = CvSVM ();
- CvSVMParams param;
- CvTermCriteria criteria;
-
- Criteria = cvTermCriteria (CV_TERMCRIT_EPS, 1000, FLT_EPSILON );
- /* 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. */
- Param = CvSVMParams (CvSVM: C_SVC, CvSVM: RBF, 10.0, 8.0, 1.0, 10.0, 0.5, NULL, criteria );
- Svm. train (data, res, Mat (), Mat (), param );
-
- For (int I = 0; I <rows; I ++)
- {
- For (int j = 0; j <cols; j ++)
- {
- Mat m = Mat: zeros (1, 2, CV_32FC1 );
- M. (0, 0) = float (j)/cols;
- M. (0, 1) = float (I)/rows;
-
- Float ret = 0.0;
- Ret = svm. predict (m );
- Scalar rcolor;
-
- Switch (int) ret)
- {
- Case 1: rcolor = CV_RGB (100, 0, 0); break;
- Case 2: rcolor = CV_RGB (0,100, 0); break;
- Case 3: rcolor = CV_RGB (0, 0,100); break;
- }
-
- Line (img, Point (j, I), Point (j, I), rcolor );
- }
- }
-
- Imshow ("dst", img );
- WaitKey (0 );
-
- // Show support vectors
- Int sv_num = svm. get_support_vector_count ();
- For (int I = 0; I <sv_num; I ++)
- {
- Const float * support = svm. get_support_vector (I );
- Circle (img, Point (int) (support [0] * cols), (int) (support [1] * rows), 5, CV_RGB (200,200,200 ));
- }
-
- Imshow ("dst", img );
- WaitKey (0 );
-
- Return 0;
- }
-
- Int main (int argc, char ** argv)
- {
- Return newSvmTest (400,600,100 );
- }
Learning samples:
Category:
Support Vector: