使用Opencv+SVM+Hog进行行人识别的代码

正年华🍀 2020-08-29 19:46:28 1546
//��ʾͼ���ļ�  
#include <iostream>    
#include <fstream>    
#include <string>    
#include <vector> 
#include <opencv2/opencv.hpp>  
#include<opencv2/ml.hpp>
using namespace std;
using namespace cv;

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")  
void train_data(const char* data_path,const char* save_path);
void svm_test(const char* svn_data_path, const char* test_data_path);

int main()
{
    train_data("Resource/train_data.txt","svm_data.xml");
    return 1;
    vector<string> img_path;
    vector<int> img_label;

    const char* air_label = "airplanes";
    const char* train_dir_path = "Resource/train_images";
    char data_path[128] = {0};
    sprintf(data_path, "%s/%s.txt", train_dir_path, air_label);
    ifstream svm_data(data_path);
    if (svm_data.fail())return -1;
    string fileName;
    while (getline(svm_data, fileName))
    {

        char full_path[128] = { 0 };
        sprintf(full_path, "%s/%s/%s", train_dir_path, air_label, fileName.c_str());
        printf("%s\n", full_path);
        img_path.push_back(string(full_path));
    }
    svm_data.close();
    Mat data_mat, res_mat;
    int nImgNum = img_path.size();
    res_mat = Mat::zeros(nImgNum, 1, CV_32FC1);
    Mat src;
    Mat trainImg = Mat::zeros(64, 64, CV_8UC3);//��Ҫ������ͼƬ  

    for (string::size_type i = 0; i != img_path.size(); i++)
    {
        src = imread(img_path[i].c_str(), 1);
        resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);

        HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);  //������˼���ο�����1,2       
        vector<float>descriptors;//�������  
        hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����    
        if (i == 0)
        {
            data_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1); //��������ͼƬ��С���з���ռ�
        }
        int n = 0;
        for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
        {
            data_mat.at<float>(i, n) = *iter;
            n++;
        }
        res_mat.at<float>(i, 0) = i%2;

    }

    CvSVM svm;//�½�һ��SVM      
    CvSVMParams param;//�����Dz���  
    CvTermCriteria criteria;
    criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
    param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria);
    /*
    SVM���ࣺCvSVM::C_SVC
    Kernel�����ࣺCvSVM::RBF
    degree��10.0���˴β�ʹ�ã�
    gamma��8.0
    coef0��1.0���˴β�ʹ�ã�
    C��10.0
    nu��0.5���˴β�ʹ�ã�
    p��0.1���˴β�ʹ�ã�
    Ȼ���ѵ���������滯������������CvMat�͵������
    */
    //����������(5)SVMѧϰ�������������           
    svm.train(data_mat, res_mat, Mat(), Mat(), param);//ѵ����      
                                                    //�������ѵ�����ݺ�ȷ����ѧϰ����,����SVMѧϰ�����       
    svm.save("SVM_DATA.xml");

    return 1;

    //const char *pstrImageName = "Resource/train_images/airplanes/image_0001.jpg";
    //const char *pstrWindowsTitle = "OpenCV";

    ////���ļ��ж�ȡͼ��  
    //IplImage *pImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);

    ////��������  
    //cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);

    ////��ָ����������ʾͼ��  
    //cvShowImage(pstrWindowsTitle, pImage);

    ////�ȴ������¼�  
    //cvWaitKey();

    //cvDestroyWindow(pstrWindowsTitle);
    //cvReleaseImage(&pImage);
    return 0;
}
void train_data(const char* data_path, const char* save_path)
{
    vector<string> img_path;
    vector<int> img_label;
    int index = 0;
    ifstream svm_data(data_path);
    if (svm_data.fail())return;
    string line;
    while (getline(svm_data, line))
    {
        if (index % 2 == 0)
        {
            img_label.push_back(atoi(line.c_str()));
        }
        else
        {
            img_path.push_back(line);
        }

        index++;
    }
    svm_data.close();
    Mat data_mat, res_mat;
    int nImgNum = img_label.size();
    res_mat = Mat::zeros(nImgNum, 1, CV_32FC1);
    Mat src;
    Mat trainImg = Mat::zeros(64, 64, CV_8UC3);//��Ҫ������ͼƬ  

    for (string::size_type i = 0; i != nImgNum; i++)
    {
        src = imread(img_path[i].c_str(), 1);
        resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);

        HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);  //������˼���ο�����1,2       
        vector<float>descriptors;//�������  
        hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����    
        if (i == 0)
        {
            data_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1); //��������ͼƬ��С���з���ռ�
        }
        int n = 0;
        for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
        {
            data_mat.at<float>(i, n) = *iter;
            n++;
        }
        res_mat.at<float>(i, 0) = img_label[i];

    }

    CvSVM svm;//�½�һ��SVM      
    CvSVMParams param;//�����Dz���  
    CvTermCriteria criteria;
    criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
    param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria);
    /*
    SVM���ࣺCvSVM::C_SVC
    Kernel�����ࣺCvSVM::RBF
    degree��10.0���˴β�ʹ�ã�
    gamma��8.0
    coef0��1.0���˴β�ʹ�ã�
    C��10.0
    nu��0.5���˴β�ʹ�ã�
    p��0.1���˴β�ʹ�ã�
    Ȼ���ѵ���������滯������������CvMat�͵������
    */
    //����������(5)SVMѧϰ�������������           
    svm.train(data_mat, res_mat, Mat(), Mat(), param);//ѵ����      
                                                      //�������ѵ�����ݺ�ȷ����ѧϰ����,����SVMѧϰ�����       
    svm.save(save_path);

}
void svm_test(const char* svm_data_path, const char* test_data_path)
{
    CvSVM svm;
    svm.load(svm_data_path);
    vector<string> img_test_path;
    ifstream img_path_input(test_data_path);
    if (img_path_input.fail())return;
    string line;
    while (getline(img_path_input,line))
    {
        img_test_path.push_back(line);
    }
    int nImgNum = img_test_path.size();

    for (string::size_type i = 0; i != nImgNum; i++)
    {
        Mat src = imread(img_test_path[i].c_str(), 1);
        Mat trainImg = Mat::zeros(64, 64, CV_8UC3);
        resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);
        HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);  //������˼���ο�����1,2       
        vector<float>descriptors;//�������  
        hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����
        Mat svm_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1);
        int n = 0;
        for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
        {
            svm_mat.at<float>(i, n) = *iter;
            n++;
        }
        int ret = svm.predict(svm_mat);
        printf("predict:%d | path:%s\n", ret, img_test_path[i].c_str());
    }
}
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 1 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
正年华🍀
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区