Siyuan Ren
Siyuan Ren

Reputation: 7844

Why does opencv's HOG descriptor return so many values

I'm trying to use OpenCV's HOG descriptor, but the feature vector computed from it seems far too long. Here is a snippet that demonstrates the problem:

#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;
}

The output is

HOG feature's length is 3780 1606500

The latter value seems absurd. The image 1.jpg has dimension 256x256x3, which has much less pixels than the feature vector. Why does OpenCV fills the feature vector with so many values? How do I obtain the 3780 long vector to feed to my SVM trainer?

Upvotes: 11

Views: 9476

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

Why does OpenCV fills the feature vector with so many values?

The size of hog features is determined by the following equation (not solely determined on the image dimensions):

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);

So it's quite normal you got such a long HOG feature vector.

How do I obtain the 3780 long vector to feed to my SVM trainer?

You can setup the parameters (i.e. nbins, blockSize, cellSize, winSize) of HOG feature before computing it, in order to get a HOG feature with the size you want.

But why are hogdis.getDescriptorSize() and features.size() inconsistent?

They are different. getDescriptorSize() returns the number of coefficients required for the classification. And it can be computed as follows (refer to here):

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

On the other hand, features.size() returns all the HOG feature size of the whole image.

To train, you need to pass in features.

Upvotes: 18

Related Questions