Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Cross-Correlation between two images

Date: 2022-07-15 10:57:54

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: 

Patch: 

Result of cross correlation: 

Answer: 

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

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

However, for this method to work properly the patch (template) and the full image should be scaled to the same resolution. 

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com