Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting HoG Features using OpenCV

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

like image 683
sub_o Avatar asked Jul 24 '12 07:07

sub_o


People also ask

How do you extract HOG features?

The HOG features are extracted from local regions with 16 × 16 pixels. Histograms of edge gradients with 8 orientations are calculated from each of 4 × 4 local cells. The edge gradients and orientations are obtained by applying Sobel filters. Thus the total number of HOG features becomes 128 = 8 × (4 × 4).

What is OpenCV HOG?

To put a formal definition to this: The HOG feature descriptor counts the occurrences of gradient orientation in localized portions of an image. Implementing HOG using tools like OpenCV is extremely simple. It's just a few lines of code since we have a predefined function called hog in the skimage.

What is HOG feature in image processing?

Histogram of Oriented Gradients, also known as HOG, is a feature descriptor like the Canny Edge Detector, SIFT (Scale Invariant and Feature Transform) . It is used in computer vision and image processing for the purpose of object detection.


2 Answers

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

like image 157
G453 Avatar answered Sep 22 '22 18:09

G453


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

like image 33
Jeonghyun Kim Avatar answered Sep 22 '22 18:09

Jeonghyun Kim