Matlab的旋转不变lbp特征提取代码
时间: 2023-07-02 21:10:38 浏览: 115
图像lbp特征提取的MATLAB实现源码
以下是MATLAB中旋转不变LBP特征提取的代码:
```matlab
function lbp_feat = rotation_invariant_lbp(img, radius, neighbors)
% img: 输入的图像
% radius: LBP算子半径
% neighbors: LBP算子采样点数
% lbp_feat: LBP特征
if nargin < 3
neighbors = 8;
end
if nargin < 2
radius = 1;
end
% 获取图像尺寸
[rows, cols] = size(img);
% 初始化LBP特征图
lbp_feat_map = zeros(rows, cols);
% 计算LBP特征图
for i = 1 : rows
for j = 1 : cols
lbp_feat_map(i,j) = get_lbp_pixel(img, i, j, radius, neighbors);
end
end
% 计算旋转不变LBP特征
lbp_feat = zeros(1, 2^neighbors);
for i = 0 : neighbors-1
% 将特征图沿着采样点方向旋转
lbp_feat_map_rotated = imrotate(lbp_feat_map, i*360/neighbors, 'nearest', 'crop');
% 计算LBP直方图
lbp_hist = histcounts(lbp_feat_map_rotated(:), 0:2^neighbors);
% 更新LBP特征
lbp_feat = lbp_feat + lbp_hist;
end
% 归一化LBP特征
lbp_feat = lbp_feat / sum(lbp_feat);
end
function lbp_pixel = get_lbp_pixel(img, row, col, radius, neighbors)
% img: 输入的图像
% row, col: 中心像素坐标
% radius: LBP算子半径
% neighbors: LBP算子采样点数
% lbp_pixel: LBP像素值
% 获取图像尺寸
[rows, cols] = size(img);
% 初始化采样点坐标
sample_points = zeros(neighbors, 2);
% 计算采样点坐标
for i = 1 : neighbors
theta = 2*pi*(i-1)/neighbors;
sample_points(i,1) = row + radius*cos(theta);
sample_points(i,2) = col - radius*sin(theta);
end
% 处理采样点坐标越界的情况
sample_points(sample_points(:,1)<1 | sample_points(:,1)>rows | sample_points(:,2)<1 | sample_points(:,2)>cols, :) = [];
% 计算LBP像素值
lbp_pixel = 0;
for i = 1 : size(sample_points, 1)
if img(round(sample_points(i,1)), round(sample_points(i,2))) >= img(row, col)
lbp_pixel = lbp_pixel + 2^(i-1);
end
end
end
```
使用方法:
```matlab
% 读取图像
img = imread('lena.png');
% 计算旋转不变LBP特征
lbp_feat = rotation_invariant_lbp(img, 1, 8);
```
阅读全文