Problem with computing Histogram of Oriented Gradients (HOG)

asked 2014-11-17 01:50:42 -0600

tarmizi_adam2005 gravatar image

Hi,

In order to understand the mechanics of Histogram of Oriented Gradients proposed by Dalal and Triggs, I have tried to hard code the HOG algorithm. However I am having some problems. It says

OpenCV error: Unsupported format or combination of formats () in calcHist....
              error: (-210) in function calcHist

I am not quite sure what format of the function argument that is not right. This is the code:

void hog::hog_process(Mat &direction) // direction is a 128 by 64 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); // Function call, Calculate gradient histogram of the 4 by 4 cell.
                    cell_fourPx.deallocate(); // clear the cell.
                }
            }
        }
    }
}

After partitioning the image with 4 by 4 cells as the above code the 9 bin histogram calculation is as follows:

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         
    }

}

Any suggestions/ directions are very much welcomed. I may have overlooked something. Thanks in advance.

edit retag flag offensive close merge delete