Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

HOG feature

I have written following program..

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace ml;
using namespace std;


//HOG vector catenation per patch done!!
//
 vector <Mat> HOGvector;
Mat histogram(Mat angle,Mat mag)
{
    if(!angle.empty() && !mag.empty())
    {
    Mat hist=Mat::zeros(1,9,CV_32FC1);
    for(int r=0;r<angle.rows;r++)
    {
        for(int c=0;c<angle.cols;c++)
        {
            float a=(angle.at<float>(r,c));
            int bin=int( (int)a % 180)/20;
            hist.at<float>(0,bin)+=mag.at<float>(r,c);

        }
    }
    normalize(hist,hist);
    return hist;
    }
    else
    {
        return Mat::zeros(1,9,CV_32FC1);
    }

}

void main()
{
        puts("lol");
    Mat h1,h2,h3;

    CascadeClassifier face("haarcascade_frontalface_alt.xml");
    if(face.empty())
    {
        puts("!!CASCADE LOADING FAILURE...");
        exit(0);
    }
    else{}

    Mat image_=imread("lena.jpg");
Mat tmp(image_); 

imshow("tmp",tmp);

Mat image_grey;
cvtColor(tmp,image_grey,COLOR_BGR2GRAY);
equalizeHist(image_grey,image_grey);
 vector<Rect>faces;
 face.detectMultiScale(image_grey,faces,1.1,4,0|CASCADE_SCALE_IMAGE,Size(100,100));
 int s=faces.size();
  vector<Mat>Ptr;

 printf("\nsize...:%d",s);
 Mat ROI;
 if(s>0)
 {
     for(int size=0;size<s;size++)
     {
     rectangle(tmp,Point(faces[size].x,faces[size].y),Point(faces[size].x+faces[size].width,faces[size].y+faces[size].height),Scalar(100,faces[size].height,faces[size].width),2);
    ROI=Mat(tmp,faces[size]);
     }
 }
 else
 {

 }

 imshow("ROI",ROI);
 printf("ROI DIMENSIONS: %d %d",ROI.rows,ROI.cols);
 Mat image(ROI);
 imshow("out",image);


 image.convertTo(image,CV_32F,1/255.0);
    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,true); //polar coordinate system with 'degree' as measurement of angle(not in rad.");
    imshow("magnitude",mag);
    imshow("angle",angle);


    //now time to calculate 8x8 patch of transformed image 
Mat patch;
int count=0;
int a=0;

int patchsize=8;
int iter=(int)image.rows/patchsize; //here iter defines no of iteration of patch over row or coloum..to estimate total iteration we need to square oter..

printf("iteration...%d",iter*iter);
printf("init Hog vector of size:%d",iter*iter);
HOGvector=vector<Mat>(iter*iter);
int r=0,c=0;
if(mag.rows<patchsize && mag.cols<patchsize)
{
    puts("PATCH SIZE..LOL!@@#%");

    _getch();   exit(0);

}


    for(int i=1,r=0;i<=iter;i++,r+=8)
{
    for(int j=1,c=0;j<=iter;j++,c+=8)
    {

        Mat Patch_angle=Mat(angle,Rect(r,c,8,8));
        Mat Patch_mag=Mat(mag,Rect(r,c,8,8));
        HOGvector[count]=histogram(Patch_angle,Patch_mag);
        count+=1;


    }


    }
    cvtColor(image,image,CV_BGR2GRAY);

printf("\ntotalfeatures:%d",count*9);
printf("\nHOG vector size:%d\n",HOGvector.size());
printf("Hog discriptor size:%d",HOGvector.size());
imshow("magniture(POLAR)",mag);
printf("\npixle lost during floating point adjustment..\n%d row %d cols",(image.rows-iter*patchsize),(image.cols-iter*patchsize));
imshow("single channel(Reduced version)",image);
mag.deallocate(); //flush all data..
angle.deallocate();

Mat HOGfeature(count,9,CV_32FC1);

printf("hog rows:%d hog cols:%d",HOGfeature.rows,HOGfeature.cols);
for(int r=0;r<HOGfeature.rows;r++)
{
    for(int c=0;c<HOGfeature.cols;c++)
    {
        HOGfeature.at<float>(r,c)=HOGvector[r].at<float>(0,c);
        HOGvector[r].deallocate();
    }
}
//catenated HOG feature obtained..
//time to apply PCA algorithm over feature..
printf("\ninit PCA object..");
PCA pca(HOGfeature,Mat(),PCA::DATA_AS_ROW,1);

std::cout<<"eigen values:\n"<<pca.eigenvalues;
std::cout<<"\neigen vectors:\n"<<pca.eigenvectors;
std::cout<<"\nmean:\n"<<pca.mean;
printf("size:%d",pca.project(HOGfeature).size);
waitKey(0);
HOGfeature.deallocate();

    _getch();


}

if you saw output of the program..(as I requested for single dimension out of all)I got one Eigen value and its corresponding Eigen vector..finally I actually got 11,025 dimension of catenated HOG vector of whole image(actually array of histogram )...then I fed this huge vector into a matrix (variable:HOGfeature) ..each row represent 9-bin histogram and whole matrix represent HOG vector of Whole image..Then I fed those vector to PCA element and got a 9-dimension Eigen vector....I just want to know I fed 11,025 vectors each of 9 dimension...and I obtained single Eigen value and single Eigen vector corresponding to that then what does that mean???

HOG feature

I have written following program..

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
wrote a program,in which I am targeting for classification of object via neural net.I calculated HOG vector by my own(I didn't used prebuild HOG descriptor).I reduced those feature using namespace cv;
using namespace ml;
using namespace std;


//HOG vector catenation per patch done!!
//
 vector <Mat> HOGvector;
Mat histogram(Mat angle,Mat mag)
{
    if(!angle.empty() && !mag.empty())
    {
    Mat hist=Mat::zeros(1,9,CV_32FC1);
    for(int r=0;r<angle.rows;r++)
    {
        for(int c=0;c<angle.cols;c++)
        {
            float a=(angle.at<float>(r,c));
            int bin=int( (int)a % 180)/20;
            hist.at<float>(0,bin)+=mag.at<float>(r,c);

        }
    }
    normalize(hist,hist);
    return hist;
    }
    else
    {
        return Mat::zeros(1,9,CV_32FC1);
    }

}

void main()
{
        puts("lol");
    Mat h1,h2,h3;

    CascadeClassifier face("haarcascade_frontalface_alt.xml");
    if(face.empty())
    {
        puts("!!CASCADE LOADING FAILURE...");
        exit(0);
    }
    else{}

    Mat image_=imread("lena.jpg");
Mat tmp(image_); 

imshow("tmp",tmp);

Mat image_grey;
cvtColor(tmp,image_grey,COLOR_BGR2GRAY);
equalizeHist(image_grey,image_grey);
 vector<Rect>faces;
 face.detectMultiScale(image_grey,faces,1.1,4,0|CASCADE_SCALE_IMAGE,Size(100,100));
 int s=faces.size();
  vector<Mat>Ptr;

 printf("\nsize...:%d",s);
 Mat ROI;
 if(s>0)
 {
     for(int size=0;size<s;size++)
     {
     rectangle(tmp,Point(faces[size].x,faces[size].y),Point(faces[size].x+faces[size].width,faces[size].y+faces[size].height),Scalar(100,faces[size].height,faces[size].width),2);
    ROI=Mat(tmp,faces[size]);
     }
 }
 else
 {

 }

 imshow("ROI",ROI);
 printf("ROI DIMENSIONS: %d %d",ROI.rows,ROI.cols);
 Mat image(ROI);
 imshow("out",image);


 image.convertTo(image,CV_32F,1/255.0);
    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,true); //polar coordinate system with 'degree' as measurement of angle(not in rad.");
    imshow("magnitude",mag);
    imshow("angle",angle);


    //now time to calculate 8x8 patch of transformed image 
Mat patch;
int count=0;
int a=0;

int patchsize=8;
int iter=(int)image.rows/patchsize; //here iter defines no of iteration of patch over row or coloum..to estimate total iteration we need to square oter..

printf("iteration...%d",iter*iter);
printf("init Hog vector of size:%d",iter*iter);
HOGvector=vector<Mat>(iter*iter);
int r=0,c=0;
if(mag.rows<patchsize && mag.cols<patchsize)
{
    puts("PATCH SIZE..LOL!@@#%");

    _getch();   exit(0);

}


    for(int i=1,r=0;i<=iter;i++,r+=8)
{
    for(int j=1,c=0;j<=iter;j++,c+=8)
    {

        Mat Patch_angle=Mat(angle,Rect(r,c,8,8));
        Mat Patch_mag=Mat(mag,Rect(r,c,8,8));
        HOGvector[count]=histogram(Patch_angle,Patch_mag);
        count+=1;


    }


    }
    cvtColor(image,image,CV_BGR2GRAY);

printf("\ntotalfeatures:%d",count*9);
printf("\nHOG vector size:%d\n",HOGvector.size());
printf("Hog discriptor size:%d",HOGvector.size());
imshow("magniture(POLAR)",mag);
printf("\npixle lost during floating point adjustment..\n%d row %d cols",(image.rows-iter*patchsize),(image.cols-iter*patchsize));
imshow("single channel(Reduced version)",image);
mag.deallocate(); //flush all data..
angle.deallocate();

Mat HOGfeature(count,9,CV_32FC1);

printf("hog rows:%d hog cols:%d",HOGfeature.rows,HOGfeature.cols);
for(int r=0;r<HOGfeature.rows;r++)
{
    for(int c=0;c<HOGfeature.cols;c++)
    {
        HOGfeature.at<float>(r,c)=HOGvector[r].at<float>(0,c);
        HOGvector[r].deallocate();
    }
}
//catenated HOG feature obtained..
//time to apply PCA algorithm over feature..
printf("\ninit PCA object..");
PCA pca(HOGfeature,Mat(),PCA::DATA_AS_ROW,1);

std::cout<<"eigen values:\n"<<pca.eigenvalues;
std::cout<<"\neigen vectors:\n"<<pca.eigenvectors;
std::cout<<"\nmean:\n"<<pca.mean;
printf("size:%d",pca.project(HOGfeature).size);
waitKey(0);
HOGfeature.deallocate();

    _getch();


}

if you saw output of the program..(as PCA...where I requested for single dimension out of all)I got one obtained three Eigen value values and its corresponding Eigen vector..finally vector.Now how shall I actually got 11,025 dimension of catenated HOG vector of whole image(actually array of histogram )...then I fed this huge vector into a matrix (variable:HOGfeature) ..each row represent 9-bin histogram and whole matrix represent HOG vector of Whole image..Then I fed feed those vector Eigen values to PCA element and got a 9-dimension Eigen vector....I just want to know I fed 11,025 vectors neural net??I mean each of 9 dimension...and I obtained single Eigen value and single Eigen vector corresponding is of nine dimension and there are total three...do we need to that then what does that mean???catenate all together to make a (9x3=27 dimensional vector in a single row) or do something else like resultanting all together??

click to hide/show revision 3
None

updated 2018-04-22 01:57:14 -0600

berak gravatar image

HOG feature

I wrote a program,in which I am targeting for classification of object via neural net.I calculated HOG vector by my own(I didn't used prebuild HOG descriptor).I reduced those feature using PCA...where I obtained three Eigen values and its corresponding Eigen vector.Now how shall I feed those Eigen values to neural net??I mean each Eigen vector is of nine dimension and there are total three...do we need to catenate all together to make a (9x3=27 dimensional vector in a single row) or do something else like resultanting all together??