对于任意一幅512X512图像(彩色图像),编程分别实现圆形LBP、旋转不变模式和等价模式的LBP特征提取,并把提取出来的LBP特征进行可视化处理。 编程语言:C++或者MATLAB
时间: 2024-05-29 11:10:09 浏览: 119
MATLAB代码实现:
% 读取图像
img = imread('lena.jpg');
% 转换为灰度图像
img = rgb2gray(img);
% 圆形LBP特征
radius = 1; % 半径为1
neighbours = 8; % 8个邻居点
lbp = lbp_circle(img, radius, neighbours); % 调用lbp_circle函数
subplot(2,2,1);
imshow(lbp);
title('Circle LBP');
% 旋转不变模式LBP特征
mapping = getmapping(neighbours, 'riu2'); % 获取旋转不变模式
lbp = lbp_custom(img, mapping); % 调用lbp_custom函数
subplot(2,2,2);
imshow(lbp);
title('Rotation Invariant LBP');
% 等价模式LBP特征
mapping = getmapping(neighbours, 'u2'); % 获取等价模式
lbp = lbp_custom(img, mapping); % 调用lbp_custom函数
subplot(2,2,3);
imshow(lbp);
title('Uniform LBP');
% 自定义LBP函数
function lbp = lbp_custom(img, mapping)
[rows, cols] = size(img);
lbp = zeros(rows, cols);
for i = 2 : rows - 1
for j = 2 : cols - 1
center = img(i, j);
code = 0;
for k = 1 : length(mapping.table)
neighbour = img(i + mapping.x(k), j + mapping.y(k));
if neighbour >= center
code = code + mapping.table(k);
end
end
lbp(i, j) = code;
end
end
lbp = uint8(lbp);
end
% 圆形LBP函数
function lbp = lbp_circle(img, radius, neighbours)
[rows, cols] = size(img);
img = padarray(img, [radius, radius], 'replicate'); % 边界填充
lbp = zeros(rows, cols);
for i = radius + 1 : rows + radius
for j = radius + 1 : cols + radius
center = img(i, j);
code = 0;
for k = 1 : neighbours
x = radius * cos(2 * pi * (k - 1) / neighbours);
y = radius * sin(2 * pi * (k - 1) / neighbours);
neighbour = interp2(double(img), j + x, i + y, 'linear');
if neighbour >= center
code = code + 2^(k - 1);
end
end
lbp(i - radius, j - radius) = code;
end
end
lbp = uint8(lbp);
end
阅读全文