在SVM opencv c ++中标记数据 [英] labeling data in SVM opencv c++
问题描述
我试图在opencv中实现SVM,使用SIFT提取的功能。我已经提取了2个不同的对象的特征(每个对象有10个不同的图像的功能,总共我有一个对象超过3000个功能),我把这些功能放在一个文件(yaml文件)..
I'm trying to implement SVM in opencv for features that I have extracted features by using SIFT. I have extracted features for 2 different objects (each object has features of 10 different images which in total I got more than 3000 features for one object) and I put those features in one file (yaml file)..
我的问题是:我不知道如何标记他们?所以我需要标记这两个文件(如我所说每个文件是yaml的类型,它包含矩阵3260 * 128和第二个对象的第二个yaml文件是3349 * 128)...
My problem is: I don't know how to label them? so I need to label these two files (as I said each file is the type of yaml and it contains matrix 3260*128 and the second yaml file for the second object is 3349*128)...
所以,请帮助我告诉我如何标记这些文件,以便训练他们...我使用openCV c ++ ..顺便说一下,openCV代码为SVM是基于LIBSVM
So please help me to show me how to label these files in order to train them later on... I'm using openCV c++.. by the way, the openCV code for SVM is based on LIBSVM
感谢您的进阶
推荐答案
正确,每行代表一个样例,你可以做什么类似于lakesh建议的:
Assume you get your matrix correctly, and each row represents one sample, what you can do is similar to what lakesh suggested:
Cv::Mat anger, disgust;
// Load the data into anger and disgust
...
// Make sure anger.cols == disgust.cols
// Combine your features from different classes into one big matrix
int numPostives = anger.rows, numNegatives = disgust.rows;
int numSamples = numPostives+numNegatives;
int featureSize = anger.cols;
cv::Mat data(numSamples, featureSize, CV_32FC1) // Assume your anger matrix is in float type
cv::Mat positiveData = data.rowRange(0, numPostives);
cv::Mat negativeData = data.rowRange(numPostives, numSamples);
anger.copyTo(positiveData);
disgust.copyTo(negativeData);
// Create label matrix according to the big feature matrix
cv::Mat labels(numSamples, 1, CV_32SC1);
labels.rowRange(0, numPositives).setTo(cv::Scalar_<int>(1));
labels.rowRange(numPositives, numSamples).setTo(cv::Scalar_<int>(-1));
// Finally, train your model
cv::SVM model;
model.train(data, labels, cv::Mat(), cv::Mat(), cv::SVMParams());
希望这有帮助。
这篇关于在SVM opencv c ++中标记数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!