2014-03-13 43 views
8

我試圖使用OpenCV的HOG描述符,但從中計算出的特徵向量似乎太長了。下面是一個說明該問題的片段:爲什麼opencv的HOG描述符返回如此多的值

#include <stdio.h> 
#include <opencv2/opencv.hpp> 
#include <stdlib.h> 
#include <vector> 

int main() 
{ 
    cv::Mat image = cv::imread("1.jpg"); 
    std::vector<float> features; 
    cv::HOGDescriptor hogdis; 
    hogdis.compute(image, features); 
    printf("HOG feature's length is %zu %zu\n", hogdis.getDescriptorSize(), features.size()); 
    return 0; 
} 

輸出是

HOG feature's length is 3780 1606500 

後者的價值似乎很荒謬。圖像1.jpg的尺寸爲256x256x3,其像素少於特徵向量。爲什麼OpenCV用這麼多值填充特徵向量?如何獲得3780長矢量以供給我的SVM教練?

回答

13

爲什麼OpenCV用這麼多值填充特徵向量?

的HOG特徵的大小由下式確定(不僅僅在圖像尺寸確定):

size_hog_features = (size_t)nbins * (blockSize.width/cellSize.width) 
         * (blockSize.height/cellSize.height) * ((winSize.width 
         - blockSize.width)/blockStride.width + 1) 
         * ((winSize.height - blockSize.height) 
         /blockStride.height + 1); 

所以這是很正常的,你有這麼長的HOG特徵向量。

如何獲取3780長矢量以供給我的SVM教練?

HOG特徵的計算過程中,爲了得到你想要的大小HOG功能之前

您可以設置參數(即nbins, blockSize, cellSize, winSize)。

但爲什麼hogdis.getDescriptorSize()和features.size()不一致?

它們是不同的。 getDescriptorSize()返回分類所需的係數數量。它可以計算如下(參見here):

HOG descriptor length = #Blocks * #CellsPerBlock * #BinsPerCell 

在另一方面,features.size()返回所有的整個圖像的HOG特徵尺寸。

要訓練,您需要通過features

+0

但爲什麼'hogdis.getDescriptorSize()'和'features.size()'不一致? –

+0

@ C.R。查看更新後的答案。 – herohuyongtao

+0

您提供的公式完全獨立於圖像大小。但是,當我嘗試尺寸爲690x622的圖像時,特徵矢量具有18514440個元素,與上面的1606500不同。另外,特徵向量比像素總數要多,更不用說超過40倍。 –