Ask Your Question
1

HOG feature

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

Shivanshu gravatar image

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

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");
    image.convertTo(image,CV_32F,1/255.0);
    imshow("out",image);
    Mat dx,dy; //image axial derivatives..
    Sobel(image,dx,CV_32F,1,0,1);
    Sobel(image,dy,CV_32F,0,1,1);
    imshow("dx",dx);
    imshow("dy",dy);
    Mat mag,angle;
    cartToPolar(dx,dy,mag,angle);
    imshow("magnitude",mag);
    imshow("angle",angle);

    waitKey(0);
    _getch();


}

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 image...how shall I proceed next

I AM NOT FOND OF USING PRE BUILD HOG DESCRIPTOR..

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
1

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

berak gravatar image

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

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

Comments

Thank you so much @berak

Shivanshu gravatar imageShivanshu ( 2018-04-05 04:23:36 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

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

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

sorry, confused you.

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

what's difference bw Mat_<float> and Mat

Shivanshu gravatar imageShivanshu ( 2018-04-06 02:41:33 -0600 )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 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 592 times

Last updated: Apr 05 '18