带有openCV的SVM,用于对数据进行分类

时间:2014-04-18 16:59:57

标签: c++ opencv classification svm leap-motion

我有一个从跳跃运动控制器收集数据的应用程序,因为用户将其移动定义为具有特定类型的手势,因此每个手势记录都被分类在特定索引下。

用户为每个手势录制自己后,我会使用该数据做一些工作并提取片刻(如果需要更多解释,我会提供)。

在另一个应用程序中我应该根据数据集识别手势,所以我决定使用我写的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 个答案:

答案 0 :(得分:0)

好的,我发现了这个问题,由于某些原因矩阵没有用我提供的数组进行初始化,所以我用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++;
          }

      }

感谢每一个人!