Unable to train SVM Model using a vector of 2D Points using openCv
Hi,
I have three classes: 0,1 and 2. These three classes basically consist of a vector of 2D Points. I am trying to train them and use SVM For predicting a correct class,for a given set of 2D Points.
So, basically my input is std::vector<point2d> and output is just a label (like 0,1,2). I am using openCv 3.4 and I wrote the below code. I am getting lot of errors like "channels is not a member of cv:;DataType"
void getSCMCategoryClass( std::vector<Point2d> Tn, std::vector<Point2d> R1n, std::vector<Point2d> R2n, std::vector<Point2d> R3n)
{
// 1. we need to setup the traindata (a Mat of N rows, and 8 cols, each feature on a row)
// and trainlabels (a Mat with N rows, and 1 col):
std::vector<std::vector<Point2d>> trainingList;
trainingList.push_back( R1n );
trainingList.push_back( R2n );
trainingList.push_back( R3n );
Mat trainData( 3, 1, CV_32F );
Mat trainLabels( 3, 1, CV_32S );
for ( int r = 0; r < 3; r++ )
{
trainData.at<std::vector<Point2d>>( r, 0 ) = trainingList.at(r);
trainLabels.at<int>( r, 0 ) = r;
}
//// 2. now we can setup the SVM, and train it:
Ptr<SVM> svm = SVM::create();
svm->setType( SVM::C_SVC );
svm->setKernel( SVM::LINEAR );
svm->train( trainData, ROW_SAMPLE, trainLabels );
//// 3. now to the prediction (on a single sample):
Mat testData( 1, 1, CV_32F );
testData.at<std::vector<Point2d>>( 0, 0 ) = Tn;
int predicted_label = ( int )svm->predict( testData );
}
I understand that,while inserting the elements inside training Data,I cannot use data types like "point2d". How do I make the code workable?
no wait, you cannot put all contours of 1 class into the same
vector<Point2d>
.make it a
vector<vector<Point2d>>
, for ALL your train samples (not per class !) push_back() each contour, one by one, and keep track of the labels, e.g. class0 is from 0 to 30, class 1 from 30 to 80, etc.if you have 150 samples, and each feature is has 100 points, that would be a vector[150][100]
how about assigning Label? Could you once have a look at it and let me know I wrote like this:
trainLabels.at<int>( r, 0 ) = r;