SVM与openCV相结合,对数据进行分类

SVM with openCV, to classify data

本文关键字:数据 分类 openCV 相结合 SVM      更新时间:2023-10-16

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

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

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

如果需要更多信息,我会提供。

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

无论如何,感谢每一个人!