基于vlfeat的HOG特征提取c++代码实现

HOG特征又叫方向特征直方图特征,是计算机视觉中作为目标检测十分常用且奏效的特征。其最著名的应用就是HOG+SVM这种思路解决了行人检测的任务,这项工作发表在了CVPR2005上,从此之后,HOG+SVM这种模式被复制在了很多其他工作中。

有趣的是,在网络上我们可以轻而易举的搜索到无数篇关于HOG特征的理论介绍,却很少可以找到C++版本的代码。这无疑对计算机视觉研究刚刚入门的同学造成了很大困扰,纸上谈兵不如将代码跑出来直接查看实验效果。这里我与大家分享一下基于VLFeat的HOG特征提取代码,希望对大家的学习有所帮助。当然,我的代码只是实现了对一张图片的处理流程,及简单的参数设置,同学们还要根据自己的实际情况在这段代码的基础上稍作修改。话不多说,直接上代码。

#include 
#include 
#include 

using namespace std;

int main()
{
    cv::Mat image,img;
    image = cv::imread("image path");
    cv::cvtColor(image,img,CV_RGB2GRAY);
    //convert img to a float array
    float *vlimg = new float[img.cols*img.rows];
    int tmp = 0;
    for(int i=0;ifor(int j=0;j(j,i)/255.0;
        }

    //set vl parameters
    vl_size numOrientations = 9;  //specifies the number of orientations
    vl_size numChannels = 1;      //number of image channel
    vl_size height = img.rows;
    vl_size width = img.cols;
    vl_size cellSize = 8;         //size of a hog cell
    vl_size hogWidth, hogHeight, hogD;
    float *hogArray;             //hog features array
    //extract hog 
    VlHog *hog = vl_hog_new(VlHogVariantDalalTriggs, numOrientations, VL_FALSE) ;
    vl_hog_put_image(hog, vlimg, height, width, numChannels, cellSize) ;
    hogWidth = vl_hog_get_width(hog) ;
    hogHeight = vl_hog_get_height(hog) ;
    hogD = vl_hog_get_dimension(hog) ;
    hogArray = (float*)vl_malloc(hogWidth*hogHeight*hogD*sizeof(float)) ;
    vl_hog_extract(hog, hogArray) ;
    vl_hog_delete(hog) ;
    //parameters of image and its hog
    cout<<"height = "<"         width = "<cout<<"hogWidth = "<"hogHeight = "<"hogD = "<"img",img);
    delete[] vlimg;
    cv::waitKey(0);
}

这段代码用到了opencv及vlfeat两个库。这两个库的配置大家可以自行百度,网上的方法很多。这里就不再赘述。
代码最后的输出部分只是依据理论分析的一个结果验证,结合程序输出可以帮助同学们更好的理解hog原理。

你可能感兴趣的:(计算机视觉)