Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

So basically I have image1, I want to select a point on it (automatically) then find its corresponding/similar point on image2.

Here are some example images:

Full image:

full image

Patch:

patch

Result of cross correlation:

result of cross correlation

like image 405
Ramo Avatar asked Mar 08 '14 12:03

Ramo


1 Answers

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) 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) Calculate cross-correlation and find maximum

X = normxcorr2(A, B);
m = max(X(:));
[i,j] = find(X == m);

(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) Illustrate things

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) Compare intentional shift with recovered shift

delta_orig = da - db
%--> [30 10]
delta_recovered = [i - N, j - N]
%--> [30 10]

As you see in (E) we get exactly the shift we intenionally introduced in (A).

Result of xcorr2


Or adjusted to your case:

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);

enter image description here

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.

like image 131
mbschenkel Avatar answered Sep 30 '22 12:09

mbschenkel