最近需要进行颜色分类的工作,查阅资料并参考 https://blog.csdn.net/ZaiYxx/article/details/50614006,修改成可在opencv4下运行的代码。
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <iostream>
int svmColorRed()
{
#if 1
// initial SVM
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
// 训练
{
int labels[6] = {1, 1, 1, 0, 0, 0};
float trainingData[6][3] ={ {255,0, 0}, {255,0,0}, {255, 0,0}, {0,0,255 },{0,0,255},{0,0,255}};
cv::Mat train_DataMat(6, 3, CV_32FC1, trainingData);
cv::Mat labelsMat(6, 1, CV_32SC1, labels);
svm->setType(cv::ml::SVM::Types::C_SVC);
svm->setKernel(cv::ml::SVM::KernelTypes::LINEAR);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6))