旋转不变LBP特征-matlab
不断的旋转圆形邻域内的LBP特征,根据选择得到一系列的LBP特征值,从这些LBP特征值选择LBP特征值最小的作为中心像素点的LBP特征。
matlab源码
function imglbp = getRotationInvariantLBPFeature(img, radius, neighbors)
imgSize = size(img);
if numel(imgSize) > 2
imgG = rgb2gray(img);
else
imgG = img;
end
[rows, cols] = size(imgG);
rows=int16(rows);
cols=int16(cols);
imglbp = uint8(zeros(rows-2*radius, cols-2*radius));
for k=0:neighbors-1
% 计算采样点对于中心点坐标的偏移量rx,ry
rx = radius * cos(2.0 * pi * k / neighbors);
ry = -radius * sin(2.0 * pi * k / neighbors);
% 对采样点偏移量分别进行上下取整
x1 = floor(rx);
x2 = ceil(rx);
y1 = floor(ry);
y2 = ceil(ry);
% 将坐标偏移量映射到0-1之间
tx = rx - x1;
ty = ry - y1;
% 根据0-1之间的x,y的权重计算公式计算权重,权重与坐标具体位置无关,与坐标间的差值有关
w1 = (1-tx) * (1-ty);
w2 = tx * (1-ty);
w3 = (1-tx) * ty;
w4 = tx * ty;
for i=radius+1:rows-radius
for j=radius+1:cols-radius
center = imgG(i, j);
% 根据双线性插值公式计算第k个采样点的灰度值
neighbor = imgG(i+x1, j+y1)*w1 + imgG(i+x1, j+y2)*w2 + imgG(i+x2, j+y1)*w3 + imgG(i+x2, j+y2)*w4;
% LBP特征图像的每个邻居的LBP值累加,累加通过与操作完成,对应的LBP值通过移位取得
if neighbor > center
flag = 1;
else
flag = 0;
end
imglbp(i-radius, j-radius) = bitor(imglbp(i-radius, j-radius), bitshift(flag, neighbors-k-1));
end
end
end
for i=1:rows-2*radius
for j=1:cols-2*radius
currentValue = imglbp(i, j);
minValue = currentValue;
currentValue = dec2bin(currentValue);
% 循环右移
for k=1:neighbors
temp = circshift(currentValue, k);
temp = bin2dec(temp);
if temp < minValue
minValue = temp;
end
end
imglbp(i, j) = minValue;
end
end
end
效果图
原图
radius=3,neighbors=8