【发布时间】:2015-09-19 05:10:40
【问题描述】:
我在 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);
代码未按预期运行 - 它未识别正确的区域。这是失败的结果 - 错误的区域被装箱:
希望您能提供帮助!谢谢。
【问题讨论】:
-
如果您查看图像处理工具箱中 normxcorr2 的实现,其中一个代码路径是在频域中计算的。您可能应该将其作为参考。
标签: matlab image-processing fft template-matching cross-correlation