Skip to content

Instantly share code, notes, and snippets.

@arumoy
Last active February 12, 2017 22:10
Show Gist options
  • Save arumoy/1a4a7f458811ac59c2cb7609b760663b to your computer and use it in GitHub Desktop.
Save arumoy/1a4a7f458811ac59c2cb7609b760663b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/core.hpp>
using namespace cv;
const std::string TRAINING_DATA_ROOT = "E:\\SVM_Training\\";
std::string acquire_training_data_color(const std::string col);
std::string acquire_training_data_color(const std::string col) {
return TRAINING_DATA_ROOT + col + "\\";
}
int main()
{
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::LINEAR); // Or POLY
svm->setGamma(1); // Or 1
svm->setDegree(0);
svm->setCoef0(0);
svm->setC(1);
svm->setNu(0);
svm->setP(0);
svm->setTermCriteria(cv::TermCriteria(CV_TERMCRIT_ITER, 1000, 0.01));
cv::Mat imr = cv::imread(acquire_training_data_color("red") + "1.bmp", cv::IMREAD_COLOR);
Mat bir = imr.reshape(0, 1);
cv::Mat imb = cv::imread(acquire_training_data_color("blue") + "1.bmp", cv::IMREAD_COLOR);
Mat bib = imb.reshape(0, 1);
cv::Mat trainData, labels;
for (int i = 0; i < 10; i++)
{
trainData.push_back(bir);
labels.push_back(1);
trainData.push_back(bib);
labels.push_back(2);
}
cv::Ptr<cv::ml::TrainData> trainD;
try
{
trainD = cv::ml::TrainData::create(trainData, cv::ml::ROW_SAMPLE, labels);
}
catch (const std::exception& ex)
{
std::cerr << ex.what();
}
/*svm->train(trainD);
std::cout << svm->predict(bib);*/
std::cout << trainData.rows << " x " << trainData.cols << std::endl;
std::cout << labels.rows << " x " << labels.cols << std::endl;
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment