gpt4 book ai didi

c++ - OpenCV HOG特征数据布局?

转载 作者:可可西里 更新时间:2023-11-01 17:59:39 28 4
gpt4 key购买 nike

我正在使用 OpenCV 的 CPU 版本的定向梯度直方图 (HOG)。我使用的是 32x32 图像,其中包含 4x4 单元格、4x4 block 、 block 之间没有重叠以及 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]

当然,这是一个平面化为一维的二维问题,所以它实际上看起来像这样:

[cell 0, 0] [cell 0, 1] ... [cell 7, 0] ... [cell 7, 7]

那么,我对数据布局有正确的想法吗?还是其他原因?


这是我的示例代码:

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;
}

相关资源: This StackOverflow postthis tutorial帮助我开始使用 OpenCV HOGDescriptor。

最佳答案

我相信您的想法是正确的。

在其原始论文中Histograms of Oriented Gradients for Human Detection (第 2 页),它说

[...] The detector window is tiled with a grid of overlapping blocks in which Histogram of Oriented Gradient feature vectors are extracted. [...]

[...] Tiling the detection window with a dense (in fact, overlapping) grid of HOG descriptors andusing the combined feature vector [...]

它谈论的只是tiling他们在一起。尽管没有介绍如何将它们准确地拼贴在一起的详细信息。我想这里不应该发生任何奇特的事情(否则他们会谈论它),即只是定期连接它们(从左到右,从上到下)。

毕竟,这是合理且最简单的数据布局方式。


编辑:如果您查看how people access and visualize the data,您将更加说服自己。 .

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)

关于c++ - OpenCV HOG特征数据布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351913/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com