简体   繁体   English

在SVM opencv c ++中标记数据

[英]labeling data in SVM opencv c++

I'm trying to implement SVM in opencv for features that I have extracted features by using SIFT. 我正在尝试在opencv中为我使用SIFT提取功能的功能实现SVM。 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).. 我已经为2个不同的对象提取了特征(每个对象具有10个不同图像的特征,总共我为一个对象提供了超过3000个特征)并且我将这些特征放在一个文件中(yaml文件)。

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)... 所以我需要标记这两个文件(因为我说每个文件是yaml的类型,它包含矩阵3260 * 128,第二个对象的第二个yaml文件是3349 * 128)...

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 所以请帮我告诉我如何标记这些文件以便以后训练它们......我正在使用openCV c ++ ..顺便说一句,SVM的openCV代码基于LIBSVM

Thank you in advanced 先谢谢你

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());

Hope this helps. 希望这可以帮助。

Labeling is easy. 贴标很容易。 Just label one of the classes/objects as 1 and the other as -1. 只需将其中一个类/对象标记为1,将另一个标记为-1即可。

                  case 'Anger'
                     CVTrainLabel = [CVTrainLabel;1];
                     Hist = UniformLBP2(I1);
                     CVTrainVec = [CVTrainVec;Hist];
                     continue;
                 case 'Disgust'
                    CVTrainLabel = [CVTrainLabel;-1];
                     Hist = UniformLBP2(I1);
                     CVTrainVec = [CVTrainVec;Hist];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM