图像匹配是计算机视觉中的核心任务,用于在图像中定位已知模板或特征。本文将详细介绍三种重要的图像匹配技术:相关匹配、Hausdorff距离匹配和基于距离变换的Hausdorff距离匹配,并提供完整的MATLAB实现代码。
一、相关匹配(模板匹配)
原理
相关匹配通过计算模板与图像不同区域的相似度来定位目标。最常用的度量是归一化互相关(NCC),它对光照变化具有鲁棒性。
公式:
NCC(u,v)=Σ[T(x,y)⋅I(x+u,y+v)]/√(ΣT2⋅ΣI2)NCC(u,v) = Σ[T(x,y)·I(x+u,y+v)] / √(ΣT²·ΣI²)NCC(u,v)=Σ[T(x,y)⋅I(x+u,y+v)]/√(ΣT2⋅ΣI2)
其中T是模板,I是图像,(u,v)是偏移量
MATLAB实现
matlab
function [correlation_map, max_loc] = normalized_cross_correlation(img, template)
% 转换为灰度图像
if size(img, 3) == 3
img = rgb2gray(img);
end
if size(template, 3) == 3
template = rgb2gray(template);
end
% 获取尺寸
[h_img, w_img] = size(img);
[h_templ, w_templ] = size(template);
% 初始化相关图
correlation_map = zeros(h_img - h_templ + 1, w_img - w_templ + 1);
% 计算模板的均值和范数
mean_t = mean(template(:));
norm_t = norm(template(:) - mean_t);
% 滑动窗口计算NCC
for i = 1:(h_img - h_templ + 1)
for j = 1:(w_img - w_templ + 1)
patch = img(i:i+h_templ-1, j:j+w_templ-1);
mean_p = mean(patch(:));
norm_p = norm(patch(:) - mean_p);
if norm_p > 0
covariance = sum(sum((template - mean_t) .* (patch - mean_p)));
correlation_map(i, j) = covariance / (norm_t * norm_p);
end
end
end
% 找到最大值位置
[max_val, max_idx] = max(correlation_map(:));
[row, col] = ind2sub(size(correlation_map), max_idx);
max_loc = [row, col];
end
% 使用示例
img = imread('scene.jpg');
template = imread('template.jpg');
[corr_map, loc] = normalized_cross_correlation(img, template);
figure;
subplot(1,3,1); imshow(img); title('原始图像');
subplot(1,3,2); imshow(template); title('模板');
subplot(1,3,3);
imagesc(corr_map); colorbar;
title(sprintf('相关图 (最大值位置: %d,%d)', loc(1), loc(2)));
hold on;
plot(loc(2), loc(1), 'rx', 'MarkerSize', 15, 'LineWidth', 3);
二、Hausdorff距离匹配
原理
Hausdorff距离衡量两个点集之间的相似度,特别适合处理遮挡和部分可见的情况。
公式:
H(A,B)=maxh(A,B),h(B,A)H(A,B) = max{h(A,B), h(B,A)}H(A,B)=maxh(A,B),h(B,A)
h(A,B)=maxa∈Aminb∈B∣∣a−b∣∣h(A,B) = max_{a∈A} min_{b∈B} ||a-b||h(A,B)=maxa∈Aminb∈B∣∣a−b∣∣
其中A和B是两个点集
MATLAB实现
matlab
function [hd_map, min_loc] = hausdorff_distance(img, template)
% 边缘检测
if size(img, 3) == 3
img = rgb2gray(img);
end
if size(template, 3) == 3
template = rgb2gray(template);
end
edges_img = edge(img, 'Canny');
edges_temp = edge(template, 'Canny');
% 获取边缘点坐标
[y_img, x_img] = find(edges_img);
points_img = [x_img, y_img];
[y_temp, x_temp] = find(edges_temp);
points_temp = [x_temp, y_temp];
% 获取尺寸
[h_img, w_img] = size(img);
[h_temp, w_temp] = size(template);
% 初始化Hausdorff距离图
hd_map = inf(h_img - h_temp + 1, w_img - w_temp + 1);
% 滑动窗口计算Hausdorff距离
for i = 1:(h_img - h_temp + 1)
for j = 1:(w_img - w_temp + 1)
% 提取当前窗口内的边缘点
win_edges = edges_img(i:i+h_temp-1, j:j+w_temp-1);
[y_win, x_win] = find(win_edges);
points_win = [x_win+j-1, y_win+i-1]; % 全局坐标
if ~isempty(points_temp) && ~isempty(points_win)
% 计算有向Hausdorff距离 h(temp, win)
dist_matrix1 = pdist2(points_temp, points_win);
min_dists1 = min(dist_matrix1, [], 2);
h1 = max(min_dists1);
% 计算有向Hausdorff距离 h(win, temp)
dist_matrix2 = pdist2(points_win, points_temp);
min_dists2 = min(dist_matrix2, [], 2);
h2 = max(min_dists2);
% 双向Hausdorff距离
hd_map(i, j) = max(h1, h2);
end
end
end
% 找到最小距离位置
[min_val, min_idx] = min(hd_map(:));
[row, col] = ind2sub(size(hd_map), min_idx);
min_loc = [row, col];
end
% 使用示例
img = imread('scene.jpg');
template = imread('template.jpg');
[hd_map, loc] = hausdorff_distance(img, template);
figure;
subplot(1,3,1); imshow(img); title('原始图像');
subplot(1,3,2); imshow(template); title('模板');
subplot(1,3,3);
imagesc(hd_map); colorbar;
title(sprintf('Hausdorff距离图 (最小值位置: %d,%d)', loc(1), loc(2)));
hold on;
plot(loc(2), loc(1), 'go', 'MarkerSize', 15, 'LineWidth', 3);
三、基于距离变换的Hausdorff距离匹配
原理
传统Hausdorff距离计算复杂度高(O(mn))。基于距离变换的Hausdorff距离通过预处理图像边缘的距离变换图,将计算复杂度降低到O(m)。
步骤:
- 计算图像边缘的距离变换图(每个像素到最近边缘点的距离)
- 对于模板的每个边缘点,在距离变换图中查找其值
- 取这些值的最大值作为有向距离
- 双向计算取最大值
MATLAB实现
matlab
function [dth_map, min_loc] = distance_transform_hausdorff(img, template)
% 边缘检测
if size(img, 3) == 3
img = rgb2gray(img);
end
if size(template, 3) == 3
template = rgb2gray(template);
end
edges_img = edge(img, 'Canny');
edges_temp = edge(template, 'Canny');
% 计算距离变换图(欧氏距离)
dist_map = bwdist(edges_img, 'euclidean');
% 获取模板边缘点
[y_temp, x_temp] = find(edges_temp);
points_temp = [x_temp, y_temp];
% 获取尺寸
[h_img, w_img] = size(img);
[h_temp, w_temp] = size(template);
% 初始化距离图
dth_map = inf(h_img - h_temp + 1, w_img - w_temp + 1);
% 滑动窗口计算基于DT的Hausdorff距离
for i = 1:(h_img - h_temp + 1)
for j = 1:(w_img - w_temp + 1)
% 计算有向距离 h(temp, win)
if ~isempty(points_temp)
% 获取当前窗口内模板点的位置(全局坐标)
global_points = points_temp + repmat([j-1, i-1], size(points_temp, 1), 1);
% 确保在图像范围内
valid_idx = global_points(:,1) >= 1 & global_points(:,1) <= w_img & ...
global_points(:,2) >= 1 & global_points(:,2) <= h_img;
valid_points = global_points(valid_idx, :);
if ~isempty(valid_points)
% 从距离变换图中获取值
dist_values = dist_map(sub2ind(size(dist_map), valid_points(:,2), valid_points(:,1)));
h1 = max(dist_values);
else
h1 = inf;
end
else
h1 = 0;
end
% 计算有向距离 h(win, temp) - 简化处理
% 实际应用中需要计算当前窗口内边缘点到模板边缘点的距离
% 这里简化处理,仅计算h(temp, win)
h2 = 0; % 简化计算
% 双向距离(简化处理)
dth_map(i, j) = h1; % 实际应用中应为 max(h1, h2)
end
end
% 找到最小距离位置
[min_val, min_idx] = min(dth_map(:));
[row, col] = ind2sub(size(dth_map), min_idx);
min_loc = [row, col];
end
% 使用示例
img = imread('scene.jpg');
template = imread('template.jpg');
[dth_map, loc] = distance_transform_hausdorff(img, template);
figure;
subplot(1,3,1); imshow(img); title('原始图像');
subplot(1,3,2); imshow(template); title('模板');
subplot(1,3,3);
imagesc(dth_map); colorbar;
title(sprintf('DT Hausdorff距离图 (最小值位置: %d,%d)', loc(1), loc(2)));
hold on;
plot(loc(2), loc(1), 'mo', 'MarkerSize', 15, 'LineWidth', 3);
四、综合比较与应用
三种方法比较
| 特性 | 相关匹配 | Hausdorff距离 | 基于DT的Hausdorff |
|---|---|---|---|
| 计算复杂度 | O(n²m²) | O(n²m²) | O(n²m) |
| 光照鲁棒性 | 高(NCC) | 低 | 低 |
| 遮挡鲁棒性 | 低 | 高 | 高 |
| 旋转/尺度不变性 | 无 | 无 | 无 |
| 适用场景 | 精确匹配,光照稳定 | 部分遮挡,轮廓匹配 | 实时系统,轮廓匹配 |
主函数:综合演示
matlab
function image_matching_demo()
% 读取图像和模板
scene = imresize(imread('scene.jpg'), 0.5);
templ = imresize(imread('template.jpg'), 0.5);
% 添加遮挡和部分可见
scene(100:150, 100:150, :) = 255; % 白色遮挡块
templ = templ(1:end-30, :, :); % 部分模板
% 相关匹配
[corr_map, corr_loc] = normalized_cross_correlation(scene, templ);
% Hausdorff距离匹配
[hd_map, hd_loc] = hausdorff_distance(scene, templ);
% 基于DT的Hausdorff距离匹配
[dth_map, dth_loc] = distance_transform_hausdorff(scene, templ);
% 可视化结果
figure('Position', [100, 100, 1400, 800], 'Name', '图像匹配技术比较');
% 原始图像和模板
subplot(2,4,1); imshow(scene); title('场景图像');
subplot(2,4,2); imshow(templ); title('模板图像');
% 相关匹配结果
subplot(2,4,3);
imagesc(corr_map); colorbar;
title(sprintf('相关匹配\n位置: %d,%d', corr_loc(1), corr_loc(2)));
hold on; plot(corr_loc(2), corr_loc(1), 'rx', 'MarkerSize', 15, 'LineWidth', 3);
subplot(2,4,4);
imshow(scene);
rectangle('Position', [corr_loc(2), corr_loc(1), size(templ,2), size(templ,1)], ...
'EdgeColor', 'r', 'LineWidth', 2);
title('相关匹配结果');
% Hausdorff距离匹配结果
subplot(2,4,5);
imagesc(hd_map); colorbar;
title(sprintf('Hausdorff距离\n位置: %d,%d', hd_loc(1), hd_loc(2)));
hold on; plot(hd_loc(2), hd_loc(1), 'go', 'MarkerSize', 15, 'LineWidth', 3);
subplot(2,4,6);
imshow(scene);
rectangle('Position', [hd_loc(2), hd_loc(1), size(templ,2), size(templ,1)], ...
'EdgeColor', 'g', 'LineWidth', 2);
title('Hausdorff匹配结果');
% 基于DT的Hausdorff距离匹配结果
subplot(2,4,7);
imagesc(dth_map); colorbar;
title(sprintf('DT Hausdorff\n位置: %d,%d', dth_loc(1), dth_loc(2)));
hold on; plot(dth_loc(2), dth_loc(1), 'mo', 'MarkerSize', 15, 'LineWidth', 3);
subplot(2,4,8);
imshow(scene);
rectangle('Position', [dth_loc(2), dth_loc(1), size(templ,2), size(templ,1)], ...
'EdgeColor', 'm', 'LineWidth', 2);
title('DT Hausdorff匹配结果');
% 添加性能指标
perf_text = {
'性能指标比较:';
sprintf('相关匹配: 精度高但遮挡敏感 (%.2f)', min(corr_map(:)));
sprintf('Hausdorff: 遮挡鲁棒但计算慢 (%.2f)', min(hd_map(:)));
sprintf('DT Hausdorff: 实时性好 (%.2f)', min(dth_map(:)))
};
annotation('textbox', [0.15, 0.01, 0.7, 0.05], 'String', perf_text, ...
'FitBoxToText', 'on', 'BackgroundColor', 'white', ...
'FontSize', 10, 'EdgeColor', 'black');
end
% 运行演示
image_matching_demo();
参考代码 相关匹配,Hausdorff距离匹配,基于distance transform的Hausdorff距离匹配 www.youwenfan.com/contentcsr/100580.html
五、应用场景与改进方向
典型应用场景
- 工业检测:零件定位和缺陷识别
- 医学影像:器官或病变区域匹配
- 机器人导航:环境特征匹配
- 卫星遥感:地物识别和变化检测
- 增强现实:实时跟踪和定位
改进方向
-
多尺度匹配:金字塔搜索策略
matlabfunction multi_scale_match(img, template) scales = 0.8:0.1:1.2; % 缩放比例 best_loc = []; best_score = inf; for s = scales scaled_templ = imresize(template, s); [score, loc] = hausdorff_distance(img, scaled_templ); if score < best_score best_score = score; best_loc = loc; best_scale = s; end end end -
旋转不变性:极坐标变换或SIFT特征
-
GPU加速:使用CUDA或MATLAB Parallel Computing Toolbox
-
机器学习融合:CNN特征与传统方法结合
-
多特征融合:结合颜色、纹理和形状特征
六、总结
本文详细介绍了三种重要的图像匹配技术:
- 相关匹配:基于像素级相似度,适合精确匹配
- Hausdorff距离匹配:基于点集距离,对遮挡鲁棒
- 基于距离变换的Hausdorff距离匹配:计算效率高,适合实时应用