版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38784454/article/details/81455235
目前对CS-LBP 的理解是CS-LBP只能描述电池片裂纹的梯度特征:CS-LBP值的大小代表的是方向,每一个值出现的频率代表的大小。CS-LBP作为一种特征描述的方法,所提取的特征向量维数小,并且对锑度变化响应明显,这也就是为什莫选用它作为裂纹特征提取主要原因。而缺点只能描述梯度特征,一旦噪声干扰与裂纹有相似的锑度变化,CS-LBP就不能很好的分辨出到底是背景还是裂纹。这是目前针对裂纹这缺陷效果低的主要原因。现在对研究的最新理解是,创新之处一定是针对所要解决的问题。所要解决的问题也要明确,像CS-LBP对于锑度变化相似的裂纹和背景区分度就不高,这就是要解决的核心问题,并非是非均匀纹理背景下的缺陷分类,非均匀纹理背景还是太大。
解决方案目前想到了两种,一种是用采用减少类间距,增加类间间距的方法。另一种是提取裂纹的专有特征,即有针对性的提取特征。
CS-LBP 的简单理解及实现:CS-LBP利用关于中心像素对称位置的像素大小关系进行二进制编码,再将这个二进制数映射成十进制数,属于将像素信息映射到了空间域。这种方法利用了图像的中心对称差分信息,也就是梯度信息,对梯度响应明显。
源码是我在LBP基础上改的(matlab写的):CSDN为啥插入的图片不能显示,真他妈气人。
% % function result = cslbp(image,radius,neighbors)
% clc
% clear
% close all
% image = imread('E:\裂纹\25乘25测试\194.jpg');
% figure;
% imshow(image)
% image = rgb2gray(image);
% image = mat2gray(image);
% radius = 1;
% neighbors = 8;
% d_image = double(image);
%
% % [m,n] = size(I);
% % dst = zeros(m-2*radius,n-2*radius);
% % for i = radius+1:(m-radius)
% % for j = radius+1:(n-radius)
% % lbpcircle = [];
% % center = I(i,j);
% % lbpvalue = 0;
% % for k = 1:neighbors
% % x = i+radius*cos(2*pi*k/neighbors);
% % y = j-radius*sin(2*pi*k/neighbors);
% % x1 = floor(x);
% % y1 = floor(y);
% % x2 = ceil(x);
% % y2 = ceil(y);
% % rx = round(x);
% % ry = round(y);
% % if abs(x-rx)<1e-6 && abs(y-ry)<1e-6
% % lbpcircle(k) = I(rx,ry);
% % else
% % tx = x-x1;
% % ty = y-y1;
% % w1 = roundn((1-tx)*(1-ty),-6);
% % w2 = roundn(tx*(1-ty),-6);
% % w3 = roundn((1-tx)*ty,-6);
% % w4 = roundn(tx*ty,-6);
% % lbpcircle(k) = I(x1,y1)*w1 + I(x1,y2)*w2 + I(x2,y1)*w3 + I(x2,y2)*w4;
% % end
% % end
% % lbpvalue = (lbpcircle(1)>=lbpcircle(5))*2^0 + (lbpcircle(2)>=lbpcircle(6))*2^1 +(lbpcircle(3)>=lbpcircle(7))*2^2 +...
% % (lbpcircle(4)>=lbpcircle(8))*2^3;
% % I(i,j) =lbpvalue;
% % end
% % end
% % I
%
% spoints=zeros(neighbors,2);
%
% % Angle step.
% a = 2*pi/neighbors;
%
% for i = 1:neighbors
% spoints(i,1) = -radius*sin((i-1)*a);
% spoints(i,2) = radius*cos((i-1)*a);
% end
% [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; %3
% bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1; %3
%
% % Coordinates of origin (0,0) in the block
% origy=1-floor(min(miny,0)); %2
% origx=1-floor(min(minx,0)); %2
% dx = xsize - bsizex; %22
% dy = ysize - bsizey; %22
%
% % Fill the center pixel matrix C.
% C = image(origy+dy,origx+dx); %2:24,2:24
% d_C = double(C);
%
% bins = 2^neighbors; %256
%
% % Initialize the result matrix with zeros.
% result=zeros(dy+1,dx+1); %23*23
%
% %Compute the LBP code image
%
%
% for i = 1:neighbors
% y = 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 datatypes
% N = image(ry,rx);
% D = N >= C;
% else
% % Interpolation needed, use double type images
% ty = y - fy;
% tx = x - fx;
%
% % Calculate the interpolation weights.
% w1 = roundn((1 - tx) * (1 - ty),-6);
% w2 = roundn(tx * (1 - ty),-6);
% w3 = roundn((1 - tx) * ty,-6) ;
% % w4 = roundn(tx * ty,-6) ;
% w4 = roundn(1 - w1 - w2 - w3, -6);
%
% % Compute interpolated pixel values
% N = w1*d_image(fy,fx) + w2*d_image(fy,cx) + ...
% w3*d_image(cy,fx) + w4*d_image(cy,cx);
% N = roundn(N,-4);
% D = N >= d_C;
% end
% v = 2^(i-1);
% result = result + v*D;
% end
% end
% end
%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,lbpimage] = cslbp(varargin) % image,radius,neighbors,mapping,mode)
% Version 0.3.3
% 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==1
spoints=[-1 -1; -1 0; -1 1; 0 -1; 0 1; 1 -1; 1 0; 1 1];
neighbors=8;
mapping=0;
mode='h';
end
if (nargin == 2) && (length(varargin{2}) == 1)
error('Input arguments');
end
if (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:neighbors
spoints(i,1) = -radius*sin((i-1)*a);
spoints(i,2) = radius*cos((i-1)*a);
end
if(nargin >= 4)
mapping=varargin{4};
if(isstruct(mapping) && mapping.samples ~= neighbors)
error('Incompatible mapping');
end
else
mapping=0;
end
if(nargin >= 5)
mode=varargin{5};
else
mode='h';
end
end
if (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');
end
else
mapping=0;
end
if(nargin >= 4)
mode=varargin{4};
else
mode='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; %3
bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1; %3
% Coordinates of origin (0,0) in the block
origy=1-floor(min(miny,0)); %2
origx=1-floor(min(minx,0)); %2
% 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; %22
dy = ysize - bsizey; %22
% Fill the center pixel matrix C.
C = image(origy:origy+dy,origx:origx+dx); %2:24,2:24
d_C = double(C);
bins = 2^4; %256
% Initialize the result matrix with zeros.
result=zeros(dy+1,dx+1); %23*23
%Compute the LBP code image
for i = 1:neighbors
y = 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 datatypes
N(1:dy+1,1:dx+1,i) = image(ry:ry+dy,rx:rx+dx);
D = N(1:dy+1,1:dx+1,i) >= C;
else
% Interpolation needed, use double type images
ty = y - fy;
tx = x - fx;
% Calculate the interpolation weights.
w1 = roundn((1 - tx) * (1 - ty),-6);
w2 = roundn(tx * (1 - ty),-6);
w3 = roundn((1 - tx) * ty,-6) ;
% w4 = roundn(tx * ty,-6) ;
w4 = roundn(1 - w1 - w2 - w3, -6);
% Compute interpolated pixel values
N(1:dy+1,1:dx+1,i) = 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);
N(1:dy+1,1:dx+1,i) = roundn(N(1:dy+1,1:dx+1,i),-4);
% D = N >= d_C;
end
% Update the result matrix.
% v = 2^(i-1);
% result = result + v*D;
end
% result = result + ((N(1:dy+1,1:dx+1,1)+N(1:dy+1,1:dx+1,5))/2<d_C)*2^0+((N(1:dy+1,1:dx+1,2)+N(1:dy+1,1:dx+1,6))/2<d_C)*2^1+...
% ((N(1:dy+1,1:dx+1,3)+N(1:dy+1,1:dx+1,7))/2<d_C)*2^2+((N(1:dy+1,1:dx+1,4)+N(1:dy+1,1:dx+1,8))/2<d_C)*2^3;
% resultaverge = (N(1:dy+1,1:dx+1,1)+N(1:dy+1,1:dx+1,2)+N(1:dy+1,1:dx+1,3)+N(1:dy+1,1:dx+1,4)+N(1:dy+1,1:dx+1,5)+...
% N(1:dy+1,1:dx+1,6)+N(1:dy+1,1:dx+1,7)+N(1:dy+1,1:dx+1,8))/8;
% image_o = d_image(fy:fy+dy,fx:fx+dx);
% result = result + (resultaverge(1:dy+1,1:dx+1)>image_o(1:dy+1,1:dx+1))*2^0+(N(1:dy+1,1:dx+1,1)>N(1:dy+1,1:dx+1,5))*2^1+(N(1:dy+1,1:dx+1,2)>N(1:dy+1,1:dx+1,6))*2^2+...
% (N(1:dy+1,1:dx+1,3)>N(1:dy+1,1:dx+1,7))*2^3+(N(1:dy+1,1:dx+1,4)>N(1:dy+1,1:dx+1,8))*2^4;
result = result + (N(1:dy+1,1:dx+1,1)>=N(1:dy+1,1:dx+1,5))*2^0+(N(1:dy+1,1:dx+1,2)>=N(1:dy+1,1:dx+1,6))*2^1+...
(N(1:dy+1,1:dx+1,3)>=N(1:dy+1,1:dx+1,7))*2^2+(N(1:dy+1,1:dx+1,4)>=N(1:dy+1,1:dx+1,8))*2^3;
lbpimage = result; %126*126原图(128*128)
%Apply mapping if it is defined
if isstruct(mapping)
bins = mapping.num;
for i = 1:size(result,1) %23
for j = 1:size(result,2) %23
result(i,j) = mapping.table(result(i,j)+1); %mapping之后的结果
end
end
end
if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh'))
% Return with LBP histogram if mode equals 'hist'.
result=hist(result(:),0:(bins-1));
if (strcmp(mode,'nh'))
result=result/sum(result);
end
else
%Otherwise return a matrix of unsigned integers
if ((bins-1)<=intmax('uint8'))
result=uint8(result);
elseif ((bins-1)<=intmax('uint16'))
result=uint16(result);
else
result=uint32(result);
end
end
end
function x = roundn(x, n)
error(nargchk(2, 2, nargin, 'struct'))
validateattributes(x, {'single', 'double'}, {}, 'ROUNDN', 'X')
validateattributes(n, ...
{'numeric'}, {'scalar', 'real', 'integer'}, 'ROUNDN', 'N')
if n < 0
p = 10 ^ -n;
x = round(p * x) / p;
elseif n > 0
p = 10 ^ n;
x = p * round(x / p);
else
x = round(x);
end
end