ORB利用高斯金字塔matlab代码
时间: 2023-12-28 12:03:29 浏览: 78
高斯金字塔的matlab代码
4星 · 用户满意度95%
以下是利用高斯金字塔实现ORB特征点提取的 Matlab 代码:
```matlab
function [keypoints, descriptors] = ORB(image)
% 将图像灰度化
gray_image = rgb2gray(image);
% 构建高斯金字塔
num_octaves = 4;
num_levels = 5;
k = sqrt(2);
sigma = 1.6;
[gauss_pyramid, dog_pyramid] = create_pyramid(gray_image, num_octaves, num_levels, k, sigma);
% 计算特征点
threshold = 0.01;
radius = 3;
[keypoints, responses] = detect_features(dog_pyramid, threshold, radius);
% 计算描述子
patch_size = 31;
descriptors = compute_descriptors(gauss_pyramid, keypoints, patch_size);
end
% 构建高斯金字塔和差分金字塔
function [gauss_pyramid, dog_pyramid] = create_pyramid(image, num_octaves, num_levels, k, sigma)
% 将图像转换成双精度类型
image = im2double(image);
% 构建高斯金字塔
gauss_pyramid = cell(num_octaves, num_levels);
% 不同octave之间的sigma值是一样的,而在同一个octave中,sigma的值是不断增加的
for i = 1:num_octaves
for j = 1:num_levels
if i == 1 && j == 1
% 第一个金字塔的第一层就是原始图像
gauss_pyramid{i, j} = image;
elseif j == 1
% 每个octave的第一层使用前一octave的最后一层下采样得到
gauss_pyramid{i, j} = imresize(gauss_pyramid{i-1, end}, 0.5, 'bilinear');
else
% 计算当前层的sigma值
sigma_curr = sigma * k^((j-2)+(i-1)*num_levels);
% 使用当前层的sigma值构建高斯滤波器
h = fspecial('gaussian', ceil(sigma_curr*3)*2+1, sigma_curr);
% 对上一层进行高斯模糊操作
gauss_pyramid{i, j} = imfilter(gauss_pyramid{i, j-1}, h, 'replicate', 'same');
end
end
end
% 构建差分金字塔
dog_pyramid = cell(num_octaves, num_levels-1);
for i = 1:num_octaves
for j = 1:num_levels-1
% 相邻两层相减得到差分金字塔
dog_pyramid{i, j} = gauss_pyramid{i, j+1} - gauss_pyramid{i, j};
end
end
end
% 计算特征点
function [keypoints, responses] = detect_features(dog_pyramid, threshold, radius)
[num_octaves, num_levels] = size(dog_pyramid);
keypoints = cell(num_octaves, num_levels-3);
responses = cell(num_octaves, num_levels-3);
for i = 1:num_octaves
for j = 2:num_levels-2
% 在当前层的3x3邻域内找到极值点
current_layer = dog_pyramid{i, j};
above_layer = dog_pyramid{i, j-1};
below_layer = dog_pyramid{i, j+1};
extrema = detect_extrema(current_layer, above_layer, below_layer, threshold, radius);
keypoints{i, j-1} = extrema;
% 计算响应值
[r, c, num_extrema] = size(extrema);
current_responses = zeros(num_extrema, 1);
for k = 1:num_extrema
current_responses(k) = compute_response(current_layer, extrema(k,1), extrema(k,2));
end
responses{i, j-1} = current_responses;
end
end
% 将所有octave中的特征点合并到一起
keypoints = cell2mat(keypoints);
responses = cell2mat(responses);
end
% 在当前层的3x3邻域内找到极值点
function extrema = detect_extrema(current_layer, above_layer, below_layer, threshold, radius)
[h, w] = size(current_layer);
extrema = [];
for i = radius+1:h-radius
for j = radius+1:w-radius
current_pixel = current_layer(i, j);
% 判断当前像素是否是3x3邻域内的极值点
if abs(current_pixel) > threshold && ...
((current_pixel > 0 && current_pixel == max(max(current_layer(i-radius:i+radius, j-radius:j+radius)))) || ...
(current_pixel < 0 && current_pixel == min(min(current_layer(i-radius:i+radius, j-radius:j+radius))))) && ...
((current_pixel > 0 && current_pixel == max(max(above_layer(i-radius:i+radius, j-radius:j+radius)))) || ...
(current_pixel < 0 && current_pixel == min(min(above_layer(i-radius:i+radius, j-radius:j+radius))))) && ...
((current_pixel > 0 && current_pixel == max(max(below_layer(i-radius:i+radius, j-radius:j+radius)))) || ...
(current_pixel < 0 && current_pixel == min(min(below_layer(i-radius:i+radius, j-radius:j+radius)))))
extrema = [extrema; i, j];
end
end
end
end
% 计算响应值
function response = compute_response(image, row, col)
% 计算像素点周围2x2邻域内的梯度值
dx = (image(row, col+1) - image(row, col-1)) / 2;
dy = (image(row+1, col) - image(row-1, col)) / 2;
% 计算Hessian矩阵
dxx = image(row, col+1) - 2*image(row, col) + image(row, col-1);
dyy = image(row+1, col) - 2*image(row, col) + image(row-1, col);
dxy = (image(row+1, col+1) - image(row+1, col-1) - image(row-1, col+1) + image(row-1, col-1)) / 4;
% 计算响应值
trace = dxx + dyy;
det = dxx * dyy - dxy * dxy;
response = det - 0.04 * trace^2;
end
% 计算描述子
function descriptors = compute_descriptors(gauss_pyramid, keypoints, patch_size)
[num_keypoints, ~] = size(keypoints);
descriptors = zeros(num_keypoints, 256);
for i = 1:num_keypoints
octave = keypoints(i, 3);
level = keypoints(i, 4);
scale = keypoints(i, 5);
row = keypoints(i, 1);
col = keypoints(i, 2);
% 计算旋转角度
angle = compute_orientation(gauss_pyramid{octave, level}, row, col, scale);
% 构建描述子
patch = get_patch(gauss_pyramid{octave, level}, row, col, scale, angle, patch_size);
descriptors(i, :) = reshape(patch, 1, []);
end
end
% 计算旋转角度
function angle = compute_orientation(image, row, col, scale)
% 计算像素点周围16x16邻域内的梯度值和方向
dx = zeros(16, 16);
dy = zeros(16, 16);
orientation = zeros(16, 16);
for i = -8:7
for j = -8:7
dx(i+9, j+9) = (image(row+i, col+j+1) - image(row+i, col+j-1)) / 2;
dy(i+9, j+9) = (image(row+i+1, col+j) - image(row+i-1, col+j)) / 2;
orientation(i+9, j+9) = atan2(dy(i+9, j+9), dx(i+9, j+9));
end
end
% 将方向分成36个bin,计算每个bin中的梯度值之和
hist = zeros(1, 36);
for i = 1:4
for j = 1:4
hist((i-1)*9+j) = sum(sum((orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) >= (j-1)*pi/18) & ...
(orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) < j*pi/18))) * ...
sum(sum((orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) >= (j-1)*pi/18) & ...
(orientation((i-1)*4+1:i*4, (j-1)*4+1:j*4) < j*pi/18)) .* ...
sqrt(dx((i-1)*4+1:i*4, (j-1)*4+1:j*4).^2 + dy((i-1)*4+1:i*4, (j-1)*4+1:j*4).^2));
end
end
% 找到最大值对应的bin,作为旋转角度
[~, max_bin] = max(hist);
angle = (max_bin-1) * pi/18;
end
% 获取patch
function patch = get_patch(image, row, col, scale, angle, patch_size)
patch = zeros(patch_size, patch_size);
for i = -patch_size/2+1:patch_size/2
for j = -patch_size/2+1:patch_size/2
x = row + i * scale * cos(angle) + j * scale * sin(angle);
y = col - i * scale * sin(angle) + j * scale * cos(angle);
patch(i+patch_size/2+1, j+patch_size/2+1) = interpolate_pixel(image, x, y);
end
end
% 归一化
patch = (patch - mean(patch(:))) / std(patch(:));
end
% 插值获取像素值
function pixel = interpolate_pixel(image, row, col)
if row < 1 || row > size(image, 1) || col < 1 || col > size(image, 2)
pixel = 0;
return
end
row1 = floor(row);
row2 = ceil(row);
col1 = floor(col);
col2 = ceil(col);
if row1 == row2 && col1 == col2
pixel = image(row1, col1);
return
end
if row1 == row2
pixel = (col2-col)*image(row1, col1) + (col-col1)*image(row1, col2);
elseif col1 == col2
pixel = (row2-row)*image(row1, col1) + (row-row1)*image(row2, col1);
else
pixel = (row2-row)*(col2-col)*image(row1, col1) + (row-row1)*(col2-col)*image(row2, col1) + ...
(row2-row)*(col-col1)*image(row1, col2) + (row-row1)*(col-col1)*image(row2, col2);
end
end
```
阅读全文