|
HOG(Histograms of Oriented Gradients)用于特征分类:
视频链接:
视频:http://pan.baidu.com/s/1qYly5Fe
录制的视频是算法底层原理讲解,底层代码实现,方便大家真正掌握算法实质,开发出更加出色的算法。录制视频的初衷是:避免读者朋友利用大把时间学习已有常见算法,本系列视频旨在让读者朋友快速深入了解这些常见算法原理,有更多的时间去研究更加高大上算法(价值)。
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
HOG特征提取算法的实现过程(图像特征提取):
1)灰度化,如果是彩色图像,采用img = rgb2gray(img)灰度化;Gray = 0.3*R+0.59*G+0.11*B;
2)采用Gamma校正法对输入图像进行颜色空间的标准化(归一化);目的是调节图像的对比度,降低图像局部的阴影和光照变化所造成的影响,同时可以抑制噪音的干扰;也可以简便地直接采用 img = sqrt(img);
3)计算图像每个像素的梯度(包括赋值和相位);主要是为了捕获轮廓信息,相位保证图像信息的特征唯一性。
滤波掩膜如[-1,0,1],具体看sobel、canny、prewitt等(MATLAB图像滤波去噪分析及其应用)
mag = sqrt( dx^2 + dy^2 )
phase = atan2(dy, dx)
dx为水平梯度,dy为垂直梯度;
4)对图像进行分块处理,每一个块是一个cell,每一个cell大小一般为16x16,cell水平、垂直移动步长为cell大小的一半,即水平、垂直步长为8,具体如下,rows和cols分别为图像高、宽;- for i = 0: rows/8 - 2
- for j= 0: cols/8 -2
- mag_patch = mag(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
- ang_patch = phase(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
- end
- end
复制代码 5)对每一个16x16的cell,进行8x8 blocks扫描,得到4个blocks,对这4个blocks进行不同梯度角度方向直方图求解:- 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);
- end
- end
复制代码 6)num_bins=9,对每一个block不同方向梯度进行直方图累加处理,具体如下:- alpha= angleA(p,q);
- % Binning Process (Bi-Linear Interpolation)
- if 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>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>170 && alpha<=180
- histr(9)=histr(9)+ magA(p,q)*(190-alpha)/20;
- histr(1)=histr(1)+ magA(p,q)*(alpha-170)/20;
- end
复制代码 7)串联所有的cell特征,feature=[feature, block_feature];
8)归一化特征feature,feature=feature/sqrt(norm(feature)^2+0.001);
得到的feature特征为1xn的矩阵,区别于HoG目标探测,HoG目标探测,得到的feature为MxNxD的矩阵,其中,n = M*N*D;
主程序如下:- % Feature Extraction
- feat=hog_vector(im);
- % Energy calculation
- E=feat.^2;
- Energy=(sum(E(:)))/(512*512);
- %features set
- features = [mean(feat(:)),var(feat(:)),skewness(feat(:)),kurtosis(feat(:)),entropy(feat(:)),Energy];
- FeaturesHog(Index,:)=features;
复制代码 hog_vector函数如下:
- function [feature] = hog_vector(im)
- 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>=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>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>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+0.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+0.001);
- for z=1:length(feature)
- if feature(z)>0.2
- feature(z)=0.2;
- end
- end
- feature=feature/sqrt(norm(feature)^2+0.001);
复制代码
|
|