简体   繁体   English

支持openCV的SVM,对数据进行分类

[英]SVM with openCV, to classify data

I have an application that collects data from the leap motion controller, as the user defines its movements as a gesture with specific type, so every record of gesture is categorized under a specific index. 我有一个应用程序可以从跳跃运动控制器收集数据,因为用户将其动作定义为具有特定类型的手势,因此每个手势记录都归类于特定索引下。

After the user records himself for each gesture, I use that data to do some work and extract moments (if more explanation needed, I will provide it). 用户为每个手势记录了自己的信息后,我将使用该数据来做一些工作并提取一些时刻(如果需要更多说明,我会提供)。

In the other application I am supposed to identify the gesture according to the sets of data, so I have decided to use SVM I wrote: 在另一个应用程序中,我应该根据数据集来识别手势,因此我决定使用我编写的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();
  }

For some reason, I cannot understand it always throws exception at: cvPreprocessCategoricalResponses error code -5 err = "response #0 is not integral" 由于某些原因,我无法理解它总是在以下cvPreprocessCategoricalResponses error code -5 err = "response #0 is not integral"引发异常: cvPreprocessCategoricalResponses error code -5 err = "response #0 is not integral"

If more information is needed, I will provide it. 如果需要更多信息,我会提供。

Ok , i found the problem , for some reason the matrix did not got initialized with the array i have supply , so i initiated the matrix with a for loop 好的,我发现了问题,由于某种原因,矩阵未使用我提供的数组进行初始化,因此我使用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++;
          }

      }

any way thanks every one! 一定要谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM