下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. /利用 svm 解决 2 维空间向量的3 级分类问题2.3. #include “stdafx.h“ 4.5. #include “cv.h“6. #include “highgui.h“ 7.8. #include <ml.h>9. #include <time.h> 10.11. #include <ctype.h> 12.13. #include <iostream>14. using namespace std; 15.16. int main(int argc, char *argv)17. 18. int size = 400;
2、/图像的长度和宽度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 rng = cvrng(time(null);26. cvpoint ptss;/定义 1000 个点27. float datas*2;/点的坐标28. int ress;/点的所属类29. cvmat data_mat, res_mat;30.
3、cvscalar rcolor;31. const float *support;32. / (1)图像区域的确保和初始化33. img= cvcreateimage(cvsize(size, size), ipl_depth_8u, 3);34. cvzero(img);35. /确保画像区域,并清0(用黑色作初始化处理)。36.37./ (2)学习数据的生成38.for (i= 0; i< s; i+) 39. ptsi.x= cvrandint(&rng) % size;/用随机整数赋值40. ptsi.y= cvrandint(&rng) % size;41.if
4、 (ptsi.y> 50 * cos(ptsi.x* cv_pi/ 100) + 200) 42. cvline(img, cvpoint(ptsi.x- 2, ptsi.y- 2), cvpoint(ptsi.x+ 2, ptsi.y+ 2), cv_rgb(255, 0, 0);43. cvline(img, cvpoint(ptsi.x+ 2, ptsi.y- 2), cvpoint(ptsi.x- 2, ptsi.y+ 2), cv_rgb(255, 0, 0);44.resi = 1;45.46.else 47.if (ptsi.x> 200) 48. cvline(im
5、g, cvpoint(ptsi.x- 2, ptsi.y- 2), cvpoint(ptsi.x+ 2, ptsi.y+ 2), cv_rgb(0, 255, 0);49. cvline(img, cvpoint(ptsi.x+ 2, ptsi.y- 2), cvpoint(ptsi.x- 2, ptsi.y+ 2), cv_rgb(0, 255, 0);50.resi = 2;51.52. else 53. cvline(img, cvpoint(ptsi.x- 2, ptsi.y- 2), cvpoint(ptsi.x+ 2, ptsi.y+ 2), cv_rgb(0, 0, 255);5
6、4. cvline(img, cvpoint(ptsi.x+ 2, ptsi.y- 2), cvpoint(ptsi.x- 2, ptsi.y+ 2), cv_rgb(0, 0, 255);55.resi = 3;56.57.58.59./生成2 维随机训练数据,并将其值放在 cvpoint 数据类型的数组pts 中。60.61./ (3)学习数据的显示62.cvnamedwindow(“svm“, cv_window_autosize);63.cvshowimage(“svm“, img);64.cvwaitkey(0);65.66./ (4)学习参数的生成67.for (i= 0; i&l
7、t; s; i+) 68.datai* 2 = float (ptsi.x) / size;69.datai* 2 + 1 = float (ptsi.y) / size;70.71.cvinitmatheader(&data_mat, s, 2, cv_32fc1, data);72.cvinitmatheader(&res_mat, s, 1, cv_32sc1, res);73.criteria= cvtermcriteria(cv_termcrit_eps, 1000, flt_epsilon);74.param= cvsvmparams (cvsvm:c_svc, c
8、vsvm:rbf, 10.0, 8.0, 1.0, 10.0,0.5,0.1, null, criteria);75./*76.svm 种类:cvsvm:c_svc77.kernel 的种类:cvsvm:rbf78.degree:10.0(此次不使用)79.gamma :8.080.coef0 :1.0 (此次不使用)81.c:10.082.nu :0.5(此次不使用)83.p:0.1(此次不使用)84.然后对训练数据正规化处理,并放在 cvmat 型的数组里。85.*/86.87.88./(5)svm 学习89.svm.train(&data_mat, &res_mat, n
9、ull, null, param); /90./利用训练数据和确定的学习参数,进行 svm 学习91.92./ (6)学习结果的绘图93.for (i= 0; i< size; i+) 94.for (j= 0; j< size; j+) 95. cvmat m;96. float ret = 0.0;97. float a = float (j) / size, float (i) / size ;98.cvinitmatheader(&m, 1, 2, cv_32fc1, a);99.ret= svm.predict(&m);100.switch (int) r
10、et) 101.case 1:102.rcolor= cv_rgb(100, 0, 0);103.break;104.case 2:105.rcolor= cv_rgb(0, 100, 0);106.break;107.case 3:108.rcolor= cv_rgb(0, 0, 100);109.break;110.111.cvset2d(img, i, j, rcolor);112.113.114./为了显示学习结果,通过输入图像区域的全部像素(特征向量)并进行分类。然后对输入像素用所属等级的颜色绘图。115.116./ (7)训练数据的再绘制117.for (i= 0; i< s
11、; i+) 118.cvscalar rcolor;119.switch (resi) 120.case 1:121.rcolor= cv_rgb(255, 0, 0);122.break;123.case 2:ptsi.y+ 2), rcolor);131.cvline(img, cvpoint(ptsi.x+ 2, ptsi.y- 2), cvpoint(ptsi.x- 2, ptsi.y+ 2), rcolor);140.141.142.143.144.145.146.147.148.149.150.size), 5, cv_rgb(200, 200, 200);/用白色的圆圈对支持向量
12、作标记。/ (9)图像的显示cvnamedwindow(“svm“, cv_window_autosize); cvshowimage(“svm“, img);cvwaitkey(0); cvdestroywindow(“svm“); cvreleaseimage(&img); return 0;/显示实际处理结果的图像,直到某个键被按下为止。151.124.rcolor= cv_rgb(0, 255, 0);125.break;126.case 3:127.rcolor= cv_rgb(0, 0, 255);128.break;129.130.cvline(img, cvpoint(ptsi.x- 2, ptsi.y- 2), cvpoint(ptsi.x+ 2,132.133./将训练数据在结果图像上重复的绘制出来。134.135./
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年江苏第二师范学院教师招聘考试备考题库及答案解析
- 2026年山西信息职业技术学院教师招聘考试参考题库及答案解析
- 2026年岭南师范学院教师招聘笔试备考试题及答案解析
- (2026年)医疗质量与安全管理控制指标(范文)
- 2026年新冠病毒感染防控试题及答案
- 2026年高考化学十校联考全真模拟试卷(五)及答案
- 2026海南海口市琼山区赴高校面向2026年应届毕业生招聘教师84人备考题库(一)参考答案详解
- 室外保洁服务合同协议2026年基础版市政适用三篇
- 2026年山西省太原市中考英语模拟试卷(含答案)
- 2026年机械设计制造及其自动化考试
- 宅基地永久转让协议
- 幽门螺杆菌科普
- 推拿手法完整版本
- 高中数学课本中的定理公式结论的证明
- 地下水监测井建设规范
- 冠脉介入并发症的防治课件
- 多产权建筑消防安全管理
- 岳飞《满江红写怀》课堂实用课件
- 网络安全渗透测试PPT完整全套教学课件
- 道氏理论课件
- 方方小说-《武昌城》
评论
0/150
提交评论