【问题标题】:Computing Histogram of Oriented Gradients计算定向梯度直方图
【发布时间】:2015-01-21 17:05:39
【问题描述】:

为了理解 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.
                }
            }
        }
    }
}

这里是用于计算 HOG 特征的 9 bin 直方图的函数 gradientHist():

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

也许我忽略了什么?任何建议/想法将不胜感激。提前谢谢...

【问题讨论】:

    标签: c++ opencv histogram-of-oriented-gradients


    【解决方案1】:

    cv::calcHist(...) 调用中的第三个参数channels 不应为 0(这使其成为空指针)。 OpenCV 在这里需要一个指向描述感兴趣通道的索引数组的指针。

    由于您想使用第一个通道(索引 0),您的代码应如下所示:

    int channels[] = { 0 };
    calcHist(&cell_fourPx, 1, channels, ...);
    

    【讨论】:

    • 嗨sansuiso,但是它仍然给出同样的错误。我不确定是什么参数造成的。
    • 您能否在代码中添加测试以检查您的输入图像是否为非空并且输入了 CV_8U 或 CV_32F ?这将有助于排除可能的错误原因。
    猜你喜欢
    • 2018-03-14
    • 2011-08-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2017-12-10
    • 2013-11-08
    相关资源
    最近更新 更多