2014-11-24 95 views
0

爲了理解Dalal和Triggs提出的面向梯度直方圖(Histogram of Oriented Gradients,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()來計算9斌直方圖的HOG特徵:

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 

也許我忽略了一些東西?任何建議/想法將不勝感激。在此先感謝...

回答

1

調用cv::calcHist(...)的第三個參數channels不應該爲0(這使它成爲空指針)。 OpenCV在這裏期望一個指向描述感興趣的頻道的索引數組的指針。

既然你要使用的第一通道(指數0),你的代碼應該是這樣的:

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

嗨sansuiso,但是它仍然是給同樣的錯誤。我不確定是什麼參數導致它。 – tarmizi 2014-11-26 01:39:05

+0

您可以在代碼中添加測試以檢查您的輸入圖像是否爲非空且已鍵入CV_8U或CV_32F?這將有助於排除可能的錯誤原因。 – sansuiso 2014-11-26 08:58:31