我在Matlab中使用傅里叶域中的模板匹配遇到了困难。这里是我的图片(艺术家是DeviantArt上的RamalamaCreatures):
我的目标是在负鼠的耳朵周围放置一个边界框,就像这个例子一样(我使用normxcorr2执行了模板匹配):
这是我正在使用的Matlab代码:
clear all; close all;
template = rgb2gray(imread('possum_ear.jpg'));
background = rgb2gray(imread('possum.jpg'));
%% calculate padding
bx = size(background, 2);
by = size(background, 1);
tx = size(template, 2); % used for bbox placement
ty = size(template, 1);
%% fft
c = real(ifft2(fft2(background) .* fft2(template, by, bx)));
%% find peak correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = find(c == max(c(:)));
figure; surf(c), shading flat; % plot correlation
%% display best match
hFig = figure;
hAx = axes;
position = [xpeak(1)-tx, ypeak(1)-ty, tx, ty];
imshow(background, 'Parent', hAx);
imrect(hAx, position);
代码未按预期运行 - 它未能识别正确的区域。这是失败的结果 - 错误的区域被框选:
![failed template matching](https://istack.dev59.com/DH9kg.webp)
这是匹配失败情况下相关性的表面绘图:
![surf plot for failed template matching](https://istack.dev59.com/HwQJI.webp)
希望你能提供帮助!谢谢。