Ask Your Question

HOG feature

asked 2018-04-05 02:46:42 -0500

Shivanshu gravatar image

updated 2020-09-28 10:49:14 -0500

I learned about HOG feature from here following this tutorial I am trying to create HOG feature...I have approached till here..

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace ml;
using namespace std;

void main()
    Mat h1,h2,h3;
    Mat image=imread("lena.jpg");
    Mat dx,dy; //image axial derivatives..
    Mat mag,angle;



I have obtained image~derivative of orignal image and mapped to polar coordinate system.Now I obtained two matrices represent gradient and its orientation namely mag and angle respectively.Now I have to obtain histogram of gradients over 8x8 patch of shall I proceed next


I am quite sure its not a good idea...But

Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-05 03:32:36 -0500

berak gravatar image

updated 2018-04-05 03:36:33 -0500

that shouldn't be dificult. first, you want the angle in degrees, not radians:

cartToPolar(dx,dy,mag,angle, true);

then, it's a double for loop, to get 8x8 patches:

for (int r=0; r<img.rows; r += 8) {
     for (int c=0; c<img.cols; c+=8) {
           Mat_<float> patch_angle = angle(Rect(c,r,8,8));
           Mat_<float> patch_mag = mag(Rect(c,r,8,8));

then, for each patch, you need to make a histogram, an array of counters:

Mat_<float> hist = Mat_<float>::zeros(1,9);

now, iterate over the values in patch_angle and patch_mag, and increase the resp. bin:

int bin = (int(patch_angle(r,c) % 180) / 20; //results in a value [0..8]
hist(0, bin) += patch_mag(r,c);
edit flag offensive delete link more


Thank you so much @berak

Shivanshu gravatar imageShivanshu ( 2018-04-05 04:23:36 -0500 )edit

So for each patch I made histogram of oriented gradients...I did the same as you suggested.Now how can I use is as feature over which I further apply dimension reduction(PCA) algorithms and feed to Neural Network to train them

Shivanshu gravatar imageShivanshu ( 2018-04-05 07:51:49 -0500 )edit

here's a PCA example . (basically: you construct a PCA from your data, then project the data with the (reduced set of) eigenvectors, a matrix multiplication, like transformations in 3d)

if you use the search bar on this site, you'll find many examples for training ann's

berak gravatar imageberak ( 2018-04-05 07:56:59 -0500 )edit

actually I don't understand basic of PCA,so I need help for good website for good tutorial....:(

Shivanshu gravatar imageShivanshu ( 2018-04-05 08:17:03 -0500 )edit

you were the 3d guy, no ? think of it as a coordinate transformation. 1st find the base coord system, then use it to transform the data to a space with less dimensions

berak gravatar imageberak ( 2018-04-05 08:36:01 -0500 )edit

Which 3d guy @berak? I am the opencv guy.....

Shivanshu gravatar imageShivanshu ( 2018-04-05 23:40:16 -0500 )edit

sorry, confused you.

berak gravatar imageberak ( 2018-04-06 02:38:21 -0500 )edit

what's difference bw Mat_<float> and Mat

Shivanshu gravatar imageShivanshu ( 2018-04-06 02:41:33 -0500 )edit

it's a templated type, and you don't need the .at<float> when accessing elements later

berak gravatar imageberak ( 2018-04-06 02:43:19 -0500 )edit

Why we take only 9 buckets for representing 128(8x8x2) pixle values ...we can take more...

Shivanshu gravatar imageShivanshu ( 2018-04-06 07:38:46 -0500 )edit

Question Tools

1 follower


Asked: 2018-04-05 02:46:42 -0500

Seen: 181 times

Last updated: Apr 05 '18