最近我正在研究SVM,现在我需要尝试自己的SVM.xml,幸运的是这本书,#34; Master Opencv with pratical project"提供了一个TrainSVM.cpp文件来生成SVM.xml文件,我使用TrainSVM.cpp文件构建一个项目,但是在运行这个项目后没有生成任何内容,我怎么能得到这个SVM.xml文件?代码如下:
#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=144;
int imageHeight=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;
}
答案 0 :(得分:0)
首先,您需要在两个目录(例如目录名0.jpg
和1.jpg
中添加一些名为n.jpg
pos
... neg
的正面和负面图像,你有30个正面和30个负面图像)
你应该运行像
这样的程序trainSVM 30 30 ./pos/ ./neg/
我看了mentioned book's repo。没有样本正面和最初的图像。我准备了样本图像进行测试。你可以找到here
输出SVM.xml
包含TrainingData和标签。你应该训练SVM使用它。看main.cpp