HOG+SVM+INRIAPerson数据集代码

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
//#include <opencv2/gpu/gpu.hpp>
#include <fstream>
#include <iomanip>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

using namespace std;
using namespace cv;

int main()
{
#if 0
    Mat image;
    image = imread("D:\\dataset\\temp\\6.png");
    HOGDescriptor hog;
    vector<float> v_feature;
    hog.compute(image, v_feature, cv::Size(8, 8));
    //hog.compute(image, v_feature, cv::Size(16, 16));

    int featureVecSize = v_feature.size();
    cout << "****************************************haha****************" << endl;
    cout << featureVecSize<<endl;
#else
    //***************************************************************************************************
    //                                           初始化
    //***************************************************************************************************

    //char positivePath[256] = "D:\\dataset\\INRIAPerson\\Train\\pos\\";
    //char negativePath[256] = "D:\\dataset\\INRIAPerson\\Train\\neg\\";
    //char testPath[256] = "D:\\dataset\\INRIAPerson\\Test\\pos\\";
    char positivePath[256] = "D:\\dataset\\INRIAPerson\\train_64x128_H96\\pos\\";
    char negativePath[256] = "D:\\dataset\\INRIAPerson\\train_64x128_H96\\neg\\";
    char testPath[256] = "D:\\dataset\\INRIAPerson\\test_64x128_H96\\pos\\";
    char classifierSavePath[256] = "D:\\dataset\\INRIAPerson\\myResult";

    int positiveSampleCount = 614;
    int negativeSampleCount = 1218;
    //int positiveSampleCount = 100;
    //int negativeSampleCount = 100;

    int totalSampleCount = positiveSampleCount + negativeSampleCount;
    int testSampleCount = 288;

    CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount, 94500, CV_32FC1);             //参数1764?
    cvSetZero(sampleFeaturesMat);
    CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//样本标识
    cvSetZero(sampleLabelMat);

    //CvMat *testFeaturesMat = cvCreateMat(testSampleCount, 94500, CV_32FC1);             //参数1764?    正288,负453
    CvMat *testFeaturesMat = cvCreateMat(1, 94500, CV_32FC1);             //参数1764?    正288,负453
    cvSetZero(testFeaturesMat);
    CvMat *testLabelMat = cvCreateMat(testSampleCount, 1, CV_32FC1);//样本标识
    cvSetZero(testLabelMat);
    //float testLabelMat[288] = {0};
    //Mat sampleFeaturesMat();

    //***************************************************************************************************
    //                                       正例的特征
    //***************************************************************************************************
    //positive文件读入
    //ifstream fin(positivePath, ios::in);
    //if (!fin)
    //{
    //    cout << "positivePath can not open the file!" << endl;
    //    return -1;
    //}
    char positiveImage[256];
    string path;
    string s_positiveImage;
    for (int i = 0; i < positiveSampleCount; i++)
    {
        //图片名修改
        memset(positiveImage, ‘\0‘, sizeof(positiveImage));
        sprintf(positiveImage, "%d.png", i+1);                              //要改成.jpg吗
        s_positiveImage = positiveImage;
        path = positivePath + s_positiveImage;

        Mat image = imread(path);
        if (image.data == NULL)
        {
            cout << "positive image sample load error: " << i << " " << path << endl;
            system("pause");
            continue;
        }
        //hog特征提取
        //gpu::HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);    //貌似还有一些参数,重载了?
        //HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);
        HOGDescriptor hog;
        vector<float> v_feature;
        hog.compute(image, v_feature, cv::Size(8, 8));
        //hog.compute(image, v_feature, cv::Size(16, 16));

        int featureVecSize = v_feature.size();
        //cout << "****************************************haha****************" << endl;
        //cout << featureVecSize<<endl;
        //return 0;
        for (int j = 0; j<featureVecSize; j++)
        {
            //sampleFeaturesMat[i][j] = v_feature[j];
            CV_MAT_ELEM(*sampleFeaturesMat, float, i, j) = v_feature[j];                    //CV_MAT_ELEM什么意思
        }
        sampleLabelMat->data.fl[i] = 1;                                                     //.fl[]是什么
        cout << "positive" << i + 1 << "is running..." << endl;
    }
    //fclose(fin);
    //***************************************************************************************************
    //                                       负例的特征
    //***************************************************************************************************
    //negative文件读入
    //ifstream fin(negativePath, ios::in);
    //if (!fin)
    //{
    //    cout << "can not open the file!" << endl;
    //    return -1;
    //}
    char negativeImage[256] = "";                                    //初始化吗
    string s_negativeImage;
    for (int i = 0; i < negativeSampleCount; i++)
    {
        //图片名修改
        //hog特征提取
        memset(negativeImage, ‘\0‘, sizeof(negativeImage));
        sprintf(negativeImage, "%d.png", i+1);                              //要改成.jpg吗
        s_negativeImage = negativeImage;
        path = negativePath + s_negativeImage;
        //cout << "********************************************************"<<endl;
        //cout << path<<endl;
        Mat image = imread(path);
        if (image.data == NULL)
        {
            cout << "positive image sample load error: " << i << " " << path << endl;
            system("pause");
            continue;
        }
        //hog特征提取
        //gpu::HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);    //貌似还有一些参数,重载了?
        //HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);
        HOGDescriptor hog;
        vector<float> v_feature;
        hog.compute(image, v_feature, cv::Size(8, 8));

        int featureVecSize = v_feature.size();
        //cout << "***************************lalala*****************************" << endl;
        //cout << featureVecSize;

        for (int j = 0; j<featureVecSize; j++)
        {
            CV_MAT_ELEM(*sampleFeaturesMat, float, positiveSampleCount +i, j) = v_feature[j];                    //CV_MAT_ELEM什么意思
        }
        sampleLabelMat->data.fl[positiveSampleCount +i] = 0;                                                     //.fl[]是什么
        cout << "negative" << i + 1 << "is running..." << endl;
    }

    //**********************************************************************************************
    //                                         Linear SVM训练
    //**********************************************************************************************
    //设置参数
    CvSVMParams params;
    params.svm_type = SVM::C_SVC;
    params.C = 0.01;
    params.kernel_type = SVM::LINEAR;
    //params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);
    params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);

    //训练
    cout << "Starting training process" << endl;
    CvSVM svm;
    svm.train(sampleFeaturesMat, sampleLabelMat, Mat(), Mat(), params);
    cout << "Finished training process" << endl;
    //**********************************************************************************************
    //                                            结果保存
    //**********************************************************************************************
    //classifierSavePath文件读入
    //ifstream fin(classifierSavePath, ios::in);
    //if (!fin)
    //{
    //    cout << "positivePath can not open the file!" << endl;
    //    return -1;
    //}
    //svm.save(classifierSavePath);
    //cvReleaseMat(&sampleFeaturesMat);
    //cvReleaseMat(&sampleLabelMat);
    //cout << "has saved succeeded! " << endl;

    //**********************************************************************************************
    //                                          测试
    //**********************************************************************************************
    //test文件读入
    //ifstream fin(testPath, ios::in);
    //if (!fin)
    //{
    //    cout << "can not open the file!" << endl;
    //    return -1;
    //}
    char testImage[256] = "";                                    //初始化吗
    string s_testImage;
    for (int i = 0; i < testSampleCount; i++)
    {
        //图片名修改
        //hog特征提取
        memset(testImage, ‘\0‘, sizeof(testImage));
        sprintf(testImage, "%d.png", i+1);                              //要改成.jpg吗
        s_testImage = testImage;
        path = testPath + s_testImage;

        Mat image = imread(path);
        if (image.data == NULL)
        {
            cout << "positive image sample load error: " << i << " " << path << endl;
            system("pause");
            continue;
        }
        //hog特征提取
        //gpu::HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);    //貌似还有一些参数,重载了?
        //HOGDescriptor hog(Size(64, 64), Size(16, 16), Size(8, 8), Size(8, 8), 9);
        HOGDescriptor hog;
        vector<float> v_feature;
        hog.compute(image, v_feature, cv::Size(8, 8));

        int featureVecSize = v_feature.size();
        //cout << "***************************lalala*****************************" << endl;
        //cout << featureVecSize;
        for (int j = 0; j<featureVecSize; j++)
        {
            //CV_MAT_ELEM(*testFeaturesMat, float, i, j) = v_feature[j];                    //CV_MAT_ELEM什么意思
            CV_MAT_ELEM(*testFeaturesMat, float, 0, j) = v_feature[j];                    //CV_MAT_ELEM什么意思
        }
        float response = svm.predict(testFeaturesMat);
        testLabelMat->data.fl[i] = response;                                                     //.fl[]是什么
        //testLabelMat[i] = response;
    }
    float right = 0;
    for (int i = 0; i < testSampleCount; i++)
    {
        if (testLabelMat->data.fl[i] == 1)
        //if (testLabelMat[i] == 1)
        {
            right++;
        }
    }
    float radio = right / testSampleCount;
    cout << "the radio of the train is:" << radio << endl;
#endif
    return 0;
}
时间: 2024-10-06 23:17:02

HOG+SVM+INRIAPerson数据集代码的相关文章

python实现HOG+SVM对CIFAR-10数据集分类(上)

本博客只用于学习,如果有错误的地方,恳请指正,如需转载请注明出处. 看机器学习也是有一段时间了,这两天终于勇敢地踏出了第一步,实现了HOG+SVM对图片分类,具体代码可以在github上下载,https://github.com/subicWang/HOG-SVM-classifer.大家都说HOG+SVM是在行人检测中很合拍的一对.至于为啥,我也讲不清楚.我猜想这么合拍的一对应用在图片分类上效果应该也不错吧,事实证明确实还行,速度挺快,分类正确率还行.我用的数据集是http://www.cs.

第十八节、基于传统图像处理的目标检测与识别(HOG+SVM附代码)

其实在深度学习分类中我们已经介绍了目标检测和目标识别的概念.为了照顾一些没有学过深度学习的童鞋,这里我重新说明一次:目标检测是用来确定图像上某个区域是否有我们要识别的对象,目标识别是用来判断图片上这个对象是什么.识别通常只处理已经检测到对象的区域,例如,人们总是会使在已有的人脸图像的区域去识别人脸. 传统的目标检测方法与识别不同于深度学习方法,后者主要利用神经网络来实现分类和回归问题.在这里我们主要介绍如何利用OpecnCv来实现传统目标检测和识别,在计算机视觉中有很多目标检测和识别的技术,这里

利用opencv的hog+svm实现细胞识别分类器

利用opencv的hog+svm实现细胞识别分类器 图像处理中的细胞识别和人脸识别可以这样来类比,图像中的一张张人脸就是一个个细胞. 模式识别的关键在于样本库的选取,识别率也只能在和样本库比较接近的环境下才能保证.主要工作是三部分一是特征提取,二是样本库的训练train,三是目标检测detect. 一.特征提取 特征提取采用的是HOG特征即HOG描述子,该特征在行人检测中效果非常好.而一般人脸识别采用的是haar特征,这在opencv中都有实现,并都给出了相应的分类器,效果都很不错. 首先采集了

HoG SVM 目标检测分析

前一段时间开始了解HoG跟SVM行人识别,看了很多包括Dalal得前辈的文章及经验分享,对HoG理论有了些初步的认识. HoG 的全称是 Histogram of Oriented Gradient, 直译过来也就是梯度方向直方图. 就是计算各像素的梯度方向,统计成为直方图来作为特征表示目标. 下面简述一下利用HoG + SVM 实现目标检测的简要步骤 Step1:获取正样本集并用hog计算特征得到hog特征描述子.例如进行行人检测,可用IRINA等行人样本集,提取出行人的描述子. Step2:

hog&amp;svm

hog&svm的方法已经非常经典,广泛使用在多种行业,并且集成在了硬件中.应该说作为运动检测的一种重要方法,使用的情况还是非常广泛的. (opencv自带的分类器) int main() { Mat src = imread( "1.png"); HOGDescriptor hog; //HOG特征检测器 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); //设置SVM分类器为默认参数 vector&l

HOG + SVM(行人检测, opencv实现)

HOG+SVM流程 1.提取HOG特征 灰度化 + Gamma变换(进行根号求解) 计算梯度map(计算梯度) 图像划分成小的cell,统计每个cell梯度直方图 多个cell组成一个block, 特征归一化 多个block串接,并归一化 2.训练SVM分类器 原文地址:https://www.cnblogs.com/my-love-is-python/p/11110277.html

简单HOG+SVM mnist手写数字分类

使用工具 :VS2013 + OpenCV 3.1 数据集:minst 训练数据:60000张 测试数据:10000张 输出模型:HOG_SVM_DATA.xml 数据准备 train-images-idx3-ubyte.gz:  training set images (9912422 bytes) train-labels-idx1-ubyte.gz:  training set labels (28881 bytes) t10k-images-idx3-ubyte.gz:   test s

hog+svm+检测人(代替默认的参数)

#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/ml/ml.hpp>//#include <opencv2/gpu/gpu.hpp>#include <fstream>#include <iomanip> #include "opencv2/imgp

MFC基于对话框 手写数字识别 SVM+MNIST数据集

完整项目下载地址: http://download.csdn.net/detail/hi_dahaihai/9892004 本项目即拿MFC做了一个画板,画一个数字后可自行识别数字.此外还 有保存图片.清空画板功能,简单实用. 识别方法为SVM调用已经训练好的MNIST数据集"SVM_DATA.xml" MNIST数据集训练方法自行百度,一大堆. 本项目基于OpenCv 2.4.6,下载的朋友自行修改配置为自己使用的OpenCv版本即可.