HOG+SVM实现行人检测(三) 行人检测(带hard样本)

1,示例代码

#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
#include <sys/time.h>

using namespace std;
using namespace cv;
using namespace cv::ml;


#define PosSamNO 2416  //原始正样本数
#define NegSamNO 6070 // 剪裁后的负样本数6070
#define cropNegNum 1214  //原始负样本数

#define HardExampleNO 10896 // hard negative num//10896
#define AugPosSamNO 0 //Aug positive num

#define TRAIN true //是否进行训练,true表示重新训练,false表示读取xml文件中的SVM模型
#define CENTRAL_CROP true //true:训练时,对96*160的INRIA正样本图片剪裁出中间的64*128大小人体
#define crop_negsample true //随机剪裁负样本数的开关

/*********************************    随机剪裁负样本   *******************************************/
void crop_negsample_random()
{
    string imgName;
    char saveName[200];
    //读入文件txt名
    ifstream fileNeg("/Users/macbookpro/CLionProjects/pedestrian_detection/img_dir/sample_neg.txt");

    int num=0;
    //如果文件存在,则先删除该文件
    //写入文件txt名
    ofstream fout("/Users/macbookpro/CLionProjects/pedestrian_detection/img_dir/sample_new_neg.txt",ios::trunc);//加路径

    //读取负样本
    //当i小于负样本数量,进行循环,同时读入文件fileNeg中值到imgName中
    for (int i = 0;i < cropNegNum && getline(fileNeg, imgName); i++)
    {
        imgName = "/Users/macbookpro/CLionProjects/pedestrian_detection/normalized_images/train/neg/" + imgName;
        //IMREAD_UNCHANGED :不进行转化,比如保存为了16位的图片,读取出来仍然为16位。
        Mat img = imread(imgName, IMREAD_UNCHANGED);
        //Linux时间函数
        //tv_sec;        /* Seconds. */
        //tv_usec;  /* Microseconds. */
        struct timeval tv;
        if (img.empty())//如果图片不存在,则输出
        {
            cout << "can not load the image:" << imgName << endl;
            continue;
        }
        if (img.cols >= 64 && img.rows >= 128)//如果图片尺寸大于64或者128时
        {
            num = 0;
            //从每张图片中随机剪裁5(10)张64*128的负样本
            for (int j = 0;j < 5;j++)
            {

                //gettimeofday()会把目前的时间用tv 结构体返回
                gettimeofday(&tv,NULL);
                srand(tv.tv_usec);//利用系统时间(微妙),设置随机数种子

                int x = rand() % (img.cols - 64); //左上角x, 范围为[0,cols - 64)
                int y = rand() % (img.rows - 128); //左上角y, 范围为[0,rows - 64)
                cout << "x:" << x << "y:" << y <<endl;
                Mat src = img(Rect(x, y, 64, 128));//Rect(x,y,64,128)从左上角坐标为(x,y)位置剪裁一个宽64,高128的矩形
                //把剪裁后的图片名称存入svaeName变量中
                sprintf(saveName, "/Users/macbookpro/CLionProjects/pedestrian_detection/normalized_images/train/new_neg/neg%dCropped%d.png",i, num);
                //把剪裁后的图片src,另存为名字为svaeName的图片
                imwrite(saveName,src);

                //保存裁剪得到的图片名称到txt文件,换行分隔
                if(i<(cropNegNum-1)){
                    fout <<"neg" << i << "Cropped"<< num++ << ".png"<< endl;
                }
                else if(i==(cropNegNum-1) && j<4){
                    fout <<"neg" << i << "Cropped"<< num++ << ".png"<< endl;
                }
                else{
                    fout <<"neg" << i << "Cropped"<< num++ << ".png";
                }
            }
        }
    }
    fout.close();//关闭文件
    cout << "crop ok!" << endl;
}

int main()
{
    if(crop_negsample){
        // crop_negsample_random(); //裁剪负样本
    }
    //检测窗口(64,128),块尺寸(16,16),块步长(8,8),cell尺寸(8,8),直方图bin个数9
    HOGDescriptor hog(Size(64,128),Size(16,16),Size(8,8),Size(8,8),9);
    int DescriptorDim;//HOG描述子的维数,由图片大小、检测窗口大小、块大小、细胞单元中直方图bin个数决定
    Ptr<SVM> svm = SVM::create();// 创建分类器

    if(TRAIN)//若TRAIN为true,重新训练分类器
    {
        string ImgName;//图片名(绝对路径)
        //正样本图片的文件名列表
        ifstream finPos("/Users/macbookpro/CLionProjects/pedestrian_detection/img_dir/sample_pos.txt");
        // ifstream finNeg("../sample_neg.txt");
        //负样本图片的文件名列表
        ifstream finNeg("/Users/macbookpro/CLionProjects/pedestrian_detection/img_dir/sample_new_neg.txt");
        //HardExample负样本的文件名列表
        ifstream finHardNeg("/Users/macbookpro/CLionProjects/pedestrian_detection/img_dir/hard_neg.txt");

        //if (!finPos || !finNeg || !finHardNeg)
        if (!finPos || !finNeg)
        {
            cout << "Pos/Neg/hardNeg imglist reading failed..." << endl;
            return 1;
        }

        Mat sampleFeatureMat;
        Mat sampleLabelMat;

        //loading original positive examples...
        for(int num=0; num < PosSamNO && getline(finPos,ImgName); num++)
        {
            cout <<"Now processing original positive image: " << ImgName << endl;
            ImgName = "/Users/macbookpro/CLionProjects/pedestrian_detection/normalized_images/train/pos/" + ImgName;
            Mat src = imread(ImgName);//读取图片

            if(CENTRAL_CROP)//true:训练时,对96*160的INRIA正样本图片剪裁出中间的64*128大小人体
                if(src.cols >= 96 && src.rows >= 160)
                //resize(src,src,Size(64,128));
                    src = src(Rect(16,16,64,128));
               // else cout << "error" << endl; //测试

            vector<float> descriptors;//HOG描述子向量
            hog.compute(src, descriptors, Size(8,8));//计算HOG描述子,检测窗口移动步长(8,8)
            //cout<<"描述子维数:"<<descriptors.size()<<endl;


            //处理第一个样本时初始化特征向量矩阵和类别矩阵,因为只有知道了特征向量的维数才能初始化特征向量矩阵
            if(num == 0 )
            {
                DescriptorDim = descriptors.size();//HOG描述子的维数
                //初始化所有训练样本的特征向量组成的矩阵,行数等于所有样本的个数,列数等于HOG描述子维数sampleFeatureMat
                sampleFeatureMat = Mat::zeros(PosSamNO +AugPosSamNO +NegSamNO +HardExampleNO, DescriptorDim, CV_32FC1);//CV_32FC1:CvMat数据结构参数
                //初始化训练样本的类别向量,行数等于所有样本的个数,列数等于1;1表示有人,0表示无人
                sampleLabelMat = Mat::zeros(PosSamNO +AugPosSamNO +NegSamNO +HardExampleNO, 1, CV_32SC1);//sampleLabelMat的数据类型必须为有符号整数型
            }

            //将计算好的HOG描述子复制到样本特征矩阵sampleFeatureMat
            for(int i=0; i<DescriptorDim; i++)
                sampleFeatureMat.at<float>(num,i) = descriptors[i];//第num个样本的特征向量中的第i个元素
            sampleLabelMat.at<int>(num,0) = 1;//正样本类别为1,有人
        }
        finPos.close();


        //依次读取负样本图片,生成HOG描述子
        for(int num = 0; num < NegSamNO && getline(finNeg,ImgName); num++)
        {
            cout<<"Now processing original negative image: "<<ImgName<<endl;
            // ImgName = "../normalized_images/train/neg/" + ImgName;
            //加上负样本的路径名
            ImgName = "/Users/macbookpro/CLionProjects/pedestrian_detection/normalized_images/train/new_neg/" + ImgName;
            Mat src = imread(ImgName);//读取图片

            vector<float> descriptors;//HOG描述子向量
            hog.compute(src,descriptors,Size(8,8));//计算HOG描述子,检测窗口移动步长(8,8)

            //将计算好的HOG描述子复制到样本特征矩阵sampleFeatureMat
            for(int i=0; i<DescriptorDim; i++)
                sampleFeatureMat.at<float>(num+PosSamNO+AugPosSamNO,i) = descriptors[i];//第PosSamNO+num个样本的特征向量中的第i个元素
            sampleLabelMat.at<int>(num +PosSamNO +AugPosSamNO, 0) = -1;//负样本类别为-1,无人

        }
        finNeg.close();

        //依次读取HardExample负样本图片,生成HOG描述子
        for(int num = 0; num < HardExampleNO && getline(finHardNeg,ImgName); num++)
        {
            cout<<"Now processing original hard negative image: "<<ImgName<<endl;
            // ImgName = "../normalized_images/train/neg/" + ImgName;
            //加上负样本的路径名
            ImgName = "/Users/macbookpro/CLionProjects/pedestrian_detection/normalized_images/train/hard_neg/" + ImgName;
            Mat src = imread(ImgName);//读取图片

            vector<float> descriptors;//HOG描述子向量
            hog.compute(src,descriptors,Size(8,8));//计算HOG描述子,检测窗口移动步长(8,8)
            //cout<<"描述子维数:"<<descriptors.size()<<endl;

            //将计算好的HOG描述子复制到样本特征矩阵sampleFeatureMat
            for(int i=0; i<DescriptorDim; i++)
                sampleFeatureMat.at<float>(num+ PosSamNO + NegSamNO + AugPosSamNO,i) = descriptors[i];//第PosSamNO+num个样本的特征向量中的第i个元素
            sampleLabelMat.at<int>(num + PosSamNO + NegSamNO + AugPosSamNO, 0) = -1;//负样本类别为-1,无人

        }
        finHardNeg.close();



        svm ->setType(SVM::C_SVC);
        svm ->setC(0.01);
        svm ->setKernel(SVM::LINEAR);
        // svm ->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 3000, 1e-6));
        svm ->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-3));

        cout<<"Starting training..."<<endl;
        svm ->train(sampleFeatureMat, ROW_SAMPLE, sampleLabelMat);
        cout<<"Finishing training..."<<endl;

        svm ->save("/Users/macbookpro/CLionProjects/pedestrian_detection/data/SVM_HOG.xml");

    }
    else {
        svm = SVM::load( "/Users/macbookpro/CLionProjects/pedestrian_detection/data/SVM_HOG_2.xml" );
    }
    cout << "loaded SVM_HOG.xml file"  << endl;

    int svdim = svm ->getVarCount();//特征向量的维数,即HOG描述子的维数
    //支持向量的个数
    Mat svecsmat = svm ->getSupportVectors();//svecsmat元素的数据类型为float
    int numofsv = svecsmat.rows;

    // Mat alphamat = Mat::zeros(numofsv, svdim, CV_32F);//alphamat和svindex必须初始化,否则getDecisionFunction()函数会报错
    Mat alphamat = Mat::zeros(numofsv, svdim, CV_32F);
    Mat svindex = Mat::zeros(1, numofsv,CV_64F);
    cout << "after initialize the value of alphamat is  " << alphamat.size()  << endl;

    Mat Result;
    double rho = svm ->getDecisionFunction(0, alphamat, svindex);

    cout << "the value of rho is  " << rho << endl;
    alphamat.convertTo(alphamat, CV_32F);//将alphamat元素的数据类型重新转成CV_32F
    cout << "the value of alphamat is  " << alphamat << endl;
    cout << "the size of alphamat is  " << alphamat.size() << endl;
    cout << "the size of svecsmat is  " << svecsmat.size() << endl;

    //计算-(alphaMat * supportVectorMat),结果放到resultMat中
    Result = -1 * alphamat * svecsmat;//float

    cout << "the value of svdim is  " << svdim << endl;

    //得到最终的setSVMDetector(const vector<float>& detector)参数中可用的检测子
    vector<float> vec;
    //将resultMat中的数据复制到数组vec中
    for (int i = 0; i < svdim; ++i)
    {
        vec.push_back(Result.at<float>(0, i));
    }
    vec.push_back(rho);

    cout << "going to write the HOGDetectorForOpenCV.txt file"  << endl;
    //saving HOGDetectorForOpenCV.txt
    ofstream fout("/Users/macbookpro/CLionProjects/pedestrian_detection/data/HOGDetectorForOpenCV.txt");
    for (int i = 0; i < vec.size(); ++i)
    {
        fout << vec[i] << endl;
    }
    fout.close();//关闭文件


    /*********************************Testing**************************************************/
    HOGDescriptor hog_test;
    hog_test.setSVMDetector(vec);

    // Mat src = imread("../person_and_bike_177b.png");
    Mat src = imread("/Users/macbookpro/CLionProjects/pedestrian_detection/data/Test.jpg");
    vector<Rect> found, found_filtered;
    hog_test.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);

    cout<<"found.size : "<<found.size()<<endl;

    //找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中
    for(int i=0; i < found.size(); i++)
    {
        Rect r = found[i];
        int j=0;
        for(; j < found.size(); j++)
            if(j != i && (r & found[j]) == r)
                break;
        if( j == found.size())
            found_filtered.push_back(r);
    }


    //画矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要做一些调整
    for(int i=0; i<found_filtered.size(); i++)
    {
        Rect r = found_filtered[i];
        r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8);
        r.y += cvRound(r.height*0.07);
        r.height = cvRound(r.height*0.8);
        rectangle(src, r.tl(), r.br(), Scalar(0,255,0), 3);
    }

    imwrite("ImgProcessed.jpg",src);
    namedWindow("src",0);
    imshow("src",src);
    waitKey(0);

    /******************读入单个64*128的测试图并对其HOG描述子进行分类*********************/
    读取测试图片(64*128大小),并计算其HOG描述子
    //Mat testImg = imread("person014142.jpg");
    //Mat testImg = imread("noperson000026.jpg");
    //vector<float> descriptor;
    //hog.compute(testImg,descriptor,Size(8,8));//计算HOG描述子,检测窗口移动步长(8,8)
    //Mat testFeatureMat = Mat::zeros(1,3780,CV_32FC1);//测试样本的特征向量矩阵
    //将计算好的HOG描述子复制到testFeatureMat矩阵中
    //for(int i=0; i<descriptor.size(); i++)
    //	testFeatureMat.at<float>(0,i) = descriptor[i];

    //用训练好的SVM分类器对测试图片的特征向量进行分类
    //int result = svm.predict(testFeatureMat);//返回类标
    //cout<<"分类结果:"<<result<<endl;

    return 0;
}


 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页