OpenCV使用图片示例训练SVM错误

时间:2014-03-17 20:03:40

标签: c++ opencv svm

训练我的分类器时,我收到此错误:

图像步骤错误(矩阵不连续,因此其行数无法更改)在重塑,文件/home/denn/Downloads/opencv-2.4.6.1/modules/core/src/matrix.cpp ,第802行

在抛出' cv :: Exception'的实例后终止调用   what():/ home / denn/Downloads/opencv-2.4.6.1/modules/core/src/matrix.cpp:802:错误:( - 13)矩阵不连续,因此其行数无法更改在功能重塑

中止(核心倾销)

我正在使用C ++编写自动车牌识别项目。现在剩下的就是训练我的SVM。

在研究完这个之后,我将所有图像的大小调整为450 x 450,但错误仍然存​​在。 我研究过并环顾四周,但没有一种解决方案适合我。

任何给予的帮助都将受到高度赞赏。

    // Main entry code OpenCV

  #include <cv.h>
  #include <highgui.h>
  #include <cvaux.h>

  #include <iostream>
  #include <vector>

  using namespace std;
  using namespace cv;

   int main ( int argc, char** argv )
  {
cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
cout << "\n";

char* path_Plates;
char* path_NoPlates;
int numPlates;
int numNoPlates;
int imageWidth=450; //144
int imageHeight=450; //33

//Check if user specify image to process
if(argc >= 5 )
{
    numPlates= atoi(argv[1]);
    numNoPlates= atoi(argv[2]);
    path_Plates= argv[3];
    path_NoPlates= argv[4];

}else{
    cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
    return 0;
}        

Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

Mat trainingImages;
vector<int> trainingLabels;

for(int i=0; i< numPlates; i++)
{

    stringstream ss(stringstream::in | stringstream::out);
    ss << path_Plates << i << ".jpg";
    Mat img=imread(ss.str(), 0);
    img= img.reshape(1, 1);
    trainingImages.push_back(img);
    trainingLabels.push_back(1);
}

for(int i=0; i< numNoPlates; i++)
{
    stringstream ss(stringstream::in | stringstream::out);
    ss << path_NoPlates << i << ".jpg";
    Mat img=imread(ss.str(), 0);
    img= img.reshape(1, 1);
    trainingImages.push_back(img);
    trainingLabels.push_back(0);

}

Mat(trainingImages).copyTo(trainingData);
//trainingData = trainingData.reshape(1,trainingData.rows);
trainingData.convertTo(trainingData, CV_32FC1);
Mat(trainingLabels).copyTo(classes);

FileStorage fs("SVM.xml", FileStorage::WRITE);
fs << "TrainingData" << trainingData;
fs << "classes" << classes;
fs.release();

return 0;
 }

我编辑了代码并将其设为:

  // Main entry code OpenCV

#include <cv.h>
 #include <highgui.h>
 #include <cvaux.h>

#include <iostream>
#include <vector>
#include <iostream>

 using namespace std;
 using namespace cv;

  int main ( int argc, char** argv )
  {
  cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
  cout << "\n";

char* path_Plates;
char* path_NoPlates;
int numPlates;
int numNoPlates;
int imageWidth=450; //144
int imageHeight=450; //33

//Check if user specify image to process
if(argc >= 5 )
{
    numPlates= atoi(argv[1]);
    numNoPlates= atoi(argv[2]);
    path_Plates= argv[3];
    path_NoPlates= argv[4];

}else{
    cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
    return 0;
}        

Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

Mat trainingImages;
vector<int> trainingLabels;


Mat classes = new Mat();
Mat trainingData = new Mat();

Mat trainingImages = new Mat();
Mat trainingLabels = new Mat();

for(int i=0; i< numPlates; i++)
{

    stringstream ss(stringstream::in | stringstream::out);
    ss << path_Plates << i << ".png";
    Mat img=imread(ss.str(), 0);

    img= img.reshape(1, 1);
    trainingImages.push_back(img);
    trainingLabels.push_back(1);//trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32FC1));//trainingLabels.push_back(1);
}

for(int i=0; i< numNoPlates; i++)
{
    stringstream ss(stringstream::in | stringstream::out);
    ss << path_NoPlates << i << ".png";
    Mat img=imread(ss.str(), 0);

    img= img.reshape(1, 1); //img= img.clone().reshape(1, 1);
    trainingImages.push_back(img);
    trainingLabels.push_back(0);//trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));//trainingLabels.push_back(0);

}

trainingImages.copyTo(trainingData);
//trainingData = trainingData.reshape(1,trainingData.rows);
trainingData.convertTo(trainingData, CV_32FC1);
trainingLabels.copyTo(classes);

FileStorage fs("SVM.xml", FileStorage::WRITE);
fs << "TrainingData" << trainingData;
fs << "classes" << classes;
fs.release();

return 0;
   }

但是我在编译时遇到了这个错误:

    /home/denn/Desktop/NumberPlateRecognition/trainSVM.cpp:52:27: error: conversion from      ‘cv::Mat*’ to non-scalar type ‘cv::Mat’ requested

  /home/denn/Desktop/NumberPlateRecognition/trainSVM.cpp:52:27: error: conversion from   ‘cv::Mat*’ to non-scalar type ‘cv::Mat’ requested

 /home/denn/Desktop/NumberPlateRecognition/trainSVM.cpp:53:32: error: conversion from ‘cv::Mat*’ to non-scalar type ‘cv::Mat’ requested

  /home/denn/Desktop/NumberPlateRecognition/trainSVM.cpp:55:34: error: conversion from      ‘cv::Mat*’ to non-scalar type ‘cv::Mat’ requested

    /home/denn/Desktop/NumberPlateRecognition/trainSVM.cpp:56:34: error: conversion from        ‘cv::Mat*’ to non-scalar type ‘cv::Mat’ requested
 make[2]: *** [CMakeFiles/trainSVM.dir/trainSVM.cpp.o] Error 1
   make[1]: *** [CMakeFiles/trainSVM.dir/all] Error 2
  make: *** [all] Error 2

我有什么建议吗?

1 个答案:

答案 0 :(得分:1)

正如berak在上面的评论中指出的那样,在以下情况下,您的cv::Mat可能会不连续:

  

如果使用Mat :: col(),Mat :: diag()等提取矩阵的一部分,或者为外部分配的数据构造矩阵头,则此类矩阵可能不再具有[连续的( )]属性。

正如他们在上述参考文献中指出的那样,使用Mat::create创建矩阵并且您不会遇到此问题。

更新:

所以,正如我们的朋友berak在上面的评论中指出的那样,函数Mat::clone将为你解决问题。它调用函数Mat::create。我刚尝试了以下代码,它就像一个魅力。

Mat trainingImages;
vector<int> trainingLabels;

for(int i=0; i< numPlates; i++)
{

  stringstream ss;
  ss << path_Plates << "grumpy" << i << ".jpg";
  std::cout << ss.str() << std::endl;
  Mat img =imread(ss.str(), CV_LOAD_IMAGE_GRAYSCALE);

  if(! img.data )  {
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
  }
  else {
    img = img.clone().reshape(0,1);
    trainingImages.push_back(img);
    trainingLabels.push_back(i);
  }
}

然而,一些注意事项,看起来您可能没有正确的头文件名。我在Ubuntu 12.04上使用了以下OpenCV 2.4.8:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

此外,请确保使用OpenCV库(即opencv_core和opencv_ml)进行编译。希望能帮助您寻求板块识别。