SVM 与 openCV,对数据进行分类
Posted
技术标签:
【中文标题】SVM 与 openCV,对数据进行分类【英文标题】:SVM with openCV, to classify data 【发布时间】:2014-06-03 05:29:25 【问题描述】:我有一个从跳跃运动控制器收集数据的应用程序,因为用户将其运动定义为具有特定类型的手势,因此每个手势记录都归类在特定索引下。
在用户记录自己的每个手势后,我使用该数据做一些工作并提取时刻(如果需要更多解释,我会提供)。
在另一个应用程序中,我应该根据数据集来识别手势,所以我决定使用我写的 SVM:
void CRecognition::SVM::SVMTrain()
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
int numberofsamples = m_gMap.size();
float ** labels;
labels = new float*[numberofsamples];
int numofTotMoments = 0;
for(int i = 0 ; i < numberofsamples ; i++)
numofTotMoments += m_gMap[i]->getNumofSamples();
labels[i] = new float[1];
labels[i][0] = (float)(i+1);
double ** Newlabels = new double *[numofTotMoments];
double ** templbls = Newlabels;
double ** trainingData = new double *[numofTotMoments];
double ** temp = trainingData;
for (int i = 0 ; i < numberofsamples ; i++)
Utils::Gesture * g = m_gMap[i];
for (int j = 0 ; j < m_gMap[i]->getNumofSamples() ; j++)
*templbls = new double [1];
*templbls[0] = (double)i+1;
*temp = (*g)[j]; //direct the pointer to an vector of moments of that gesture
temp++;
templbls++;
Mat matlabesls(numofTotMoments,1, CV_32FC1, Newlabels);
Mat mattrainingDataMat(numofTotMoments, NUM_OF_MOMENTS, CV_32FC1,trainingData);
try
// ... Contents of your main
m_svm.train(mattrainingDataMat,matlabesls,Mat(),Mat(),params);
catch ( cv::Exception & e )
cout << e.msg() << endl;
cout<< "hh";
this->SaveSVM();
由于某种原因,我无法理解它总是抛出异常
在:cvPreprocessCategoricalResponses error code -5 err = "response #0 is not integral"
如果需要更多信息,我会提供。
【问题讨论】:
【参考方案1】:好的,我发现了问题,由于某种原因,矩阵没有用我提供的数组初始化,所以我用 for 循环启动了矩阵
int countRow = 0;
for (int i = 0 ; i < numberofsamples ; i++)
Utils::Gesture * g = m_gMap[i];
for (int j = 0 ; j < m_gMap[i]->getNumofSamples() ; j++)
*templbls = new float [1];
*templbls[0] = (float)i+1;
matlabesls1.at<float>(countRow,0) = (float)i+1;
templbls++;
*temp = new float[NUM_OF_MOMENTS];
for (int k = 0 ; k < NUM_OF_MOMENTS ; k++)
float num = (float)((*g)[j])[k];
mattrainingDataMat1.at<float>(countRow,k) = num;
(*temp)[k] = num; //direct the pointer to an vector of moments of that gesture
temp++;
countRow++;
感谢每一个人!
【讨论】:
以上是关于SVM 与 openCV,对数据进行分类的主要内容,如果未能解决你的问题,请参考以下文章