lbp特征的matlab实现
生活随笔
收集整理的這篇文章主要介紹了
lbp特征的matlab实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
matlab自帶函數extractLBPFeatures可以很好地提取lbp特征,但是不好進行改進,因此在網上找了人家用maltab實現的lbp特征提取,但是性能比自帶函數稍差,代碼主要來自
http://www.cse.oulu.fi/wsgi/MVG/Downloads/LBPMatlab
針對lbp特征提取部分,有稍微進行修改,代碼如下:
testmylbp.m
mylbp.m(可以針對子塊提取lbp特征,需要調用lbp函數)
function H2=mylbp(img,row,col,blocksizerow,blocksizecol,radius,neighbors,mapping,mode) % [row,col,k]=size(img); % if k>1 % img=rgb2gray(img); % end new_row = floor(row/blocksizerow) * blocksizerow;%ceil是向上取整,floor向下 new_col = floor(col/blocksizecol) * blocksizecol; new_img = imresize(img, [new_row new_col], 'bilinear');%以雙線性插值來縮放[y_row y_col dim] = size(new_img); row_blk_num = y_row/blocksizerow; % 3 col_blk_num = y_col/blocksizecol; % 6blocks = 1; for i = 1:row_blk_numfor j = 1:col_blk_num % disp(blocks);block = new_img((i - 1) * blocksizerow + 1 : i * blocksizerow, (j - 1) * blocksizecol + 1 : j * blocksizecol, :);H1(blocks,:)=lbp(block,radius,neighbors,mapping,mode);% imshow(block);%imwrite(block, ['./' num2str(blocks) '.jpg']);blocks = blocks + 1;end end H2=reshape(H1,1,[]);%變成行向量lbp.m 代碼如下
%LBP returns the local binary pattern image or LBP histogram of an image. % J = LBP(I,R,N,MAPPING,MODE) returns either a local binary pattern % coded image or the local binary pattern histogram of an intensity % image I. The LBP codes are computed using N sampling points on a % circle of radius R and using mapping table defined by MAPPING. % See the getmapping function for different mappings and use 0 for % no mapping. Possible values for MODE are % 'h' or 'hist' to get a histogram of LBP codes % 'nh' to get a normalized histogram % Otherwise an LBP code image is returned. % % J = LBP(I) returns the original (basic) LBP histogram of image I % % J = LBP(I,SP,MAPPING,MODE) computes the LBP codes using n sampling % points defined in (n * 2) matrix SP. The sampling points should be % defined around the origin (coordinates (0,0)). % % Examples % -------- % I=imread('rice.png'); % mapping=getmapping(8,'u2'); % H1=LBP(I,1,8,mapping,'h'); %LBP histogram in (8,1) neighborhood % %using uniform patterns % subplot(2,1,1),stem(H1); % % H2=LBP(I); % subplot(2,1,2),stem(H2); % % SP=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1]; % I2=LBP(I,SP,0,'i'); %LBP code image using sampling points in SP % %and no mapping. Now H2 is equal to histogram % %of I2.function result = lbp(varargin) % image,radius,neighbors,mapping,mode) % Version 0.3.2 % Authors: Marko Heikkil錕? and Timo Ahonen% Changelog % Version 0.3.2: A bug fix to enable using mappings together with a % predefined spoints array % Version 0.3.1: Changed MAPPING input to be a struct containing the mapping % table and the number of bins to make the function run faster with high number % of sampling points. Lauge Sorensen is acknowledged for spotting this problem.% Check number of input arguments. error(nargchk(1,5,nargin));image=varargin{1}; d_image=double(image);if nargin==1spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];neighbors=8;mapping=0;mode='h'; endif (nargin == 2) && (length(varargin{2}) == 1)error('Input arguments'); endif (nargin > 2) && (length(varargin{2}) == 1)radius=varargin{2};neighbors=varargin{3};spoints=zeros(neighbors,2);% Angle step. %由采樣點個數計算出各個采樣點的坐標,a = 2*pi/neighbors;for i = 1:neighborsspoints(i,1) = -radius*sin((i-1)*a);spoints(i,2) = radius*cos((i-1)*a);endif(nargin >= 4)mapping=varargin{4};if(isstruct(mapping) && mapping.samples ~= neighbors)error('Incompatible mapping');endelsemapping=0;endif(nargin >= 5)mode=varargin{5};elsemode='h';end endif (nargin > 1) && (length(varargin{2}) > 1)spoints=varargin{2};neighbors=size(spoints,1);if(nargin >= 3)mapping=varargin{3};if(isstruct(mapping) && mapping.samples ~= neighbors)error('Incompatible mapping');endelsemapping=0;endif(nargin >= 4)mode=varargin{4};elsemode='h';end end% Determine the dimensions of the input image. [ysize xsize] = size(image);miny=min(spoints(:,1)); maxy=max(spoints(:,1)); minx=min(spoints(:,2)); maxx=max(spoints(:,2));% Block size, each LBP code is computed within a block of size bsizey*bsizex bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1; bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;% Coordinates of origin (0,0) in the block origy=1-floor(min(miny,0)); origx=1-floor(min(minx,0));% Minimum allowed size for the input image depends % on the radius of the used LBP operator. if(xsize < bsizex || ysize < bsizey)error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)'); end% Calculate dx and dy; dx = xsize - bsizex; dy = ysize - bsizey;% Fill the center pixel matrix C. C = image(origy:origy+dy,origx:origx+dx); d_C = double(C);bins = 2^neighbors;% Initialize the result matrix with zeros. result=zeros(dy+1,dx+1);%Compute the LBP code imagefor i = 1:neighborsy = spoints(i,1)+origy;x = spoints(i,2)+origx;% Calculate floors, ceils and rounds for the x and y.fy = floor(y); cy = ceil(y); ry = round(y);fx = floor(x); cx = ceil(x); rx = round(x);% Check if interpolation is needed.if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)% Interpolation is not needed, use original datatypesN = image(ry:ry+dy,rx:rx+dx);D = N >= C; else% Interpolation needed, use double type images ty = y - fy;tx = x - fx;% Calculate the interpolation weights.w1 = (1 - tx) * (1 - ty);w2 = tx * (1 - ty);w3 = (1 - tx) * ty ;w4 = tx * ty ;% Compute interpolated pixel valuesN = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);D = N >= d_C; end % Update the result matrix.v = 2^(i-1);result = result + v*D; end%Apply mapping if it is defined if isstruct(mapping)bins = mapping.num;for i = 1:size(result,1)for j = 1:size(result,2)result(i,j) = mapping.table(result(i,j)+1);endend endif (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh')||strcmp(mode,'L2'))% Return with LBP histogram if mode equals 'hist'.result=hist(result(:),0:(bins-1));if (strcmp(mode,'nh'))result=result/sum(result);elseif(strcmp(mode,'L2'))result=result/norm(result);end else%Otherwise return a matrix of unsigned integersif ((bins-1)<=intmax('uint8'))result=uint8(result);elseif ((bins-1)<=intmax('uint16'))result=uint16(result);elseresult=uint32(result);end endendgetmapping.m 代碼如下
%GETMAPPING returns a structure containing a mapping table for LBP codes. % MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a % structure containing a mapping table for % LBP codes in a neighbourhood of SAMPLES sampling % points. Possible values for MAPPINGTYPE are % 'u2' for uniform LBP % 'ri' for rotation-invariant LBP % 'riu2' for uniform rotation-invariant LBP. % % Example: % I=imread('rice.tif'); % MAPPING=getmapping(16,'riu2'); % LBPHIST=lbp(I,2,16,MAPPING,'hist'); % Now LBPHIST contains a rotation-invariant uniform LBP % histogram in a (16,2) neighbourhood. %function mapping = getmapping(samples,mappingtype) % Version 0.1.1 % Authors: Marko Heikkil錕? and Timo Ahonen% Changelog % 0.1.1 Changed output to be a structure % Fixed a bug causing out of memory errors when generating rotation % invariant mappings with high number of sampling points. % Lauge Sorensen is acknowledged for spotting this problem.samples2='uint8'; table = 0:2^samples-1; newMax = 0; %number of patterns in the resulting LBP code index = 0;if strcmp(mappingtype,'u2') %Uniform 2newMax = samples*(samples-1) + 3; for i = 0:2^samples-1j = bitset(bitshift(i,1,samples2),1,bitget(i,samples)); %rotate leftnumt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and%0->1 transitions%in binary string %x is equal to the%number of 1-bits in%XOR(x,Rotate left(x)) if numt <= 2table(i+1) = index;index = index + 1;elsetable(i+1) = newMax - 1;endend endif strcmp(mappingtype,'ri') %Rotation invarianttmpMap = zeros(2^samples,1) - 1;for i = 0:2^samples-1rm = i;r = i;for j = 1:samples-1r = bitset(bitshift(r,1,samples),1,bitget(r,samples)); %rotate%leftif r < rmrm = r;endendif tmpMap(rm+1) < 0tmpMap(rm+1) = newMax;newMax = newMax + 1;endtable(i+1) = tmpMap(rm+1);end endif strcmp(mappingtype,'riu2') %Uniform & Rotation invariantnewMax = samples + 2;for i = 0:2^samples - 1j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate leftnumt = sum(bitget(bitxor(i,j),1:samples));if numt <= 2table(i+1) = sum(bitget(i,1:samples));elsetable(i+1) = samples+1;endend endmapping.table=table; mapping.samples=samples; mapping.num=newMax;把這幾個函數放在一起,就可以運行tesmylbp文件了,testmylbp顧名思義就是測試muylbp函數能不能使用,mylbp其實就是加了一個子塊的功能而已,lbp.m可以提取一整張圖片的lbp特征,但是一般情況下是對圖片劃分子塊,然后整個串聯起來,所以只是把這些整合在一起罷了。
總結
以上是生活随笔為你收集整理的lbp特征的matlab实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实现类似于visio_类似v
- 下一篇: 【MIKE21】批处理依次打开模型文件运