计算定向梯度的直方图
Computing Histogram of Oriented Gradients
为了理解Dalal和Triggs提出的定向梯度直方图(HOG)功能,我选择在不使用openCV的HOGDescriptor的情况下对其进行硬编码。以下是我尝试实现 HOG 的一些相关代码:
void hog::hog_process(Mat &direction) // direction is the gradient direction matrix
{
Size blockSize(8,8);
Size cellSize(4,4);
vector<Mat> block;
Size s = direction.size();
Mat cell_eightPx;
Mat cell_fourPx;
// Essentially split the image into 8 by 8 cells. HOG processing of each block should be essiantially here.
// the 8 by 8 cells are then split again into 4 by 4 cells
for(int col = 0; col < direction.rows; col += blockSize.height)
{
for(int row = 0; row < direction.cols; row += blockSize.width)
{
Rect rect= Rect(row, col,blockSize.width,blockSize.height);
cell_eightPx = Mat(direction,rect); // Get a 8 by 8 cell from the gradient direction matrix.
//**********COMPUTE 9 bin gradient direction histogram of the 8 by 8 cell here !!! ****
for(int i = 0; i < cell_eightPx.rows; i += cellSize.height)
{
for(int j = 0; j < cell_eightPx.cols; j += cellSize.width)
{
Rect rect_fourPx = Rect(i,j,cellSize.width,cellSize.height); // 4 by 4 rectangle size
cell_fourPx = Mat(cell_eightPx,rect_fourPx); // create a 4 by 4 cell from gradient direction matrix (cell_eightPx)
gradientHist(cell_fourPx); // Calculate gradient histogram of the 4 by 4 cell. (Function call)
cell_fourPx.deallocate(); // clear the cell.
}
}
}
}
}
以下是函数 gradientHist() 来计算 HOG 特征的 9 箱直方图:
void hog::gradientHist(Mat &cell_fourPx)
{
Mat hist;
ofstream feature("Hist_Values.csv",std::ofstream::app);
// create a 9 bin histogram with range from 0 t0 180 for HOG descriptors.
int histSize = 9;
float range[] = {0,180};
const float *histRange = {range};
bool uniform = true;
bool accumulate = false;
calcHist(&cell_fourPx, 1, 0,Mat(),hist, 1, &histSize, &histRange, uniform, accumulate); //Calculate the 9 bin histogram.
normalize(hist, hist, 0, 0, NORM_MINMAX, -1, Mat());
for(int i = 0; i < histSize; i++)
{
feature << hist.at<float>(i) << ","; // Output the value of HOG to a csv file
}
}
然而,OpenCV告诉我:
unsupported format or combination of formats () in calcHist, file....line 1219....histogram.cpp:1219:
error(-210) in function calcHist
也许我忽略了什么?任何建议/想法将不胜感激。提前感谢...
调用 cv::calcHist(...)
时channels
的第三个参数不应为 0(这使其成为空指针)。OpenCV 在这里期望一个指向描述感兴趣通道的索引数组的指针。
由于要使用第一个通道(索引 0),因此代码应如下所示:
int channels[] = { 0 };
calcHist(&cell_fourPx, 1, channels, ...);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何在OpenCV 3.4.7中计算CV_32FC1 cv::GpuMat图像的(范围)直方图
- 如何计算图像中每个块的直方图,然后将它们连接起来?
- 使用标准库或Boost库从一组数据中计算直方图
- 拥有云pcl::P ointCloud<xyzrgb>,如何独立计算每个颜色层的直方图?
- 如何使用每 n 个像素 OpenCV 计算直方图
- 尝试在没有opencv calcHist()的情况下计算我自己的直方图
- CUDA使用共享内存计算直方图
- 计算彩色图像的HSV直方图是否与H-S直方图不同
- Opencv:如何计算 3D 直方图
- OpencCV:从修改后的直方图计算图像
- 计算定向梯度的直方图
- 为什么尝试计算直方图和背向投影不起作用
- 在C++中计算单通道直方图的平均值和标准偏差
- 如何在OpenCV中计算矩阵的直方图
- 计算累积直方图