Hog feature Extraction code (OPENCV processing basic data, C language implementation of basic algorithm) __ block chain

Source: Internet
Author: User
#include <cv.h>
#include #include <math.h>
#include <opencv.hpp>
using namespace CV;
using namespace Std;
Iplimage * Grbtogrey (iplimage* img)//RGB image to grayscale image
{
iplimage* dest=cvcreateimage (Cvsize (Img->width, img->height), img->depth,1);


uchar* data = (uchar*) img->imagedata;
int b,g,r;
for (int i = 0;iheight;i++) {
for (int j = 0;jwidth;j++) {
b = data[i*img->widthstep+j*img->nchannels+0];
g = data[i*img->widthstep+j*img->nchannels+1];
r = data[i*img->widthstep+j*img->nchannels+2];
((uchar*) (Dest->imagedata+i*dest->widthstep)) [J]=0.3*r+0.59*g+0.11*b;
}
}
return dest;
}


iplimage* nomolize (iplimage *img)//Must be a grayscale image
{
uchar* GM = (uchar*) img->imagedata;
int max = 0,min = 255;
for (int i = 0;iheight;i++) {
for (int j = 0;jwidth;j++) {
int num=gm[i*img->widthstep+j*img->nchannels];
if (Num>max) {
max = num;
}
if (num<min) {
min = num;
}
}
}
for (int i = 0;iheight;i++) {
for (int j = 0;jwidth;j++) {
int num = (int) (float) (gm[i*img->widthstep+j*img->nchannels]-min) *
((float) (255)/(float) (max-min);
if (num>255) {
num = 255;
}
((uchar*) (Img->imagedata+i*img->widthstep)) [j] = num;
}
}
return img;
}


Iplimage *greygamma (Iplimage *greyimage) {
Iplimage *gammaimage = Cvcreateimage (Cvsize (greyimage->width,greyimage->height),
Greyimage->depth,greyimage->nchannels);


uchar* data = (uchar*) greyimage->imagedata;
for (int i = 0;i<greyimage->height;i++) {
for (int j = 0;j<greyimage->width;j++) {
int gamma=data[i*greyimage->widthstep+j*greyimage->nchannels];
((uchar*) (Gammaimage->imagedata+i*gammaimage->widthstep)) [j] = (int) sqrt ((float) gamma);
}
}
Return Nomolize (Gammaimage);
}


Iplimage *gradientimg (Iplimage *img) {
UCHAR * data = (uchar*) img->imagedata;
for (int i = 1;iheight-1;i++) {
for (int j = 1;jwidth-1;j++) {
int Gx = data[i*img->widthstep+ (j+1) *img->nchannels]-data[i*img->widthstep+ (j-1) *img->nchannels];
int Gy = data[(i+1) *img->widthstep+j*img->nchannels]-data[(i-1) *img->widthstep+j*img->nchannels];
int gradient = (int) sqrt ((float) (gx*gx+gy*gy));
int theta = (int) (Atan (float) gy/(float) Gx) *180/3.14);
((uchar*) (Img->imagedata+i*img->widthstep)) [j] = theta;
}
}
Return Nomolize (IMG);
}


int *cellfeature (iplimage *img, int w,int h) {//8*8 cell
int cell_w = 8;
int f_n = 9;
int *feature = new Int[f_n];
for (int i = 0;i<f_n;i++) {
f_n = 0;
}
for (int i = h;ifor (int j = w;j<w+cell_w;j++) {
feature[((uchar*) (Img->imagedata+i*img->widthstep)) [j]/40]++;
}
}
return feature;
}


int *hogfeature (Iplimage *img) {


int cell_w = 8;
int CELL_FN = 9;
int cell_wn = img->width/cell_w;
int cell_hn = img->height/cell_w;
int *hog_f = new INT[CELL_WN*CELL_HN*CELL_FN];
int m = 0;


for (int i = 0;i<cell_hn;i++) {
for (int j = 0;j<cell_wn;j++) {
int *feature = Cellfeature (img,j*cell_w,i);
for (int k = 0;k<cell_fn;k++) {
hog_f[m++] = feature[k];
}
}
}


for (int i = 0;i<cell_wn*cell_hn*cell_fn;i++) {
if (i%9==0) {
printf ("\ n");
}
if (i% (2*2*9) ==0) {
printf ("-------------------------------------\ n \ nthe");
}
printf ("%d", hog_f[i]);
}
return hog_f;
}


int main ()
{
Iplimage *img=cvloadimage ("img2.jpg");
if (img==null) {
printf ("IMG is null");
return 0;
}
Cvnamedwindow ("Greyimage");
iplimage* greyimg = Grbtogrey (IMG);
Cvshowimage ("Greyimage", greyimg);
Cvwaitkey ();
Cvnamedwindow ("Gammaimage");
iplimage* gammaimg = Greygamma (greyimg);
Cvshowimage ("Gammaimage", gammaimg);
printf ("Gamma end\n");
Cvwaitkey ();
Cvnamedwindow ("Gradientimage");
iplimage* gradientimage = gradientimg (gammaimg);
Cvshowimage ("Gradientimage", gradientimage);
printf ("Gradient end\n");
Cvwaitkey ();
int *hog_f = Hogfeature (gradientimage);
return 1;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.