LBPV.m
上传用户:kandtrade
上传日期:2009-06-26
资源大小:12k
文件大小:4k
源码类别:

图形图象

开发平台:

Matlab

  1. % LBPV returns LBPV histogram of an image.
  2. %  J = LBPV(I,R,N,MAPPING) returns the local binary pattern variance
  3. %  histogram of an intensity image I. The LBP codes are computed using P sampling 
  4. %  points on a circle of radius R and using mapping table defined by MAPPING. 
  5. %  The VAR values are computed for the P sampling points. Instead of
  6. %  computing the joint histogram of LBP and VAR globally, the LBPV computes the 
  7. %  VAR from a local region and accumulates it into the LBP bin. This can be regarded 
  8. %  as the integral projection along the VAR coordinate. 
  9. %  Examples
  10. %  --------
  11. %       I=imread('rice.png');
  12. %       mapping=getmapping(8,'u2'); 
  13. %       H1=LBPV(I,1,8,mapping); %LBPV histogram in (8,1) neighborhood using uniform patterns
  14. function result = LBPV(I,R,P,MAPPING)
  15. % Version 1.0
  16. % Authors: Zhenhua Guo, Lei Zhang and David Zhang
  17. % Copyright @ Biometrics Research Centre, the Hong Kong Polytechnic University
  18. if nargin<1
  19.     disp('No input image')
  20.     return
  21. end
  22. if nargin<2
  23.     R = 1;
  24. end
  25. if nargin<3
  26.     P = 8;
  27. end   
  28. if nargin<4
  29.     MAPPING = getmapping(P,'riu2'); 
  30. end
  31. % Get LBP value for each pixel of the input image
  32. LBPMap = lbp_new(I,R,P,MAPPING,'x');  
  33. % Get VAR value for each pixel of the input image
  34. spoints=zeros(P,2);
  35. % Angle step.
  36. a = 2*pi/P;
  37. for i = 1:P
  38.     spoints(i,1) = -R*sin((i-1)*a);
  39.     spoints(i,2) = R*cos((i-1)*a);
  40. end
  41. [ysize xsize] = size(I);
  42. miny=min(spoints(:,1));
  43. maxy=max(spoints(:,1));
  44. minx=min(spoints(:,2));
  45. maxx=max(spoints(:,2));
  46. % Block size, each VAR value is computed within a block of size bsizey*bsizex
  47. bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
  48. bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;
  49. % Coordinates of origin (0,0) in the block
  50. origy=1-floor(min(miny,0));
  51. origx=1-floor(min(minx,0));
  52. % Minimum allowed size for the input image depends
  53. % on the radius of the used VAR operator.
  54. if(xsize < bsizex || ysize < bsizey)
  55.   error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');
  56. end
  57. % Calculate dx and dy;
  58. dx = xsize - bsizex;
  59. dy = ysize - bsizey;
  60. % convert the input image to "double" type.
  61. d_image = double(I);
  62. % Reorganize the image to compute VAR value
  63. for i = 1:P
  64.   y = spoints(i,1)+origy;
  65.   x = spoints(i,2)+origx;
  66.   % Calculate floors, ceils and rounds for the x and y.
  67.   fy = floor(y); cy = ceil(y); ry = round(y);
  68.   fx = floor(x); cx = ceil(x); rx = round(x);
  69.   % Check if interpolation is needed.
  70.   if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
  71.     % Interpolation is not needed, use original datatypes
  72.     T = d_image(ry:ry+dy,rx:rx+dx);
  73.     
  74.     % convert the matrix to a column vector
  75.     NArray(:,i) = reshape(T,prod(size(T)),1); 
  76.   else
  77.     % Interpolation needed, use double type images 
  78.     ty = y - fy;
  79.     tx = x - fx;
  80.     % Calculate the interpolation weights.
  81.     w1 = (1 - tx) * (1 - ty);
  82.     w2 =      tx  * (1 - ty);
  83.     w3 = (1 - tx) *      ty ;
  84.     w4 =      tx  *      ty ;
  85.     % Compute interpolated pixel values
  86.     T = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...
  87.         w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
  88.     
  89.     % convert the matrix to a column vector
  90.     NArray(:,i) = reshape(T,prod(size(T)),1);
  91.   end  
  92. end
  93. % after get all neighborhood for each pixel, compute variance for the image
  94. VAR = var(NArray,1,2);
  95. VAR = reshape(VAR,size(T));
  96. MapNum = max(MAPPING(:));
  97. % Initialize the result matrix with zeros.
  98. result=zeros(1,MapNum+1);
  99. for k=0:MapNum;
  100.     index = find(LBPMap==k);
  101.     result(k+1) = sum(VAR(index));
  102. end