gpt4 book ai didi

c++ - OpenCV 3.4.1 获取自定义训练的线性 SVM HoG detectMultiScale 的原始形式

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:50 24 4
gpt4 key购买 nike

我在 OpenCV 3.4.1 中训练了一个线性 SVM。现在我想将我的自定义 SVM 与 OpenCV 3 的 HoG detectMultiScale 函数结合使用。使用自定义 SVM 原始 vector 设置 HoG 检测器的旧方法不再有效。

对于 OpenCV 2,可以像这样从 OpenCV 2 自定义训练的 SVM 中获取原始 vector :

#include "linearsvm.h"

LinearSVM::LinearSVM() {

qDebug() << "Creating SVM and loading trained data...";

load("/home/pi/trainedSVM.xml");

qDebug() << "Done loading data...";

}

std::vector<float> LinearSVM::getPrimalForm() const
{
std::vector<float> support_vector;

int sv_count = get_support_vector_count();

const CvSVMDecisionFunc* df = getDecisionFunction();

if ( !df ) {
return support_vector;
}

const double* alphas = df[0].alpha;
double rho = df[0].rho;
int var_count = get_var_count();

support_vector.resize(var_count, 0);

for (unsigned int r = 0; r < (unsigned)sv_count; r++)
{
float myalpha = alphas[r];
const float* v = get_support_vector(r);
for (int j = 0; j < var_count; j++,v++)
{
support_vector[j] += (-myalpha) * (*v);
}
}

support_vector.push_back(rho);

return support_vector;
}

一旦从经过训练的 SVM 数据创建了原始 vector ,就可以像这样设置 HoG 检测器 SVM:

    // Primal for of cvsvm descriptor
vector<float> primalVector = m_CvSVM.getPrimalForm();

qDebug() << "Got primal form of detection vector...";
qDebug() << "Setting SVM detector...";

// Set the SVM Detector - custom trained HoG Detector
m_HoG.setSVMDetector(primalVector);

在 OpenCV 3.4.1 中,这不再有效,因为 CvSVM 不再存在并且许多 SVM API 已更改。

如何在我这样训练的 OpenCV 3.4.1 中获取自定义 SVM 的原始 vector :

// Set up SVM's parameters
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::LINEAR);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 10, 1e-6));

// Train the SVM with given parameters
cv::Ptr<cv::ml::TrainData> td = cv::ml::TrainData::create(trainingDataMat, cv::ml::ROW_SAMPLE, trainingLabelsMat);

// Or auto train
qDebug() << "Training dataset...";
QElapsedTimer trainingTimer;
trainingTimer.restart();
svm->trainAuto(td);
qDebug() << "Done training dataset in: " << (float)trainingTimer.elapsed() / 1000.0f;

谢谢。

最佳答案

原来答案在 Github 上的 OpenCV 测试/示例 train_HOG.cpp 中。

看起来像这样:

/// Get the SVM Detector in HoG Format
vector<float> getSVMDetector(const Ptr<SVM>& svm)
{
// get the support vectors
Mat sv = svm->getSupportVectors();
const int sv_total = sv.rows;
// get the decision function
Mat alpha, svidx;
double rho = svm->getDecisionFunction( 0, alpha, svidx );

CV_Assert( alpha.total() == 1 && svidx.total() == 1 && sv_total == 1 );
CV_Assert( (alpha.type() == CV_64F && alpha.at<double>(0) == 1.) ||
(alpha.type() == CV_32F && alpha.at<float>(0) == 1.f) );
CV_Assert( sv.type() == CV_32F );

vector< float > hog_detector( sv.cols + 1 );
memcpy( &hog_detector[0], sv.ptr(), sv.cols*sizeof( hog_detector[0] ) );
hog_detector[sv.cols] = (float)-rho;
return hog_detector;
}

关于c++ - OpenCV 3.4.1 获取自定义训练的线性 SVM HoG detectMultiScale 的原始形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49987827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com