Hog feature extraction, detailed steps and source code

Source: Internet
Author: User
Tags pow

Just finished writing, press 2 under the space. The picture below all lost, re-write it ...

A novice, the image is just contact with the hog, others do not understand, some words may be said is not very accurate, forgive the next ~

Blogging is also the first time ... Take note of the notes ...

Not much to say ...

Original

Post-operation renderings: One is a grayscale image, and the other is a size chart for each cell's bin:




Here is the macro in my Code

#define PI 3.14
#define BIN_SIZE
#define BIN_NVM 9
#define NORM_WIDTH
#define Norm_height
#define CELL_SIZE 8
#define BLOCK_SIZE 2
#define PIC_CELL_WH
#define CELL_W_NVM  ((norm_width-2)/ cell_size)
#define CELL_H_NVM  ((norm_height-2)/cell_size)
#define BLOCK_W_NVM  (CELL_W_NVM- Block_size + 1)
#define BLOCK_H_NVM  (cell_h_nvm-block_size + 1)
#define CELL_NVM (CELL_W_NVM * Cell_h_nv M)
#define BLOCK_NVM (BLOCK_W_NVM * block_h_nvm)
#define Array_all (BLOCK_W_NVM * BLOCK_H_NVM * block_size * BLO Ck_size * BIN_NVM)


First load a picture, grayscale and gamma correction

    iplimage* img = Cvloadimage ("./003.jpg");
 	Iplimage *img1 = cvcreateimage (Cvgetsize (IMG), ipl_depth_8u,1);
 	Iplimage *img2 = cvcreateimage (Cvgetsize (IMG), ipl_depth_8u,1);
 	cvmat* mat = Cvcreatemat (Img->width, IMG->HEIGHT,CV_32FC1);

	Grayscale Figure
 	Cvcvtcolor (img,img1,cv_bgr2gray); 
 	Cvnamedwindow ("Grayimage", cv_window_autosize);
 	Cvshowimage ("Grayimage", IMG1); 

	Gamma correction
 	uchar* uData  = (uchar*) (img1->imagedata);
 	float* FMat = mat->data.fl;
 
 	for (int II = 0; II < img1->imagesize; ii++)
 	{
 		Fmat[ii] = POW (udata[ii],  0.5f); 
 		((uchar*) img2->imagedata) [II] = (UCHAR) (Fmat[ii]);
 	

After zooming the picture to the specified size, the gradient direction of the pixels in each cell is already size, and here is the Func () function, which is the width of the left x;y;cell of the upper left corner of each cell, and the 9-D bin array for each cell that is output.
void func (int i_x, int i_y, int i_w, iplimage* img_in, float* fbin) {memset (fbin, 0, 9*sizeof (float));
	float f_x = 0.0f, f_y = 0.0f, F_NVM = 0.0f, F_theta = 0.0f; for (int II = i_y, ii < i_y + I_w; ii++) {for (int JJ = i_x; JJ < i_x + I_w; jj++) {uchar* PData = (uchar*
			) (Img_in->imagedata + II * img_in->widthstep + JJ);
			f_x = pdata[1]-pdata[-1];
			f_y = pdata[img_in->widthstep]-pdata[-img_in->widthstep]; 

			F_NVM = POW (f_x*f_x + f_y*f_y, 0.5f);
			float fangle = 90.0f;
				if (f_x = = 0.0f) {if (f_y > 0) {fangle = 90.0f;
				}} else if (f_y = = 0.0f) {if (f_x > 0) {fangle = = 0.0f;
				} else if (f_x < 0) {Fangle = = 180.0f; }} else {F_theta = Atan (f_y/f_x);////atan () range-PI/2 to PI/2 all 9 BIN ranges are 0~180°fangle = (bin_size*bin
			_NVM * F_theta)/pi;
			} if (Fangle < 0) {fangle + = 180;
			} int iwhichbin = Fangle/bin_size; Fbin[iwhichbin] + = f_NVM; }
	}
}

To create an IMG, draw the direction size of each cell's 9 bin, note: x squares to the right, y squares downward. Because each cell on the bin of the size of the number of different, and the gap is also large, so you have to x, such as a cell map with 50*50 size of the lattice, then each diagram is (25,25) as the center of the drawing, so all bin values in the 0~25 range
	for (int II = 0; II < CELL_H_NVM, ii++)
	{for
		(int jj = 0; JJ < CELL_W_NVM; jj++)
		{for
			(int kk = 0; KK < BIN_NVM; kk++)
			{
				F_cell_max = (F_bin_out[ii*cell_w_nvm+jj][kk] > F_cell_max)? F_BIN_OUT[II*CELL_W_NVM+JJ][KK]: f_ Cell_max;
			}

			for (int kk = 0; KK < BIN_NVM; kk++)
			{
				if (F_cell_max = = 0.0f)
				{
					F_pic[ii*cell_w_nvm+jj][kk] = 0.0f;< c13/>}
				Else
				{
					F_pic[ii*cell_w_nvm+jj][kk] = f_bin_out[ii*cell_w_nvm+jj][kk] * (PIC_CELL_WH/2)/  F_cell_max; 
				}
			}

			F_cell_max = 0.0f;
		}
	}

When you are finished, show each number to the picture, Note: X square to the right, y square down. At that time the angle of the time with the Atan () function, the range is-PI/2~PI/2, the bin range is 0~180°, each bin is 20°, drawing according to 0~360°, each bin for 40° to draw, [0,40] in the range of size drawn at 0°, [40~80] The size within the range is drawn in the 40° direction, and so on
    Cvscalar SS;
	Ss.val[0] = 0;
	SS.VAL[1] = 0;
	SS.VAL[2] = 255;
	
	for (int II = 0; II < CELL_H_NVM, ii++)
	{for
		(int jj = 0; JJ < CELL_W_NVM; jj++)
		{for
			(int kk = 0; KK < BIN_NVM; kk++)
			{
				Cvpoint Ststart = Cvpoint ((PIC_CELL_WH/2) + JJ*PIC_CELL_WH, (PIC_CELL_WH/2) + II*PIC_CELL_WH);
				Cvpoint Ststop;
				float fangle = kk*40*pi/180;
				float fsize = F_pic[ii*cell_w_nvm+jj][kk];
				ststop.x = fsize * cos (fangle) + ststart.x;
				ststop.y = fsize * sin (fangle) + Ststart.y;
				Cvline (Img4, Ststart, Ststop, ss);}}}
	

When it is finished, the drawing is the third picture shown above, and you can see that there are more places in the gradient direction and size than anywhere else. Finally, to complete the output characteristics of large arrays, it should be used to identify the characteristics of the vector, the first to be normalized to each block, and then to concatenate all blocks into a large array,
	int Iblockwhichcell = 0;
	int UU = 0;
	float F_max = 0.0f;
	float F_ETHER_BLOCK[BLOCK_SIZE*BLOCK_SIZE][BIN_NVM];
	float F_last_array[array_all]; for (int II = 0; II < BLOCK_W_NVM, ii++) {for (int JJ = 0; JJ < BLOCK_H_NVM; jj++) {for (int kk = 0; KK & Lt BIN_NVM;
				kk++) {F_ether_block[0][kk] = F_bin_out[ii*cell_w_nvm+jj][kk];
				F_ETHER_BLOCK[1][KK] = F_bin_out[ii*cell_w_nvm+jj+1][kk];
				F_ETHER_BLOCK[2][KK] = f_bin_out[ii*cell_w_nvm+jj+ CELL_W_NVM][KK];
			F_ETHER_BLOCK[3][KK] = f_bin_out[ii*cell_w_nvm+jj+ CELL_W_NVM+1][KK];
					} for (int ss = 0, SS < Block_size * BLOCK_SIZE; ss++) {for (int mm = 0; mm < BIN_NVM; mm++) { F_max = (f_ether_block[ss][mm] > F_max)?
				F_ETHER_BLOCK[SS][MM]: F_max; 
					}} for (int ss = 0, SS < Block_size * BLOCK_SIZE; ss++) {for (int mm = 0; mm < BIN_NVM; mm++) {
					F_ETHER_BLOCK[SS][MM]/= F_max;
				f_last_array[uu++] = f_ether_block[ss][mm];
  }
			}

		}
	}

Source code Download Address: http://download.csdn.net/detail/u011960822/8919895





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.