MATLAB exercise program (hog-Oriented Gradient histogram)

Source: Internet
Author: User

Hog (histogram of Oriented Gradient) Direction gradient histogram, mainly used to extract image features, the most common is to use SVM for pedestrian detection.

The algorithm flow chart is as follows (in this paper ):

Next I will repeat it with my own program:

1. Gamma Correction for the original image, IMG = SQRT (IMG );

2. Find the vertical edge, horizontal edge, edge strength, and edge slope of the image.

3. Divide the image into a cell every 16*16 pixels (Other pixels can also be used. For 256*256 Lena, it is divided into 16*16 cells.

4. Calculate the gradient histogram for each cell. Generally, 9 (other directions can also be used) (features) are taken, that is, each 360/9 = 40 degrees is allocated to one direction, and the direction is weighted by the pixel edge strength. The last normalized histogram.

5. Every 2*2 cells (other cells can also be used) form a block, so here there are (16-1) * (16-1) = 225 blocks.

6. Therefore, each block has 2*2*9 features, a total of 225 blocks, so the total number of features is 225*36.

Of course, generally, hog features are not used for the entire image, but for a sliding window in the image.

Lena diagram:

225*36 features:

The Matlab code is as follows:

Clear all; close all; clc?img=double(imread('lena.jpg '); imshow (IMG, []); [m n] = size (IMG); IMG = SQRT (IMG ); % Gamma Correction % The following is edge FY = [-1 0 1]; % defines the vertical template FX = FY '; % defines the horizontal template Iy = imfilter (IMG, fy, 'replicate'); % vertical edge IX = imfilter (IMG, FX, 'replicate'); % horizontal edge IED = SQRT (IX. ^ 2 + Iy. ^ 2); % edge strength iphase = Iy. /IX; % edge slope, some of which are INF,-INF, Nan, where Nan needs to be processed again % The following is the result of cellstep = 16; % step * Step pixels as a unit Orient = 9; % Number of directions in the direction histogram Jiao = 360/Orient; % Number of angles in each direction cell = cell ); % For all the angle histograms, cell can be dynamically increased, so we first set a II = 1; JJ = 1; for I = 1: Step: M % if the M/step being processed is not an integer, I = 1: Step: M-Step II = 1; for j = 1: Step: N % annotation ditto tmpx = IX (I: I + step-1, J: J + step-1); tmped = IED (I: I + step-1, J: J + step-1); tmped = tmped/sum (tmped); % partial edge intensity normalization tmpphase = iphase (I: I + step-1, J: J + step-1); hist = zeros (1, Orient); % current step * Step pixel block statistical angle histogram, that is, cell for p = 1: Step for q = 1: step if isnan (tmpphase (p, q) = 1% 0/0 will get Nan. If the pixel is Nan, reset it to 0 tmpphase (p, q) = 0; end ang = atan (tmpphase (p, q); % atan calculates ang = Mod (ANG * 180/PI, 360) between degrees [-90 ); % all changed to normal,-90 changed to 270 if tmpx (p, q) <0% determined the true angle based on X direction if ang <90% if it is the first quadrant ang = ang + 180; % Move to third quadrant end if ang> 270% if the Quadrant ang = ang-180; % Move to second quadrant end ang = ang + 0.0000001; % prevent Ang from being 0 hist (Ceil (ANG/Jiao) = Hist (Ceil (ANG/Jiao) + tmped (p, q); % Ceil rounded up, use the edge strength weighted end hist = Hist/sum (hist); % the direction histogram normalized cell {II, JJ} = Hist; % Put in cell II = II + 1; % Y-coordinate cyclic variable for cell end JJ = JJ + 1; % for cell X-coordinate cyclic variable end % The following is a feature, 2*2 cells form a block, no explicit block [m n] = size (cell); feature = cell (1, m-1) * (n-1); for I = 1: m-1 for j = 1: n-1 F = []; F = [F cell {I, j} (:) 'cell {I, J + 1} (:) 'cell {I + 1, J} (:) 'cell {I + 1, J + 1} (:) ']; feature {(I-1) * (n-1) + J} = f; endend % ends here. feature is the desired %. The following is the L = length (feature) written for display; F = []; for I = 1: l f = [F; feature {I} (:) ']; end figuremesh (f)

 

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.