我试图使用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)
请提出建议
答案 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);