使用hu矩训练SVM
Training an SVM using hu moments
im正在学习SVM,因此我制作了一个样本程序,训练SVM来检测符号是否在图像中。所有的图像都是黑白的(符号是黑色的,背景是白色的)。我有12张训练图像,6张阳性(带符号)和6张阴性(不带符号)。Im使用hu moments
来获得每个图像的描述符,然后用这些描述符构造训练矩阵。我还有一个Labels
矩阵,它包含每个图像的标签:如果是正的,则为1,如果是负的,则是0。但我在训练SVM的线路上出现了错误(有点像分割错误)。这是我的代码:
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
//arrays where the labels and the features will be stored
float labels[12] ;
float trainingData[12][7] ;
Moments moment;
double hu[7];
//===============extracting the descriptos for each positive image=========
for ( int i = 0; i <= 5; i++){
//the images are called t0.png ... t5.png and are in the folder train
std::string path("train/t");
path += std::to_string(i);
path += ".png";
Mat input = imread(path, 0); //read the images
bitwise_not(input, input); //invert black and white
Mat BinaryInput;
threshold(input, BinaryInput, 100, 255, cv::THRESH_BINARY); //apply theshold
moment = moments(BinaryInput, true); //calculate the moments of the current image
HuMoments(moment, hu); //calculate the hu moments (this will be our descriptor)
//setting the row i of the training data as the hu moments
for (int j = 0; j <= 6; j++){
trainingData[i][j] = (float)hu[j];
}
labels[i] = 1; //label=1 because is a positive image
}
//===============extracting the descriptos for each negative image=========
for (int i = 0; i <= 5; i++){
//the images are called tn0.png ... tn5.png and are in the folder train
std::string path("train/tn");
path += std::to_string(i);
path += ".png";
Mat input = imread(path, 0); //read the images
bitwise_not(input, input); //invert black and white
Mat BinaryInput;
threshold(input, BinaryInput, 100, 255, cv::THRESH_BINARY); //apply theshold
moment = moments(BinaryInput, true); //calculate the moments of the current image
HuMoments(moment, hu); //calculate the hu moments (this will be our descriptor)
for (int j = 0; j <= 6; j++){
trainingData[i + 6][j] = (float)hu[j];
}
labels[i + 6] = 0; //label=0 because is a negative image
}
//===========================training the SVM================
//we convert the labels and trainingData matrixes to Mat objects
Mat labelsMat(12, 1, CV_32FC1, labels);
Mat trainingDataMat(12, 7, CV_32FC1, trainingData);
//create the SVM
Ptr<ml::SVM> svm = ml::SVM::create();
//set the parameters of the SVM
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
svm->setTermCriteria(criteria);
//Train the SVM !!!!!HERE OCCURS THE ERROR!!!!!!
svm->train(trainingDataMat, ml::ROW_SAMPLE, labelsMat);
//Testing the SVM...
Mat test = imread("train/t1.png", 0); //this should be a positive test
bitwise_not(test, test);
Mat testBin;
threshold(test, testBin, 100, 255, cv::THRESH_BINARY);
Moments momentP = moments(testBin, true); //calculate the moments of the test image
double huP[7];
HuMoments(momentP, huP);
Mat testMat(1, 7, CV_32FC1, huP); //setting the hu moments to the test matrix
double resp = svm->predict(testMat); //pretiction of the SVM
printf("%f", resp); //Response
getchar();
}
我知道程序在那一行之前运行得很好,因为我打印了labelsMat
和trainingDataMat
,它们里面的值都还可以。即使在控制台中,我也可以看到程序在那行执行之前运行得不错。控制台随后显示以下消息:
OpenCV error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainDatam or pass integer responses)
我真的不知道这意味着什么。你知道是什么导致了这个问题吗?如果你需要任何其他细节,请告诉我。
编辑
面向未来读者:
问题在于我将CCD_ 5阵列定义为float的阵列并且将LabelsMat
定义为CCD_。包含标签的数组内部需要有整数,所以我更改了:
float labels[12];
至
int labels[12];
还改变了
Mat labelsMat(12, 1, CV_32FC1, labels);
至
Mat labelsMat(12, 1, CV_32SC1, labels);
这就解决了错误。感谢
尝试更改:
Mat labelsMat(12, 1, CV_32FC1, labels);
至
Mat labelsMat(12, 1, CV_32SC1, labels);
发件人:http://answers.opencv.org/question/63715/svm-java-opencv-3/
如果这不起作用,希望其中一个帖子能帮助你:
Opencv 3.0 SVM列车分类问题
OpenCV SVM训练数据
相关文章:
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 使用 SVM 进行分类,使用来自 Bag of Word 的词汇构建
- SVM 规模:找不到命令
- 将svm分类器设置为HOG检测器
- SVM 训练时间是否取决于输入数据的内容?
- OPENCV 3.4.1获取经过定制训练的线性SVM HOG DETECTMULTISCALE的原始形式
- 如何使用 dlib 训练或合并多个 .svm 并检测多个类
- 如何在OPENCV 2.4.13中导入训练有素的SVM检测器
- 如何使用2通道垫子中的OPENCV中使用SVM
- 如何在C++代码中使用 SVM Light?(可能无需从C++代码调用可执行文件)
- 贝叶斯和SVM分类器在OpenCV3中的实现
- OPENCV SVM-对象不属于任何受过训练的类
- C++中的对象序列化,用于存储 OpenCV 的 SVM 对象 [OpenCV 3.1.0]
- 断言 sv_count !=0 失败 - 函数 train_auto,SVM 类型 - EPS_SVR
- 支持向量机 (SVM) 与 K-NN 测试效率
- OpenCV 3 and SVM training
- 如何通过安卓NDK加载svm训练文件
- 在C++中使用 Matlab SVM 模型
- OpenCV SVM 分类器图像识别
- 使用hu矩训练SVM