问题描述
- OpenCV中CV_SVM中单类如何实现代码?
-
我现在想利用CV_SVM中的ONE_CLASS进行单类分类方法,但是又不知道该怎么样使用函数以及具体参数的设置。
以下是我参照多类别分类的代码,但是运行时报错,希望懂得人给点指导。#include "stdafx.h"
#include
#include
#include
#include
#includeusing namespace std;
int main(int arg,char **argv)
{
const int s=1000;
int size= 400;
int i,j,svm_num;IplImage *img; img = cvCreateImage(cvSize(size,size),IPL_DEPTH_8U,3); cvZero(img); CvSVM svm = CvSVM(); CvSVMParams param; CvTermCriteria criteria; CvRNG rng = cvRNG(time(NULL)); CvPoint pts[s]; float data[s*2]; float space[s*2]; int res[s]; CvMat data_mat,res_mat; CvScalar rcolor; const float *support; int n=0; for (i=0; i<s; i++) { pts[i].x = cvRandInt(&rng) %size; pts[i].y = cvRandInt(&rng) %size; if ((pts[i].y > 50 && pts[i].y < 100) && (pts[i].x >50 &&pts[i].x<100)) //根据指定位置设定想要的范围,这些也是仅有的正样本。 { 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)); 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)); res[n] =1; //记录样本 space[n*2] = float(pts[i].x)/size; space[n*2 +1] = float(pts[i].y)/size; n++; } else { cvLine(img, cvPoint(pts[i].x - 2,pts[i].y-2), cvPoint(pts[i].x+2,pts[i].y+2),CV_RGB(0,255,0)); cvLine (img, cvPoint (pts[i].x + 2, pts[i].y - 2), cvPoint (pts[i].x - 2, pts[i].y + 2), CV_RGB (0,255, 0)); } } cvNamedWindow("SVM",CV_WINDOW_AUTOSIZE); cvShowImage("SVM",img); cvWaitKey(0); float *data1 = new float[n*2]; int *res1 = new int[n]; for(i=0; i<n; i++) { data1[i*2] = space[i*2]; data1[i*2+1] =space[i*2+1]; res1[i] = res[i]; }
/* for (i=0; i<s; i++)
{
data[i 2] = float(pts[i].x) /size ;
data[i*2 + 1] = float(pts[i].y)/size;
}/cvInitMatHeader(&data_mat, s , 2 ,CV_32FC1, data1); cvInitMatHeader(&res_mat, s , 1,CV_32SC1 , res1); criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
//参数设置ONE_CLASS
param = CvSVMParams(CvSVM::ONE_CLASS, CvSVM::RBF, 10.0 , 8.0 , 1.0, 10.0, 0.5, 0.1,NULL,criteria);
//这个地方有错,不知道是参数问题 还是用法问题?
//对样本进行训练
svm.train(&data_mat,&res_mat,NULL,NULL,param);svm_num = svm.get_support_vector_count(); for (i=0; i<svm_num; i++) { support = svm.get_support_vector(i); cvCircle (img, cvPoint ((int) (support[0] * size), (int) (support[1] * size)), 5, CV_RGB (200, 200, 200)); } cvNamedWindow ("SVM", CV_WINDOW_AUTOSIZE); cvShowImage ("SVM", img); cvWaitKey (0); return 1;
}
解决方案
opencv不是开源的么,你把源代码搞出来看看
或者参考一下啊这个
http://www.cnblogs.com/justany/archive/2012/11/23/2784125.html
时间: 2024-10-06 06:16:26