LBP特徵提取

第一篇博文特徵臉方法不同,LBP(Local Binary Patterns,局部二值模式)是提取局部特徵作爲判別依據的。LBP方法顯著的優點是對光照不敏感,但是依然沒有解決姿態和表情的問題。不過相比於特徵臉方法,LBP的識別率已經有了很大的提升。在[1]的文章裏,有些人臉庫的識別率已經達到了98%+。


1、LBP特徵提取

最初的LBP是定義在像素3x3鄰域內的,以鄰域中心像素爲閾值,將相鄰的8個像素的灰度值與其進行比較,若周圍像素值大於中心像素值,則該像素點的位置被標記爲1,否則爲0。這樣,3x3鄰域內的8個點經比較可產生8位二進制數(通常轉換爲十進制數即LBP碼,共256種),即得到該鄰域中心像素點的LBP值,並用這個值來反映該區域的紋理信息。如下圖所示:



用比較正式的公式來定義的話:


其中代表3x3鄰域的中心元素,它的像素值爲ic,ip代表鄰域內其他像素的值。s(x)是符號函數,定義如下:



LBP的改進版本

(1)圓形LBP算子

基本的 LBP算子的最大缺陷在於它只覆蓋了一個固定半徑範圍內的小區域,這顯然不能滿足不同尺寸和頻率紋理的需要。爲了適應不同尺度的紋理特徵,並達到灰度和旋轉不變性的要求,Ojala等對 LBP 算子進行了改進,將 3×3鄰域擴展到任意鄰域,並用圓形鄰域代替了正方形鄰域,改進後的 LBP 算子允許在半徑爲 R 的圓形鄰域內有任意多個像素點。從而得到了諸如半徑爲R的圓形區域內含有P個採樣點的LBP算子。比如下圖定了一個5x5的鄰域:


上圖內有八個黑色的採樣點,每個採樣點的值可以通過下式計算:


其中爲鄰域中心點,爲某個採樣點。通過上式可以計算任意個採樣點的座標,但是計算得到的座標未必完全是整數,所以可以通過雙線性插值來得到該採樣點的像素值:



(2)LBP等價模式

一個LBP算子可以產生不同的二進制模式,對於半徑爲R的圓形區域內含有P個採樣點的LBP算子將會產生2^P種模式。很顯然,隨着鄰域集內採樣點數的增加,二進制模式的種類是急劇增加的。例如:5×5鄰域內20個採樣點,有220=1,048,576種二進制模式。如此多的二值模式無論對於紋理的提取還是對於紋理的識別、分類及信息的存取都是不利的。同時,過多的模式種類對於紋理的表達是不利的。例如,將LBP算子用於紋理分類或人臉識別時,常採用LBP模式的統計直方圖來表達圖像的信息,而較多的模式種類將使得數據量過大,且直方圖過於稀疏。因此,需要對原始的LBP模式進行降維,使得數據量減少的情況下能最好的代表圖像的信息。

        爲了解決二進制模式過多的問題,提高統計性,Ojala提出了採用一種“等價模式”(Uniform Pattern)來對LBP算子的模式種類進行降維。Ojala等認爲,在實際圖像中,絕大多數LBP模式最多隻包含兩次從10或從01的跳變。因此,Ojala將“等價模式”定義爲:當某個LBP所對應的循環二進制數從01或從10最多有兩次跳變時,該LBP所對應的二進制就稱爲一個等價模式類。如00000000(0次跳變),00000111(只含一次從0到1的跳變),10001111(先由1跳到0,再由0跳到1,共兩次跳變)都是等價模式類。除等價模式類以外的模式都歸爲另一類,稱爲混合模式類,例如10010111(共四次跳變)。比如下圖給出了幾種等價模式的示意圖。



       通過這樣的改進,二進制模式的種類大大減少,而不會丟失任何信息。模式數量由原來的2P種減少爲 P ( P-1)+2種,其中P表示鄰域集內的採樣點數。對於3×3鄰域內8個採樣點來說,二進制模式由原始的256種減少爲58種,這使得特徵向量的維數更少,並且可以減少高頻噪聲帶來的影響。這幾段摘自[2]。


通過上述方法,每個像素都會根據鄰域信息得到一個LBP值,如果以圖像的形式顯示出來可以得到下圖,明顯LBP對光照有較強的魯棒性。



2、LBP特徵匹配

如果將以上得到的LBP值直接用於人臉識別,其實和不提取LBP特徵沒什麼區別,會造成計算量準確率等一系列問題。文獻[1]中,將一副人臉圖像分爲7x7的子區域(如下圖),並在子區域內根據LBP值統計其直方圖,以直方圖作爲其判別特徵。這樣做的好處是在一定範圍內避免圖像沒完全對準的情況,同時也對LBP特徵做了降維處理。


對於得到的直方圖特徵,有多種方法可以判別其相似性,假設已知人臉直方圖爲Mi,待匹配人臉直方圖爲Si,那麼可以通過:

(1)直方圖交叉核方法


該方法的介紹在博文:Histogram intersection(直方圖交叉核,Pyramid Match Kernel)

(2)卡方統計方法


該方法的介紹在博文:卡方檢驗(Chi square statistic)



參考文獻:

[1]Timo Ahonen, Abdenour Hadid:Face Recognition with Local Binary Patterns

[2]目標檢測的圖像特徵提取之(二)LBP特徵

圖像分析:LBP特徵解析與代碼

分類: 算法與理論研究 圖像分析專欄 542人閱讀 評論(0) 收藏 舉報

LBP(Local Binary Patterns),即局部二值模式,是一種描述圖像局部空間結構的非參數算子。芬蘭Oulu大學的T.Ojala等人於1996年提出這個算子用來分析圖像紋理特徵,並且描述了它在紋理分類中的強區分能力。LBP算子定義爲一種灰度尺度不變的紋理算子,是從局部鄰域紋理的普通定義得來的。

基本思想是:用中心像素的灰度值作爲閾值,與它的鄰域相比較得到的二進制碼來表述局部紋理特徵。

在紋理分析方面,LBP算子是最好的紋理描述符之一,它的主要優點有以下幾點:

Ø 通過它的定義可知,LBP算子的灰度尺度不隨任何單一變換而變化,因此灰度尺度的魯棒性好,也就是光照條件下的魯棒性好;

Ø 計算速度快。由於它可以通過在小鄰域內進行比較操作得到,使得在複雜的實時條件下分析圖像成爲可能;

Ø 由於LBP算子是一種無參數(Non-Parametric)的方法,在應用過程中不需要對它的分佈進行預先假設。

_____________________________________________________________________________________


[plain] view plaincopy
  1. %LBP returns the local binary pattern image or LBP histogram of an image.  
  2. %  J = LBP(I,R,N,MAPPING,MODE) returns either a local binary pattern  
  3. %  coded image or the local binary pattern histogram of an intensity  
  4. %  image I. The LBP codes are computed using N sampling points on a   
  5. %  circle of radius R and using mapping table defined by MAPPING.   
  6. %  See the getmapping function for different mappings and use 0 for  
  7. %  no mapping. Possible values for MODE are  
  8. %       'h' or 'hist'  to get a histogram of LBP codes  
  9. %       'nh'           to get a normalized histogram  
  10. %  Otherwise an LBP code image is returned.  
  11. %  
  12. %  J = LBP(I) returns the original (basic) LBP histogram of image I  
  13. %  
  14. %  J = LBP(I,SP,MAPPING,MODE) computes the LBP codes using n sampling  
  15. %  points defined in (n * 2) matrix SP. The sampling points should be  
  16. %  defined around the origin (coordinates (0,0)).  
  17. %  
  18. %  Examples  
  19. %  --------  
  20. %       I=imread('rice.png');  
  21. %       mapping=getmapping(8,'u2');   
  22. %       H1=LBP(I,1,8,mapping,'h'); %LBP histogram in (8,1) neighborhood  
  23. %                                  %using uniform patterns  
  24. %       subplot(2,1,1),stem(H1);  
  25. %  
  26. %       H2=LBP(I);  
  27. %       subplot(2,1,2),stem(H2);  
  28. %  
  29. %       SP=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];  
  30. %       I2=LBP(I,SP,0,'i'); %LBP code image using sampling points in SP  
  31. %                           %and no mapping. Now H2 is equal to histogram  
  32. %                           %of I2.  
  33.   
  34. function result = lbp(varargin) % image,radius,neighbors,mapping,mode)  
  35. % Version 0.3.2  
  36. % Authors: Marko Heikkil�and Timo Ahonen  
  37.   
  38. % Changelog  
  39. % Version 0.3.2: A bug fix to enable using mappings together with a  
  40. % predefined spoints array  
  41. % Version 0.3.1: Changed MAPPING input to be a struct containing the mapping  
  42. % table and the number of bins to make the function run faster with high number  
  43. % of sampling points. Lauge Sorensen is acknowledged for spotting this problem.  
  44.   
  45.   
  46. % Check number of input arguments.  
  47. error(nargchk(1,5,nargin));  
  48.   
  49. image=varargin{1};  
  50. d_image=double(image);  
  51.   
  52. if nargin==1  
  53.     spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];  
  54.     neighbors=8;  
  55.     mapping=0;  
  56.     mode='h';  
  57. end  
  58.   
  59. if (nargin == 2) && (length(varargin{2}) == 1)  
  60.     error('Input arguments');  
  61. end  
  62.   
  63. if (nargin > 2) && (length(varargin{2}) == 1)  
  64.     radius=varargin{2};  
  65.     neighbors=varargin{3};  
  66.       
  67.     spoints=zeros(neighbors,2);  
  68.   
  69.     % Angle step.  
  70.     a = 2*pi/neighbors;  
  71.       
  72.     for i = 1:neighbors  
  73.         spoints(i,1) = -radius*sin((i-1)*a);  
  74.         spoints(i,2) = radius*cos((i-1)*a);  
  75.     end  
  76.       
  77.     if(nargin >= 4)  
  78.         mapping=varargin{4};  
  79.         if(isstruct(mapping) && mapping.samples ~= neighbors)  
  80.             error('Incompatible mapping');  
  81.         end  
  82.     else  
  83.         mapping=0;  
  84.     end  
  85.       
  86.     if(nargin >= 5)  
  87.         mode=varargin{5};  
  88.     else  
  89.         mode='h';  
  90.     end  
  91. end  
  92.   
  93. if (nargin > 1) && (length(varargin{2}) > 1)  
  94.     spoints=varargin{2};  
  95.     neighbors=size(spoints,1);  
  96.       
  97.     if(nargin >= 3)  
  98.         mapping=varargin{3};  
  99.         if(isstruct(mapping) && mapping.samples ~= neighbors)  
  100.             error('Incompatible mapping');  
  101.         end  
  102.     else  
  103.         mapping=0;  
  104.     end  
  105.       
  106.     if(nargin >= 4)  
  107.         mode=varargin{4};  
  108.     else  
  109.         mode='h';  
  110.     end     
  111. end  
  112.   
  113. % Determine the dimensions of the input image.  
  114. [ysize xsize] = size(image);  
  115.   
  116.   
  117.   
  118. miny=min(spoints(:,1));  
  119. maxy=max(spoints(:,1));  
  120. minx=min(spoints(:,2));  
  121. maxx=max(spoints(:,2));  
  122.   
  123. % Block size, each LBP code is computed within a block of size bsizey*bsizex  
  124. bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;  
  125. bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;  
  126.   
  127. % Coordinates of origin (0,0) in the block  
  128. origy=1-floor(min(miny,0));  
  129. origx=1-floor(min(minx,0));  
  130.   
  131. % Minimum allowed size for the input image depends  
  132. % on the radius of the used LBP operator.  
  133. if(xsize < bsizex || ysize < bsizey)  
  134.   error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');  
  135. end  
  136.   
  137. % Calculate dx and dy;  
  138. dx = xsize - bsizex;  
  139. dy = ysize - bsizey;  
  140.   
  141. % Fill the center pixel matrix C.  
  142. C = image(origy:origy+dy,origx:origx+dx);  
  143. d_C = double(C);  
  144.   
  145. bins = 2^neighbors;  
  146.   
  147. % Initialize the result matrix with zeros.  
  148. result=zeros(dy+1,dx+1);  
  149.   
  150. %Compute the LBP code image  
  151.   
  152. for i = 1:neighbors  
  153.   y = spoints(i,1)+origy;  
  154.   x = spoints(i,2)+origx;  
  155.   % Calculate floors, ceils and rounds for the x and y.  
  156.   fy = floor(y); cy = ceil(y); ry = round(y);  
  157.   fx = floor(x); cx = ceil(x); rx = round(x);  
  158.   % Check if interpolation is needed.  
  159.   if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)  
  160.     % Interpolation is not needed, use original datatypes  
  161.     N = image(ry:ry+dy,rx:rx+dx);  
  162.     D = N >= C;   
  163.   else  
  164.     % Interpolation needed, use double type images   
  165.     ty = y - fy;  
  166.     tx = x - fx;  
  167.   
  168.     % Calculate the interpolation weights.  
  169.     w1 = (1 - tx) * (1 - ty);  
  170.     w2 =      tx  * (1 - ty);  
  171.     w3 = (1 - tx) *      ty ;  
  172.     w4 =      tx  *      ty ;  
  173.     % Compute interpolated pixel values  
  174.     N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...  
  175.         w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);  
  176.     D = N >= d_C;   
  177.   end    
  178.   % Update the result matrix.  
  179.   v = 2^(i-1);  
  180.   result = result + v*D;  
  181. end  
  182.   
  183. %Apply mapping if it is defined  
  184. if isstruct(mapping)  
  185.     bins = mapping.num;  
  186.     for i = 1:size(result,1)  
  187.         for j = 1:size(result,2)  
  188.             result(i,j) = mapping.table(result(i,j)+1);  
  189.         end  
  190.     end  
  191. end  
  192.   
  193. if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh'))  
  194.     % Return with LBP histogram if mode equals 'hist'.  
  195.     result=hist(result(:),0:(bins-1));  
  196.     if (strcmp(mode,'nh'))  
  197.         result=result/sum(result);  
  198.     end  
  199. else  
  200.     %Otherwise return a matrix of unsigned integers  
  201.     if ((bins-1)<=intmax('uint8'))  
  202.         result=uint8(result);  
  203.     elseif ((bins-1)<=intmax('uint16'))  
  204.         result=uint16(result);  
  205.     else  
  206.         result=uint32(result);  
  207.     end  
  208. end  
  209.   
  210. end  

[plain] view plaincopy
  1. %GETMAPPING returns a structure containing a mapping table for LBP codes.  
  2. %  MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a structure containing a mapping table for  
  3. %  LBP codes in a neighbourhood of SAMPLES sampling  points. Possible values for MAPPINGTYPE are  
  4. %       'u2'   for uniform LBP  
  5. %       'ri'   for rotation-invariant LBP  
  6. %       'riu2' for uniform rotation-invariant LBP.  
  7. %  
  8. %  Example:  
  9. %       I=imread('rice.tif');  
  10. %       MAPPING=getmapping(16,'riu2');  
  11. %       LBPHIST=lbp(I,2,16,MAPPING,'hist');  
  12. %  Now LBPHIST contains a rotation-invariant uniform LBP  histogram in a (16,2) neighbourhood.  
  13. %  
  14.   
  15. function mapping = getmapping(samples,mappingtype)  
  16. % Version 1.0 beta  
  17. % Authors: Marko Heikkil?and Timo Ahonen  
  18. % Editor: LinJianmin - Huaqiao University  
  19. % Email:    [email protected]  
  20.   
  21. table = 0:2^samples-1;  
  22. newMax  = 0; %number of patterns in the resulting LBP code  
  23. index   = 0;  
  24.   
  25. if strcmp(mappingtype,'u2') %Uniform 2  
  26.   newMax = samples*(samples-1) + 3;   
  27.     
  28.   for i = 0:2^samples-1  
  29.     j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left  
  30.     numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and 0->1 transitions in binary string   
  31.                                                %x is equal to the number of 1-bits in XOR(x,Rotate left(x))   
  32.     if numt <= 2  
  33.       table(i+1) = index;  
  34.       index = index + 1;  
  35.     else  
  36.       table(i+1) = newMax - 1;  
  37.     end  
  38.   end  
  39. end  
  40.   
  41. if strcmp(mappingtype,'ri') %Rotation invariant  
  42.   tmpMap = zeros(2^samples,1) - 1;  
  43.   for i = 0:2^samples-1  
  44.     rm = i;  
  45.     r  = i;  
  46.     for j = 1:samples-1  
  47.       r = bitset(bitshift(r,1,samples),1,bitget(r,samples)); %rotate  
  48.                                                              %left  
  49.       if r < rm  
  50.         rm = r;  
  51.       end  
  52.     end  
  53.     if tmpMap(rm+1) < 0  
  54.       tmpMap(rm+1) = newMax;  
  55.       newMax = newMax + 1;  
  56.     end  
  57.     table(i+1) = tmpMap(rm+1);  
  58.   end  
  59. end  
  60.   
  61. if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant  
  62.   newMax = samples + 2;  
  63.   for i = 0:2^samples - 1  
  64.     j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left  
  65.     numt = sum(bitget(bitxor(i,j),1:samples));  
  66.     if numt <= 2  
  67.       table(i+1) = sum(bitget(i,1:samples));  
  68.     else  
  69.       table(i+1) = samples+1;  
  70.     end  
  71.   end  
  72. end  
  73.   
  74. mapping.table=table;  
  75. mapping.samples=samples;  
  76. mapping.num=newMax;  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章