Extracting HoG Features using OpenCV

54,670

Solution 1

You can use hog class in opencv as follows

HOGDescriptor hog;
vector<float> ders;
vector<Point> locs;

This function computes the hog features for you

hog.compute(grayImg, ders, Size(32, 32), Size(0, 0), locs);

The HOG features computed for grayImg are stored in ders vector to make it into a matrix, which can be used later for training.

Mat Hogfeat(ders.size(), 1, CV_32FC1);

for(int i=0;i<ders.size();i++)
    Hogfeat.at<float>(i,0)=ders.at(i);

Now your HOG features are stored in Hogfeat matrix.

You can also set the window size, cell size and block size by using object hog as follows:

hog.blockSize = 16;
hog.cellSize = 4;
hog.blockStride = 8;

// This is for comparing the HOG features of two images without using any SVM 
// (It is not an efficient way but useful when you want to compare only few or two images)
// Simple distance
// Consider you have two HOG feature vectors for two images Hogfeat1 and Hogfeat2 and those are same size.

double distance = 0;
for(int i = 0; i < Hogfeat.rows; i++)
    distance += abs(Hogfeat.at<float>(i, 0) - Hogfeat.at<float>(i, 0));

if (distance < Threshold)
    cout<<"Two images are of same class"<<endl;
else
    cout<<"Two images are of different class"<<endl;

Hope it is useful :)

Solution 2

I also wrote the program of 2 hog feature comparing with the help of the above article. And I apply this method to check ROI region changing or not. Please refer to the page here. source code and simple introduction

Solution 3

OpenCV 3 provides some changes to the way GPU algorithms (i.e. CUDA) can be used by the user, see the Transition Guide - CUDA.

To update the answer from user3398689 to OpenCV 3, here is a snipped code:

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaimgproc.hpp>

[...]

/* Suppose you load an image in a cv::Mat variable called 'src' */

int img_width  = 320;
int img_height = 240;
int block_size = 16;
int bin_number = 9;

cv::Ptr<cv::cuda::HOG> cuda_hog = cuda::HOG::create(Size(img_width, img_height),
                                                    Size(block_size, block_size),
                                                    Size(block_size/2, block_size/2),
                                                    Size(block_size/2, block_size/2),
                                                    bin_number);

/* The following commands are optional: default values applies */
cuda_hog->setDescriptorFormat(cuda::HOG::DESCR_FORMAT_COL_BY_COL);
cuda_hog->setGammaCorrection(true);
cuda_hog->setWinStride(Size(img_width_, img_height_));

cv::cuda::GpuMat image;
cv::cuda::GpuMat descriptor;

image.upload(src);

/* May not apply to you */
/* CUDA HOG works with intensity (1 channel) or BGRA (4 channels) images */
/* The next function call convert a standard BGR image to BGRA using the GPU */
cv::cuda::GpuMat image_alpha;
cuda::cvtColor(image, image_alpha, COLOR_BGR2BGRA, 4);

cuda_hog->compute(image_alpha, descriptor);

cv::Mat dst;
image_alpha.download(dst);

You can then use the descriptors in 'dst' variable as you prefer like, e.g., as suggested by G453.

Share:
54,670
sub_o
Author by

sub_o

subokita.com

Updated on December 08, 2020

Comments

  • sub_o
    sub_o over 3 years

    I am trying to extract features using OpenCV's HoG API, however I can't seem to find the API that allow me to do that.

    What I am trying to do is to extract features using HoG from all my dataset (a set number of positive and negative images), then train my own SVM.

    I peeked into HoG.cpp under OpenCV, and it didn't help. All the codes are buried within complexities and the need to cater for different hardwares (e.g. Intel's IPP)

    My question is:

    1. Is there any API from OpenCV that I can use to extract all those features / descriptors to be fed into a SVM ? If there's how can I use it to train my own SVM ?
    2. If there isn't, are there any existing libraries out there, which could accomplish the same thing ?

    So far, I am actually porting an existing library (http://hogprocessing.altervista.org/) from Processing (Java) to C++, but it's still very slow, with detection taking around at least 16 seconds

    Has anyone else successfully to extract HoG features, how did you go around it ? And do you have any open source codes which I could use ?

    Thanks in advance

  • sub_o
    sub_o almost 12 years
    currently I can't test it out, because XCode 4.4 somehow crippled most of the installed libraries. I'll inform you later when I managed to solve that issue
  • G453
    G453 almost 12 years
    U can use simple Euclidean distance as a matcher....I edited the answer see the same answer above :)
  • sub_o
    sub_o almost 12 years
    @Sistu Thanks, I can finally extract the features, but I'd still need to feed into my own SVM.
  • Yamaneko
    Yamaneko over 11 years
    I am using OpenCV 2.3.1, but I don't have HOGDescriptor::compute() in this version. Which OpenCV version did you use in this answer?
  • Yamaneko
    Yamaneko over 11 years
    Sorry, just found out that it also works in OpenCV 2.3.1. I was using cv::gpu::HOGDescriptor instead of cv::HOGDescriptor. Thanks!
  • spurra
    spurra over 9 years
    Hi, i'm using opencv 2.4.9 and this approach you described does not work. I get the error that none of the overloaded functions match the argument list. Could you update your answer to accomodate for opencv2.4.9?
  • Thirumalai murugan
    Thirumalai murugan about 9 years
    Only the link can't be accepted as answer, describe the needed part of the link in your answer.