之前因为忘记把float型的class名四舍五入取整,而是直接取整,所以结果不对。
本来不想追究了,没想到还有人感兴趣。
机器学习函数库依然用的OpenCV。
1 //////////////////////////////////////////////////////////////////////////
2 // File Name: ann_point_test.cpp //
3 // Author: Ruoruo(du@in.tum.de) //
4 //////////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "cv.h"
7 #include "highgui.h"
8 #include <ml.h>
9 #include <time.h>
10 #include <ctype.h>
11 #include <vector>
12 #include <math.h>
13 #include <iostream>
14 using namespace std;
15
16 static CvScalar colors[] =
17 {
18 {{0,0,255}},
19 {{0,128,255}},
20 {{0,255,255}},
21 {{0,255,0}},
22 //{{255,128,0}},
23 //{{255,255,0}},
24 {{255,0,0}},
25 {{255,0,255}}
26 };
27
28 int main( int argc, char** argv )
29 {
30 vector<float> point;
31 vector<float> result;
32
33 float p[20] = { 0.4, 0.4,
34 0.5, 0.5,
35 2.2, 2.3,
36 2.3, 2.2,
37 4.3, 4.2,
38 4.2, 4.1,
39 4.3, 0.3,
40 4.4, 0.4,
41 0.3, 4.0,
42 0.4, 4.1 };
43 float res[10] = { 1,1,2,2,3,3,4,4,5,5 };
44 int i;
45 for(i=0; i<10; i++)
46 {
47 point.push_back(p[i]);
48 if(i<5) result.push_back(res[i]);
49 }
50
51 CvMat* input = cvCreateMat( 10, 2, CV_32FC1 );
52 cvInitMatHeader( input, 10, 2, CV_32FC1, p );
53 CvMat* output = cvCreateMat( 10, 1, CV_32FC1 );
54 cvInitMatHeader( output, 10, 1, CV_32FC1, res );
55 IplImage* img = cvCreateImage(cvSize(450, 450), IPL_DEPTH_8U, 3);
56 img->origin = 1;
57 for(i= 0; i<10; i++)
58 {
59 cvCircle(img, cvPoint((int)(p[i*2]*100), (int)(p[i*2+1]* 100)), 5, colors[(int)res[i]%8], 1, CV_AA, 0);
60 }
61
62 int layer_num[3] = { 2, 3, 1 };
63 CvMat* layer_size = cvCreateMatHeader( 1, 3, CV_32S );
64 cvInitMatHeader( layer_size, 1, 3, CV_32S, layer_num );
65 CvANN_MLP ann;
66 ann.create( layer_size, CvANN_MLP::SIGMOID_SYM, 1, 1 );
67 CvANN_MLP_TrainParams params;
68 params.term_crit = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30000, 0.0001 );
69 params.train_method = 0;
70 params.bp_dw_scale = 0.1;
71 params.bp_moment_scale = 0.1;
72 cout<<"begin training
"<<endl;
73 ann.train( input, output, 0, 0, params );
74 cout<<"end training
"<<endl;
75
76 //begin to test
77 float testp[10] = { 0.4, 0.3,
78 2.5, 2.4,
79 4.1, 4.3,
80 4.4, 0.5,
81 0.5, 4.2 };
82 CvMat* test_point = cvCreateMat( 1, 2, CV_32FC1 );
83 CvMat* test_result = cvCreateMat( 1, 1, CV_32FC1 );
84 CvFont font;
85 double hScale=0.5;
86 double vScale=0.5;
87 int lineWidth=1;
88 cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
89
90 for(i= 0; i<5; i++)
91 {
92 cvSetReal2D( test_point, 0, 0, testp[2*i] );
93 cvSetReal2D( test_point, 0, 1, testp[2*i+1] );
94 ann.predict(test_point, test_result);
95 cout<<cvRound(cvmGet(test_result,0,0))<<endl;
96
97 cvCircle( img, cvPoint((int)(testp[i*2]*100), (int)(testp[i*2+1]* 100)), 0, colors[cvRound(cvmGet(test_result,0,0))%8], 10, CV_AA, 0 );
98
99 char buffer[10];
100 _itoa(i+1,buffer,10);
101 string point_id(buffer);
102 cvPutText(img, point_id.c_str(), cvPoint(testp[2*i]*100,testp[2*i+1]*100), &font, cvScalar(255,255,255));
103
104 cout<<i<<": "<<"("<<testp[i*2]<<", "<<testp[i*2+1]<<")"<<"\t"<<cvmGet(test_result,0,0)<<endl;
105 }
106
107 cvNamedWindow( "Coordinates" , 1 );
108 cvShowImage( "Coordinates" ,img);
109
110 cvWaitKey( 0 );
111
112 cvDestroyWindow("Coordinates");
113 cvReleaseImage(&img);
114
115 return 0;
116 }
2 // File Name: ann_point_test.cpp //
3 // Author: Ruoruo(du@in.tum.de) //
4 //////////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "cv.h"
7 #include "highgui.h"
8 #include <ml.h>
9 #include <time.h>
10 #include <ctype.h>
11 #include <vector>
12 #include <math.h>
13 #include <iostream>
14 using namespace std;
15
16 static CvScalar colors[] =
17 {
18 {{0,0,255}},
19 {{0,128,255}},
20 {{0,255,255}},
21 {{0,255,0}},
22 //{{255,128,0}},
23 //{{255,255,0}},
24 {{255,0,0}},
25 {{255,0,255}}
26 };
27
28 int main( int argc, char** argv )
29 {
30 vector<float> point;
31 vector<float> result;
32
33 float p[20] = { 0.4, 0.4,
34 0.5, 0.5,
35 2.2, 2.3,
36 2.3, 2.2,
37 4.3, 4.2,
38 4.2, 4.1,
39 4.3, 0.3,
40 4.4, 0.4,
41 0.3, 4.0,
42 0.4, 4.1 };
43 float res[10] = { 1,1,2,2,3,3,4,4,5,5 };
44 int i;
45 for(i=0; i<10; i++)
46 {
47 point.push_back(p[i]);
48 if(i<5) result.push_back(res[i]);
49 }
50
51 CvMat* input = cvCreateMat( 10, 2, CV_32FC1 );
52 cvInitMatHeader( input, 10, 2, CV_32FC1, p );
53 CvMat* output = cvCreateMat( 10, 1, CV_32FC1 );
54 cvInitMatHeader( output, 10, 1, CV_32FC1, res );
55 IplImage* img = cvCreateImage(cvSize(450, 450), IPL_DEPTH_8U, 3);
56 img->origin = 1;
57 for(i= 0; i<10; i++)
58 {
59 cvCircle(img, cvPoint((int)(p[i*2]*100), (int)(p[i*2+1]* 100)), 5, colors[(int)res[i]%8], 1, CV_AA, 0);
60 }
61
62 int layer_num[3] = { 2, 3, 1 };
63 CvMat* layer_size = cvCreateMatHeader( 1, 3, CV_32S );
64 cvInitMatHeader( layer_size, 1, 3, CV_32S, layer_num );
65 CvANN_MLP ann;
66 ann.create( layer_size, CvANN_MLP::SIGMOID_SYM, 1, 1 );
67 CvANN_MLP_TrainParams params;
68 params.term_crit = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30000, 0.0001 );
69 params.train_method = 0;
70 params.bp_dw_scale = 0.1;
71 params.bp_moment_scale = 0.1;
72 cout<<"begin training
"<<endl;73 ann.train( input, output, 0, 0, params );
74 cout<<"end training
"<<endl;75
76 //begin to test

77 float testp[10] = { 0.4, 0.3,
78 2.5, 2.4,
79 4.1, 4.3,
80 4.4, 0.5,
81 0.5, 4.2 };
82 CvMat* test_point = cvCreateMat( 1, 2, CV_32FC1 );
83 CvMat* test_result = cvCreateMat( 1, 1, CV_32FC1 );
84 CvFont font;
85 double hScale=0.5;
86 double vScale=0.5;
87 int lineWidth=1;
88 cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
89
90 for(i= 0; i<5; i++)
91 {
92 cvSetReal2D( test_point, 0, 0, testp[2*i] );
93 cvSetReal2D( test_point, 0, 1, testp[2*i+1] );
94 ann.predict(test_point, test_result);
95 cout<<cvRound(cvmGet(test_result,0,0))<<endl;
96
97 cvCircle( img, cvPoint((int)(testp[i*2]*100), (int)(testp[i*2+1]* 100)), 0, colors[cvRound(cvmGet(test_result,0,0))%8], 10, CV_AA, 0 );
98
99 char buffer[10];
100 _itoa(i+1,buffer,10);
101 string point_id(buffer);
102 cvPutText(img, point_id.c_str(), cvPoint(testp[2*i]*100,testp[2*i+1]*100), &font, cvScalar(255,255,255));
103
104 cout<<i<<": "<<"("<<testp[i*2]<<", "<<testp[i*2+1]<<")"<<"\t"<<cvmGet(test_result,0,0)<<endl;
105 }
106
107 cvNamedWindow( "Coordinates" , 1 );
108 cvShowImage( "Coordinates" ,img);
109
110 cvWaitKey( 0 );
111
112 cvDestroyWindow("Coordinates");
113 cvReleaseImage(&img);
114
115 return 0;
116 }
小圆圈为10个训练点,实点为5个测试点,不同颜色代表不同分类。

怎么训练
不错看一下你程序 顺便了解下机器学习里的函数。。。是不是离某个类的欧几里得距离近 就是那个类的
不是欧几里得距离,就是ann,神经网络训练。
欧几里得是另外的方法。
这空间我都废了,居然还有人看。。。
那为啥最后的测试结果 你挑了个离某个类几何距离近的 点 就被归为那个类呢。。。
ann的算法跟距离无关,但是结果你可以看成是几何距离近的是一类。
上面那么说是怕你混淆,欧氏距离和马氏距离都是独立的一种分类算法。