其他分享
首页 > 其他分享> > OpenCV+Hog预测

OpenCV+Hog预测

作者:互联网

 

#include <iostream>
#include <opencv2/opencv.hpp>  //头文件
#include <stdio.h>  
#include <time.h>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/ml/ml.hpp>  
#include <io.h> //查找文件相关函数

using namespace cv::ml;
using namespace cv;  //包含cv命名空间
using namespace std;
Mat detector_HOG(Mat image)
{
    HOGDescriptor *hog = new HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);

    Mat featureMat;
    vector<float> descriptors;
    hog->compute(image, descriptors, Size(1, 1));
    featureMat = Mat::zeros(1, descriptors.size(), CV_32FC1);

    for (int j = 0; j < descriptors.size(); j++)
    {
        featureMat.at<float>(0, j) = descriptors[j];
    }
    return featureMat;
}

void svm_pridect_HOG(Ptr<SVM> &model, Mat test)
{
    Mat tmp;
    float validity = 0;
    float rst;
    tmp = detector_HOG(test);

    tmp.convertTo(tmp, CV_32F);

    rst = model->predict(tmp);

    cout << rst << endl;

}

int main() 
{


    // 创建分类器并设置参数
    
    
    Ptr<ml::SVM>svm = StatModel::load<SVM>("svm_hog_726_2.xml");
    Mat src= imread("0.jpg",0);
    Mat image;
    resize(src, image, Size(64, 64));;
    svm_pridect_HOG(svm, image);
}

 

标签:tmp,Hog,svm,Mat,descriptors,image,OpenCV,include,预测
来源: https://www.cnblogs.com/hsy1941/p/12432724.html