HOGアルゴリズムのmatlab実装1.0


  • HOGアルゴリズムの実現過程
  • 画像正規化
  • 勾配ろ過
  • ヒストグラム統計のユニット区分
  • hog特徴ベクトルを正規化

  • HOGアルゴリズムの実現過程
    1.画像正規化
    従来の画像正規化は一般的にgamma標準化形式を採用している:作用時に光照射などの印象要素を効果的に除去する
    2.勾配ろ過
    3.ヒストグラム統計の単位区分
    4.hog特徴ベクトルの正規化
    正規化動作はブロックに対して行われ,主に特徴ベクトル空間が光照射シャドウと変化にロバスト性をもたらすようにした.
    次はmatlabコード(回転)
    clear;
    clc;
    
    img=imread('D:\lena.jpg');%    
    %% 1、   
    img=rgb2gray(img);%          
    img=double(img);
    figure;
    imshow(img,[]);%    
    step=8;      %step*step       cell
    [m1 ,n1]=size(img);%      
    %       step      ,           
    img=imresize(img,[floor(m1/step)*step,floor(n1/step)*step],'nearest');
    [m,n]=size(img);
    % 1、    
    figure;
    img=sqrt(img); 
    imshow(img,[]);%    
    %%       ,     ,   
    fy=[-1 0 1];        %      
    fx=fy';             %      ,      
    Iy=imfilter(img,fy,'replicate');    %    
    Ix=imfilter(img,fx,'replicate');    %    
    Ied=sqrt(Ix.^2+Iy.^2);              %           
    Iphase=Iy./Ix;              %    ,   inf,-inf,nan,  nan       
    
    figure;                     %%       
    imshow(Ied,[]);             %         
    
    %%      cell
    orient=9;               %          
    angular=360/orient;        %          ,      ,0 40     ...
    Cell=cell(1,1);            %        ,cell        ,       
    %%     orient        
    ii=1;                      
    jj=1;
    for i=1:step:m-step         %     m/step    ,   i=1:step:m-step
        ii=1;
        for j=1:step:n      %    
            tmpx=Ix(i:i+step-1,j:j+step-1);     %  
            %%      
            tmped=Ied(i:i+step-1,j:j+step-1);  
            %%          
            tmped=tmped/sum(sum(tmped));    
            %%         
            tmpphase=Iphase(i:i+step-1,j:j+step-1);
            %%      
            Hist=zeros(1,orient);               %  step*step          ,  cell
    
            %%             
             for p=1:step
                for q=1:step
                    %%           True for Not-a-Number.        ,   
                    if isnan(tmpphase(p,q))==1  %     0/0                                   
                        tmpphase(p,q)=0;
                    end
                    %%        
                    ang=atan(tmpphase(p,q));    %atan   [-90 90]   
                    ang=mod(ang*180/pi,360);    %    ,-90 270
                    if tmpx(p,q)<0              %  x         
                        if ang<90               %       
                            ang=ang+180;        %      
                        end
                        if ang>270              %       
                            ang=ang-180;        %      
                        end
                    end
                    ang=ang+0.0000001;          %  ang 0
                    Hist(ceil(ang/angular)) = Hist(ceil(ang/angular))+tmped(p,q);   %ceil    ,        
                end
             end
            %%         
            Hist=Hist/sum(Hist);    
            Cell{ii,jj}=Hist;       %  Cell 
            ii=ii+1;                %  Cell y      
        end
        jj=jj+1;                    %  Cell x      
    end
    
    %%     feature,2*2 cell    block,      block
    [m2, n2]=size(Cell);
    feature=cell(1,(m2-1)*(n2-1));
    for i=1:m2-1
       for j=1:n2-1           
            f=[];
            f=[f Cell{i,j}(:)' Cell{i,j+1}(:)' Cell{i+1,j}(:)' Cell{i+1,j+1}(:)'];
            feature{(i-1)*(n2-1)+j}=f;
       end
    end
    
    l=length(feature);
    f=[];
    for i=1:l
        f=[f;feature{i}(:)'];  
    end 
    figure
    mesh(f)