资源描述:
《opencv机器学习svm支持向量机的分类程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、1.//利用SVM解决2维空间向量的3级分类问题 2. 3.#include "stdafx.h" 4. 5.#include "cv.h" 6.#include "highgui.h" 7. 8.#include 9.#include 10. 11.#include 12. 13.#include 14.using namespace std; 15. 16.int main(int argc, char **argv)
2、 17.{ 18. int size = 400; //图像的长度和宽度 19. const int s = 100; //试验点个数(可更改!!) 20. int i, j, sv_num; 21. IplImage *img; 22. CvSVM svm = CvSVM(); //★★★ 23. CvSVMParams param; 24. CvTermCriteria criteria;//停止迭代的标准 25. CvRNG
3、rng = cvRNG(time(NULL)); 26. CvPoint pts[s]; //定义1000个点 27. float data[s*2]; //点的坐标 28. int res[s]; //点的所属类 29. CvMat data_mat, res_mat; 30. CvScalar rcolor; 31. const float *support; 32. // (1)图像区域的确保和初始化 33. img
4、= cvCreateImage(cvSize(size, size), IPL_DEPTH_8U, 3); 34. cvZero(img); 35. //确保画像区域,并清0(用黑色作初始化处理)。 36. 37. // (2)学习数据的生成 38. for (i= 0; i< s; i++) { 39. pts[i].x= cvRandInt(&rng) % size; //用随机整数赋值 40. pts[i].y= cvRandInt(&rng) % size;
5、41. if (pts[i].y> 50 * cos(pts[i].x* CV_PI/ 100) + 200) { 42. cvLine(img, cvPoint(pts[i].x- 2, pts[i].y- 2), cvPoint(pts[i].x+ 2, pts[i].y+ 2), CV_RGB(255, 0, 0)); otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJ
6、ingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitte
7、eSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand1. cvLine(img, cvPoint(pts[i].x+ 2, pts[i].y- 2), cvPoint(pts[i].x- 2, pts[i].y+ 2), CV_RGB(255, 0, 0)); 2. res[i] = 1; 3. } 4. else { 5. if (pts[i].x> 200)
8、{ 6. cvLine(img, cvPoin