Facebook
From Mustard Dove, 9 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 779
  1. %Image descriptor based on Histogram of Orientated Gradients for gray-level images. This code
  2. %was developed for the work: O. Ludwig, D. Delgado, V. Goncalves, and U. Nunes, 'Trainable
  3. %Classifier-Fusion Schemes: An Application To Pedestrian Detection,' In: 12th International IEEE
  4. %Conference On Intelligent Transportation Systems, 2009, St. Louis, 2009. V. 1. P. 432-437. In
  5. %case of publication with this code, please cite the paper above.
  6.  
  7. function H=HOG(Im)
  8. nwin_x=4;%set here the number of HOG windows per bound box
  9. nwin_y=4;
  10. B= 16;%set here the number of histogram bins
  11. [L,C]=size(Im); % L num of lines ; C num of columns
  12. H=zeros(nwin_x*nwin_y*B,1); % column vector with zeros
  13. m=sqrt(L/2);
  14. if C==1 % if num of columns==1
  15.     Im=im_recover(Im,m,2*m);%verify the size of image, e.g. 25x50
  16.     L=2*m;
  17.     C=m;
  18. end
  19. Im=double(Im);
  20. step_x=floor(C/(nwin_x+1));
  21. step_y=floor(L/(nwin_y+1));
  22. cont=0;
  23. hx = [-1,0,1];
  24. hy = -hx';
  25. grad_xr = imfilter(double(Im),hx);
  26. grad_yu = imfilter(double(Im),hy);
  27. angles=atan2(grad_yu,grad_xr);
  28. magnit=((grad_yu.^2)+(grad_xr.^2)).^.5;
  29. for n=0:nwin_y-1
  30.     for m=0:nwin_x-1
  31.         cont=cont+1;
  32.         angles2=angles(n*step_y+1:(n+2)*step_y,m*step_x+1:(m+2)*step_x);
  33.         magnit2=magnit(n*step_y+1:(n+2)*step_y,m*step_x+1:(m+2)*step_x);
  34.         v_angles=angles2(:);    
  35.         v_magnit=magnit2(:);
  36.         K=max(size(v_angles));
  37.         %assembling the histogram with 9 bins (range of 20 degrees per bin)
  38.         bin=0;
  39.         H2=zeros(B,1);
  40.         for ang_lim=-pi+2*pi/B:2*pi/B:pi
  41.             bin=bin+1;
  42. %             for k=1:K
  43. %                 if v_angles(k)<ang_lim
  44. %                     v_angles(k)=100;
  45. %                     H2(bin)=H2(bin)+v_magnit(k);
  46. %                 end
  47. %             end
  48.             idx = find(v_angles<ang_lim);
  49.             v_angles(idx) = 100;
  50.             H2(bin) = H2(bin) + sum(v_magnit(idx));
  51.         end
  52.                
  53.         H2=H2/(norm(H2)+0.01);        
  54.         H((cont-1)*B+1:cont*B,1)=H2;
  55.     end
  56. end
  57.