Hello Mat

 找回密码
 立即注册
查看: 6651|回复: 0

HOG(Histograms of Oriented Gradients)用于特征分类

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-9-28 21:06:11 | 显示全部楼层 |阅读模式
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分别为图像高、宽;
  1. for i = 0: rows/8 - 2
  2.     for j= 0: cols/8 -2
  3.         mag_patch = mag(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
  4.         ang_patch = phase(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
  5.     end
  6. end
复制代码
5)对每一个16x16的cell,进行8x8 blocks扫描,得到4个blocks,对这4个blocks进行不同梯度角度方向直方图求解:
  1. for x= 0:1
  2.     for y= 0:1
  3.         angleA =ang_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
  4.         magA   =mag_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
  5.     end
  6. end
复制代码
6)num_bins=9,对每一个block不同方向梯度进行直方图累加处理,具体如下:
  1. alpha= angleA(p,q);
  2. % Binning Process (Bi-Linear Interpolation)
  3. if alpha>=0 && alpha<=10
  4.     histr(1)=histr(1)+ magA(p,q)*(alpha+10)/20;
  5.     histr(9)=histr(9)+ magA(p,q)*(10-alpha)/20;
  6. elseif alpha>10 && alpha<=30
  7.     histr(1)=histr(1)+ magA(p,q)*(30-alpha)/20;
  8.     histr(2)=histr(2)+ magA(p,q)*(alpha-10)/20;
  9. elseif alpha>30 && alpha<=50
  10.     histr(2)=histr(2)+ magA(p,q)*(50-alpha)/20;
  11.     histr(3)=histr(3)+ magA(p,q)*(alpha-30)/20;
  12. elseif alpha>50 && alpha<=70
  13.     histr(3)=histr(3)+ magA(p,q)*(70-alpha)/20;
  14.     histr(4)=histr(4)+ magA(p,q)*(alpha-50)/20;
  15. elseif alpha>70 && alpha<=90
  16.     histr(4)=histr(4)+ magA(p,q)*(90-alpha)/20;
  17.     histr(5)=histr(5)+ magA(p,q)*(alpha-70)/20;
  18. elseif alpha>90 && alpha<=110
  19.     histr(5)=histr(5)+ magA(p,q)*(110-alpha)/20;
  20.     histr(6)=histr(6)+ magA(p,q)*(alpha-90)/20;
  21. elseif alpha>110 && alpha<=130
  22.     histr(6)=histr(6)+ magA(p,q)*(130-alpha)/20;
  23.     histr(7)=histr(7)+ magA(p,q)*(alpha-110)/20;
  24. elseif alpha>130 && alpha<=150
  25.     histr(7)=histr(7)+ magA(p,q)*(150-alpha)/20;
  26.     histr(8)=histr(8)+ magA(p,q)*(alpha-130)/20;
  27. elseif alpha>150 && alpha<=170
  28.     histr(8)=histr(8)+ magA(p,q)*(170-alpha)/20;
  29.     histr(9)=histr(9)+ magA(p,q)*(alpha-150)/20;
  30. elseif alpha>170 && alpha<=180
  31.     histr(9)=histr(9)+ magA(p,q)*(190-alpha)/20;
  32.     histr(1)=histr(1)+ magA(p,q)*(alpha-170)/20;
  33. 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;

主程序如下:
  1. % Feature Extraction
  2. feat=hog_vector(im);
  3. % Energy calculation
  4. E=feat.^2;
  5. Energy=(sum(E(:)))/(512*512);
  6. %features set
  7. features = [mean(feat(:)),var(feat(:)),skewness(feat(:)),kurtosis(feat(:)),entropy(feat(:)),Energy];
  8. FeaturesHog(Index,:)=features;
复制代码
hog_vector函数如下:
  1. function [feature] = hog_vector(im)

  2. im=double(im);
  3. rows=size(im,1);
  4. cols=size(im,2);
  5. Ix=im; %Basic Matrix assignment
  6. Iy=im; %Basic Matrix assignment

  7. % Gradients in X and Y direction. Iy is the gradient in X direction and Iy
  8. % is the gradient in Y direction
  9. for i=1:rows-2
  10.     Iy(i,:)=(im(i,:)-im(i+2,:));
  11. end
  12. for i=1:cols-2
  13.     Ix(:,i)=(im(:,i)-im(:,i+2));
  14. end

  15. gauss=fspecial('gaussian',8); %% Initialized a gaussian filter with sigma=0.5 * block width.

  16. angle=atand(Ix./Iy); % Matrix containing the angles of each edge gradient
  17. angle=imadd(angle,90); %Angles in range (0,180)
  18. magnitude=sqrt(Ix.^2 + Iy.^2);

  19. % figure,imshow(uint8(angle));
  20. % figure,imshow(uint8(magnitude));

  21. % Remove redundant pixels in an image.
  22. angle(isnan(angle))=0;
  23. magnitude(isnan(magnitude))=0;

  24. feature=[]; %initialized the feature vector

  25. % Iterations for Blocks
  26. for i = 0: rows/8 - 2
  27.     for j= 0: cols/8 -2
  28.         %disp([i,j])
  29.         
  30.         mag_patch = magnitude(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
  31.         %mag_patch = imfilter(mag_patch,gauss);
  32.         ang_patch = angle(8*i+1 : 8*i+16 , 8*j+1 : 8*j+16);
  33.         
  34.         block_feature=[];
  35.         
  36.         %Iterations for cells in a block
  37.         for x= 0:1
  38.             for y= 0:1
  39.                 angleA =ang_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
  40.                 magA   =mag_patch(8*x+1:8*x+8, 8*y+1:8*y+8);
  41.                 histr  =zeros(1,9);
  42.                
  43.                 %Iterations for pixels in one cell
  44.                 for p=1:8
  45.                     for q=1:8
  46.                         %
  47.                         alpha= angleA(p,q);
  48.                         
  49.                         % Binning Process (Bi-Linear Interpolation)
  50.                         if alpha>=0 && alpha<=10
  51.                             histr(1)=histr(1)+ magA(p,q)*(alpha+10)/20;
  52.                             histr(9)=histr(9)+ magA(p,q)*(10-alpha)/20;
  53.                         elseif alpha>10 && alpha<=30
  54.                             histr(1)=histr(1)+ magA(p,q)*(30-alpha)/20;
  55.                             histr(2)=histr(2)+ magA(p,q)*(alpha-10)/20;
  56.                         elseif alpha>30 && alpha<=50
  57.                             histr(2)=histr(2)+ magA(p,q)*(50-alpha)/20;
  58.                             histr(3)=histr(3)+ magA(p,q)*(alpha-30)/20;
  59.                         elseif alpha>50 && alpha<=70
  60.                             histr(3)=histr(3)+ magA(p,q)*(70-alpha)/20;
  61.                             histr(4)=histr(4)+ magA(p,q)*(alpha-50)/20;
  62.                         elseif alpha>70 && alpha<=90
  63.                             histr(4)=histr(4)+ magA(p,q)*(90-alpha)/20;
  64.                             histr(5)=histr(5)+ magA(p,q)*(alpha-70)/20;
  65.                         elseif alpha>90 && alpha<=110
  66.                             histr(5)=histr(5)+ magA(p,q)*(110-alpha)/20;
  67.                             histr(6)=histr(6)+ magA(p,q)*(alpha-90)/20;
  68.                         elseif alpha>110 && alpha<=130
  69.                             histr(6)=histr(6)+ magA(p,q)*(130-alpha)/20;
  70.                             histr(7)=histr(7)+ magA(p,q)*(alpha-110)/20;
  71.                         elseif alpha>130 && alpha<=150
  72.                             histr(7)=histr(7)+ magA(p,q)*(150-alpha)/20;
  73.                             histr(8)=histr(8)+ magA(p,q)*(alpha-130)/20;
  74.                         elseif alpha>150 && alpha<=170
  75.                             histr(8)=histr(8)+ magA(p,q)*(170-alpha)/20;
  76.                             histr(9)=histr(9)+ magA(p,q)*(alpha-150)/20;
  77.                         elseif alpha>170 && alpha<=180
  78.                             histr(9)=histr(9)+ magA(p,q)*(190-alpha)/20;
  79.                             histr(1)=histr(1)+ magA(p,q)*(alpha-170)/20;
  80.                         end

  81.                     end
  82.                 end
  83.                 block_feature=[block_feature, histr]; % Concatenation of Four histograms to form one block feature
  84.                
  85.             end
  86.         end
  87.         % Normalize the values in the block using L1-Norm
  88.         block_feature=block_feature/sqrt(norm(block_feature)^2+0.01);
  89.         
  90.         feature=[feature, block_feature]; %Features concatenation
  91.     end
  92. end
  93. feature(isnan(feature))=0; % Removing Infinitiy values

  94. % Normalization of the feature vector using L2-Norm
  95. feature=feature/sqrt(norm(feature)^2+0.001);
  96. for z=1:length(feature)
  97.     if feature(z)>0.2
  98.         feature(z)=0.2;
  99.     end
  100. end
  101. feature=feature/sqrt(norm(feature)^2+0.001);
复制代码














算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-27 08:38 , Processed in 0.261180 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表