Ask Your Question
0

Train images with SVM [closed]

asked 2016-02-02 07:16:02 -0500

bob409 gravatar image

updated 2016-02-02 07:19:33 -0500

berak gravatar image

I have used HOGDescriptors to compute the feature vector of an image, now I have to use SVM to create training set for a class. I have to create multiclass SVM as i have 6 classes each with 10 images. I do not know how to convert vector into Mat and how to label the class to create an xml file?

hog.compute(drawing, ders, Size(1, 1), Size(0, 0));



Mat Hogfeat;
Hogfeat.create(ders.size(), 1, CV_32FC1);

for (int i = 0; i<ders.size(); i++)
{
    Hogfeat.at<float>(i, 0) = ders.at(i);


}

int labels = {1};
Mat labelsMat(1, 1, CV_32SC1, labels);

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setGamma(3);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

Ptr<ml::TrainData> tData = ml::TrainData::create(Hogfeat, ml::SampleTypes::ROW_SAMPLE, labelsMat);
svm->train(tData);

svm->save("testing.xml");
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by bob409
close date 2016-04-02 07:17:35.842536

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-02-02 09:29:07 -0500

berak gravatar image

updated 2016-02-02 23:10:18 -0500

for 6 x 10 images, you will need to put 60 hog feature vectors into a single Mat, each on a row of its own, and 60 integer labels, one for each feature:

Mat features;
Mat labels;
for (each image) // a bit pseudo here, idk, how you organize your data
{
      Mat drawing = ... // ?
      int label =   ... // ? a number between 1 and 6
      vector<float> descriptors;
      vector<Point> location;
      hog.compute(drawing, descriptors, Size( 8, 8 ), Size( 0, 0 ), location );
      Mat descr = Mat(descriptors,true).reshape(1,1); 
      features.push_back(descr);
      labels.push_back(label);
}

// now you can train the svm:
Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setGamma(3);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

Ptr<ml::TrainData> tData = ml::TrainData::create(features, ml::ROW_SAMPLE, labels);
svm->train(tData);    
svm->save("testing.xml");
edit flag offensive delete link more

Comments

descr should be vector<float>descr and then converted into matrix. Any idea on how to do it?

bob409 gravatar imagebob409 ( 2016-02-02 13:07:21 -0500 )edit

HOGDescriptor hog(cvSize(9, 9), cvSize(9, 9), cvSize(9, 9), cvSize(9, 9), 8); Mat drawing1 = img1.clone(); int label = 1; std::vector<float> ders1;

hog.compute(drawing1, ders1, Size(1, 1), Size(0, 0));
Mat mat1 = Mat(ders1);
features.push_back(mat1);
labels.push_back(label);

Mat drawing2 = img2.clone();
label = 2;
hog.compute(drawing2, ders1, Size(1, 1), Size(0, 0));
mat1 = Mat(ders1);
features.push_back(ders1);
labels.push_back(label);


Mat drawing3 = img3.clone();
label = 3;
hog.compute(drawing3, ders1, Size(1, 1), Size(0, 0));
mat1 = Mat(ders1);
features.push_back(ders1);
labels.push_back(label);

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setGamma(3);
svm->setTermCriteria(TermCriteria(TermCriteria
bob409 gravatar imagebob409 ( 2016-02-02 13:23:21 -0500 )edit

Its still not working.

bob409 gravatar imagebob409 ( 2016-02-02 13:24:21 -0500 )edit

sorry, i forgot the bit with the vector<float> . please see updated code above.

berak gravatar imageberak ( 2016-02-02 23:11:30 -0500 )edit

Thank you. Its actually working now.

bob409 gravatar imagebob409 ( 2016-02-03 03:16:07 -0500 )edit

@bob409 I followed your code, but i always get the same predict result for difference input image. Here is my code: http://pastebin.com/6y2MgTb8 Can you explain why for me?

Tuan Anh gravatar imageTuan Anh ( 2016-06-14 06:07:21 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-02-02 07:16:02 -0500

Seen: 682 times

Last updated: Feb 02 '16