图像HOG特征提取

目录

一、理论基础

1.1梯度计算

1.2梯度方向分配

1.3块归一化

1.4实现步骤

二、核心程序

三、仿真结论


一、理论基础


      在计算机视觉领域中,图像的特征提取是一项重要的任务。HOG(Histogram of Oriented Gradients)特征是一种用于图像特征提取的常用技术,它可以有效地描述图像中的形状和边缘信息,广泛应用于物体检测、人脸识别等领域。本文将从数学模型和实现步骤两个方面,详细介绍图像HOG特征提取算法。

1.1梯度计算

        HOG特征提取算法首先需要计算图像中每个像素点的梯度值,以描述图像中的边缘信息。对于图像中的每个像素点p=(x,y),我们可以计算其水平方向和竖直方向上的梯度值G_x和G_y,然后根据G_x和G_y计算出该像素点的梯度强度G和梯度方向\theta:

$$
G=\sqrt{G_x^2+G_y^2}
$$

$$
\theta=\tan^{-1}\frac{G_y}{G_x}
$$

其中,G_x和G_y分别表示像素点p在水平和竖直方向上的梯度值。

1.2梯度方向分配


       将图像划分为若干个小的细胞(cell),对于每个细胞内的像素点,我们将其梯度方向分配到最近的几个方向上的直方图中。具体地,我们将梯度方向\theta分配到距离其最近的两个方向上的直方图中,即令:

$$
b_i=\frac{\theta-\theta_i}{\Delta\theta}
$$

       其中,b_i表示\theta被分配到第i个直方图中的权重,\theta_i表示第i个直方图所表示的方向,\Delta\theta表示相邻两个方向之间的间隔。我们可以使用线性插值法将\theta分配到两个最近的方向上的直方图中:

$$
H_i=H_i+b_iG
$$

其中,H_i表示第i个直方图的值,G表示当前像素点的梯度强度。

1.3块归一化


       为了使HOG特征具有不变性和泛化性,我们需要对每个细胞进行块归一化。具体地,我们将每个细胞与其相邻的若干个细胞合并成一个块(block),并对块内的直方图进行归一化。归一化的方法可以采用L2范数或L1范数,即:

$$
v=\frac{v}{\sqrt{|v|^2+\epsilon^2}}
$$

$$
v=\frac{v}{|v|_1+\epsilon}
$$

其中,v表示块内的直方图,\epsilon是一个小常数,用来避免分母为0的情况。

1.4实现步骤

HOG特征提取算法的实现步骤如下:

计算图像中每个像素点的梯度值G和梯度方向\theta。

将图像划分为若干个细胞,并将每个像素点的梯度方向分配到最近的几个方向上的直方图中。

将每个细胞与其相邻的若干个细胞合并成一个块,并对块内的直方图进行归一化。

将所有块内的直方图连接起来,得到一个HOG特征向量。

       具体地,步骤1可以通过使用Sobel算子或Scharr算子来计算梯度值和梯度方向。步骤2中的直方图数量可以根据需要进行设置,一般取8或9个方向,每个方向之间的间隔为20度或40度。步骤3中的归一化方法可以根据需要选择,一般使用L2范数进行归一化。步骤4中,连接所有块内的直方图可以按照从左到右、从上到下的顺序进行连接,得到一个长向量作为图像的HOG特征向量。

       本文详细介绍了图像HOG特征提取算法的数学模型和实现步骤。HOG特征提取算法可以有效地描述图像中的形状和边缘信息,广泛应用于物体检测、人脸识别等领域。通过对HOG特征提取算法的理解和实现,可以更好地理解计算机视觉中的特征提取技术,并应用于实际的图像处理任务中。

二、核心程序

................................................................................
if size(im,3)==3
    im=rgb2gray(im);
end
im=double(im);
rows=size(im,1);
cols=size(im,2);
Ix=im; %Basic Matrix assignment
Iy=im; %Basic Matrix assignment
% Gradients in X and Y direction. Iy is the gradient in X direction and Iy
% is the gradient in Y direction
for i=1:rows-2
    Iy(i,:)=(im(i,:)-im(i+2,:));
end
for i=1:cols-2
    Ix(:,i)=(im(:,i)-im(:,i+2));
end
gauss=fspecial('gaussian',8); %% Initialized a gaussian filter with sigma=0.5 * block width.    
angle=atand(Ix./Iy); % Matrix containing the angles of each edge gradient
angle=imadd(angle,90); %Angles in range (0,180)
magnitude=sqrt(Ix.^2 + Iy.^2);
% figure,imshow(uint8(angle));
% figure,imshow(uint8(magnitude));
% Remove redundant pixels in an image. 
angle(isnan(angle))=0;
magnitude(isnan(magnitude))=0;
feature=[]; %initialized the feature vector
% Iterations for Blocks
for i = 0: rows/8 - 2
    for j= 0: cols/8 -2
        %disp([i,j])
        
        mag_patch = magnitude(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
        %mag_patch = imfilter(mag_patch,gauss);
        ang_patch = angle(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
        
        block_feature=[];
        
        %Iterations for cells in a block
        for x= 0:1
            for y= 0:1
                angleA =ang_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
                magA   =mag_patch(8*x+1:8*x+8, 8*y+1:8*y+8); 
                histr  =zeros(1,9);
                
                %Iterations for pixels in one cell
                for p=1:8
                    for q=1:8
%                       
                        alpha= angleA(p,q);
                        
                        % Binning Process (Bi-Linear Interpolation)
                        if alpha>10 && alpha<=30
                            histr(1)=histr(1)+ magA(p,q)*(30-alpha)/20;
                            histr(2)=histr(2)+ magA(p,q)*(alpha-10)/20;
                        elseif alpha>30 && alpha<=50
                            histr(2)=histr(2)+ magA(p,q)*(50-alpha)/20;                 
                            histr(3)=histr(3)+ magA(p,q)*(alpha-30)/20;
                        elseif alpha>50 && alpha<=70
                            histr(3)=histr(3)+ magA(p,q)*(70-alpha)/20;
                            histr(4)=histr(4)+ magA(p,q)*(alpha-50)/20;
                        elseif alpha>70 && alpha<=90
                            histr(4)=histr(4)+ magA(p,q)*(90-alpha)/20;
                            histr(5)=histr(5)+ magA(p,q)*(alpha-70)/20;
                        elseif alpha>90 && alpha<=110
                            histr(5)=histr(5)+ magA(p,q)*(110-alpha)/20;
                            histr(6)=histr(6)+ magA(p,q)*(alpha-90)/20;
                        elseif alpha>110 && alpha<=130
                            histr(6)=histr(6)+ magA(p,q)*(130-alpha)/20;
                            histr(7)=histr(7)+ magA(p,q)*(alpha-110)/20;
                        elseif alpha>130 && alpha<=150
                            histr(7)=histr(7)+ magA(p,q)*(150-alpha)/20;
                            histr(8)=histr(8)+ magA(p,q)*(alpha-130)/20;
                        elseif alpha>150 && alpha<=170
                            histr(8)=histr(8)+ magA(p,q)*(170-alpha)/20;
                            histr(9)=histr(9)+ magA(p,q)*(alpha-150)/20;
                        elseif alpha>=0 && alpha<=10
                            histr(1)=histr(1)+ magA(p,q)*(alpha+10)/20;
                            histr(9)=histr(9)+ magA(p,q)*(10-alpha)/20;
                        elseif alpha>170 && alpha<=180
                            histr(9)=histr(9)+ magA(p,q)*(190-alpha)/20;
                            histr(1)=histr(1)+ magA(p,q)*(alpha-170)/20;
                        end
                        
                
                    end
                end
                block_feature=[block_feature histr]; % Concatenation of Four histograms to form one block feature
                                
            end
        end
        % Normalize the values in the block using L1-Norm
        block_feature=block_feature/sqrt(norm(block_feature)^2+.01);
               
        feature=[feature block_feature]; %Features concatenation
    end
end
feature(isnan(feature))=0; %Removing Infinitiy values
% Normalization of the feature vector using L2-Norm
feature=feature/sqrt(norm(feature)^2+.001);
for z=1:length(feature)
    if feature(z)>0.2
         feature(z)=0.2;
    end
end
feature=feature/sqrt(norm(feature)^2+.001);        
% toc;       
UP2144

三、仿真结论

猜你喜欢

转载自blog.csdn.net/ccsss22/article/details/131361946