两个图像之间的互相关 [英] Cross-Correlation between two images
问题描述
如何在一个图像上选择一个随机点,然后使用互相关在另一个图像上找到它的对应点?
How can I select a random point on one image, then find its corresponding point on another image using cross-correlation?
所以基本上我有image1,我想要选择它上面的一个点(自动)然后在image2上找到它的相应/相似点。
So basically I have image1, I want to select a point on it (automatically) then find its corresponding/similar point on image2.
以下是一些示例图像:
完整图片:
补丁:
互相关结果:
推荐答案
那么, xcorr2
基本上可以被视为分析正向和负向的所有可能的变化,并给出它们适合每个班次的程度。因此,对于大小 N x N
的图像,结果必须大小(2 * N-1)x(2 * N-1)
,其中如果两个图像相等或不相移,则索引 [N,N]
的相关性将是最大的。如果它们移动了10个像素,则最大相关性将为 [N-10,N]
,依此类推。因此,您需要减去 N
以获得绝对移位。
Well, xcorr2
can essentially be seen as analyzing all possible shifts in both positive and negative direction and giving a measure for how well they fit with each shift. Therefore for images of size N x N
the result must have size (2*N-1) x (2*N-1)
, where the correlation at index [N, N]
would be maximal if the two images where equal or not shifted. If they were shifted by 10 pixels, the maximum correlation would be at [N-10, N]
and so on. Therefore you will need to subtract N
to get the absolute shift.
使用您的实际代码可能会更容易帮助。但让我们看一个例子:
With your actual code it would probably be easier to help. But let's look at an example:
(A)我们读取一个图像并选择两个不同的子图像,其中包含偏移量da和db
(A) We read an image and select two different sub-images with offsets da and db
Orig = imread('rice.png');
N = 200; range = 1:N;
da = [0 20];
db = [30 30];
A=Orig(da(1) + range, da(2) + range);
B=Orig(db(1) + range, db(2) + range);
(b)计算互相关并找到最大值
(b) Calculate cross-correlation and find maximum
X = normxcorr2(A, B);
m = max(X(:));
[i,j] = find(X == m);
(C)使用恢复的班次将它们一起修补
(C) Patch them together using recovered shift
R = zeros(2*N, 2*N);
R(N + range, N + range) = B;
R(i + range, j + range) = A;
(D)说明事情
figure
subplot(2,2,1), imagesc(A)
subplot(2,2,2), imagesc(B)
subplot(2,2,3), imagesc(X)
rectangle('Position', [j-1 i-1 2 2]), line([N j], [N i])
subplot(2,2,4), imagesc(R);
(E)将故意转移与恢复的转变进行比较
(E) Compare intentional shift with recovered shift
delta_orig = da - db
%--> [30 10]
delta_recovered = [i - N, j - N]
%--> [30 10]
正如您在(E)中看到的那样,我们得到了我们特意引入的转变( A)。
As you see in (E) we get exactly the shift we intenionally introduced in (A).
或根据您的情况调整:
full=rgb2gray(imread('a.jpg'));
template=rgb2gray(imread('b.jpg'));
S_full = size(full);
S_temp = size(template);
X=normxcorr2(template, full);
m=max(X(:));
[i,j]=find(X==m);
figure, colormap gray
subplot(2,2,1), title('full'), imagesc(full)
subplot(2,2,2), title('template'), imagesc(template),
subplot(2,2,3), imagesc(X), rectangle('Position', [j-20 i-20 40 40])
R = zeros(S_temp);
shift_a = [0 0];
shift_b = [i j] - S_temp;
R((1:S_full(1))+shift_a(1), (1:S_full(2))+shift_a(2)) = full;
R((1:S_temp(1))+shift_b(1), (1:S_temp(2))+shift_b(2)) = template;
subplot(2,2,4), imagesc(R);
但是,要使此方法正常运行补丁(模板
)并且应该将完整图像缩放到相同的分辨率。
However, for this method to work properly the patch (template
) and the full image should be scaled to the same resolution.
还可以找到更详细的示例在其他地方。
A more detailed example can also be found here.
这篇关于两个图像之间的互相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!