无法使用normxcorr2 matlab函数将相关图像定位在原始图像的顶部

时间:2013-07-16 17:21:03

标签: matlab image-processing computer-vision

我试图使用normxcorr2 matlab函数找到图像子图像的确切位置。除了将图像放在准确的位置外,它似乎运作良好。

完整图片为full image

子图像是part of the image

我找到两个图像相关的xpeak和ypeak,并使用这两个坐标显示它们。但由于未知原因,它不在它应该的位置。

输出看起来像resultant image

我的代码如下

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)

请提出建议

1 个答案:

答案 0 :(得分:1)

你换了x和y。 offset定义为[y, x],但您已将其用作[x, y]。我在下面指出了以下几行:

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

然后

xbegin = offset(1)+1;
xend = offset(1)+size(obj,2);
ybegin = offset(2)+1;
yend = offset(2)+size(obj,1);