sol*_*les 8 c++ opencv histogram feature-extraction computer-vision
我正在使用OpenCV的Directogram Gradients(HOG)的CPU版本.我使用32x32图像,4x4单元格,4x4块,块之间没有重叠,以及15个方向箱.OpenCV HOGDescriptor给了我一个长度为960的一维特征向量.这是有道理的,因为(32*32像素)*(15个方向)/(4*4个单元格)= 960.
但是,我不确定这些960号码是如何在内存中布局的.我的猜测就是这样:
vector<float> descriptorsValues =
[15 bins for cell 0, 0]
[15 bins for cell 0, 1]
...
[15 bins for cell 0, 7]
....
[15 bins for cell 7, 0]
[15 bins for cell 7, 1]
...
[15 bins for cell 7, 7]
Run Code Online (Sandbox Code Playgroud)
当然,这是一个2D问题扁平化为1D,所以实际上看起来像这样:
[cell 0, 0] [cell 0, 1] ... [cell 7, 0] ... [cell 7, 7]
Run Code Online (Sandbox Code Playgroud)
这是我的示例代码:
using namespace cv;
//32x32 image, 4x4 blocks, 4x4 cells, 4x4 blockStride
vector<float> hogExample(cv::Mat img)
{
img = img.rowRange(0, 32).colRange(0,32); //trim image to 32x32
bool gamma_corr = true;
cv::Size win_size(img.rows, img.cols); //using just one window
int c = 4;
cv::Size block_size(c,c);
cv::Size block_stride(c,c); //no overlapping blocks
cv::Size cell_size(c,c);
int nOri = 15; //number of orientation bins
cv::HOGDescriptor d(win_size, block_size, block_stride, cell_size, nOri, 1, -1,
cv::HOGDescriptor::L2Hys, 0.2, gamma_corr, cv::HOGDescriptor::DEFAULT_NLEVELS);
vector<float> descriptorsValues;
vector<cv::Point> locations;
d.compute(img, descriptorsValues, cv::Size(0,0), cv::Size(0,0), locations);
printf("descriptorsValues.size() = %d \n", descriptorsValues.size()); //prints 960
return descriptorsValues;
}
Run Code Online (Sandbox Code Playgroud)
相关资源: 这篇StackOverflow文章和本教程帮助我开始使用OpenCV HOGDescriptor.
我相信你的想法是正确的。
在其原始论文《人体检测的定向梯度直方图》(第 2 页)中,它说
[...] 检测器窗口平铺有重叠块的网格,其中提取定向梯度特征向量的直方图。[...]
[...] 使用密集(实际上是重叠)的 HOG 描述符网格平铺检测窗口,并使用组合特征向量 [...]
它所谈论的只是将它们平铺在一起。尽管没有介绍如何准确地将它们平铺在一起的详细信息。我想这里不应该发生什么奇特的事情(否则他们会谈论它),即只是定期连接它们(从左到右,从上到下)。
毕竟,这是合理且最简单的数据布局方式。
编辑:如果你看看人们如何访问和可视化数据,你会更加说服自己。
for (int blockx=0; blockx<blocks_in_x_dir; blockx++)
{
for (int blocky=0; blocky<blocks_in_y_dir; blocky++)
{
for (int cellNr=0; cellNr<4; cellNr++)
{
for (int bin=0; bin<gradientBinSize; bin++)
{
float gradientStrength = descriptorValues[ descriptorDataIdx ];
descriptorDataIdx++;
// ... ...
} // for (all bins)
} // for (all cells)
} // for (all block x pos)
} // for (all block y pos)
Run Code Online (Sandbox Code Playgroud)