0

我正在尝试使用 normxcorr2 matlab 函数定位图像子图像的确切位置。除了将图像放置在确切位置之外,它似乎工作得很好。

全图是这个全图

子图是这个图像的一部分

我发现两个图像的相关性的 xpeak 和 ypeak 并使用这两个坐标显示它们。但不知什么原因,它不在它应该在的位置。

输出看起来像这样结果图像

我的代码如下

clear
clc
%#read & convert the image
imgGray  = imread('1.jpg');
imgGray = rgb2gray(imgGray);
obj     = rgb2gray(imread('2.jpg'));




%# cross-correlate and find the offset
cor          = normxcorr2(obj,imgGray); 
[max_cc,indx]       = max(abs(cor(:))); %# Modify for multiple instances (generalize)
[yPeak, xPeak] = ind2sub(size(cor),indx(1));
offset     = [yPeak - size(obj,1), xPeak - size(obj,2)]; 

% size(obj,2)
% create a mask
xbegin = offset(1)+1;
xend = offset(1)+size(obj,2);
ybegin = offset(2)+1;
yend = offset(2)+size(obj,1);
mask      = uint8(zeros(size(imgGray)));
mask(ybegin:yend,xbegin:xend) = obj;




h1 = imshow(imgGray);
set(h1,'AlphaData',0.7)
hold on
h2= imshow(mask);
set(h2,'AlphaData',0.9)

请提出一些建议

4

1 回答 1

1

You've swapped x and y. offset is defined as [y, x], but you've used it as [x, y]. I've pointed out these lines below:

offset     = [yPeak - size(obj,1), xPeak - size(obj,2)];

and then

xbegin = offset(1)+1;
xend = offset(1)+size(obj,2);
ybegin = offset(2)+1;
yend = offset(2)+size(obj,1);
于 2013-07-16T17:26:51.097 回答