ace_vent
ace_vent

Reputation: 278

How to align images based on a pixel in Matlab?

I have two similar images: [A] and [B] (please see images). They are offset in X and Y. How to align A over B, using an pixel from A as reference? In other words, locating the indicated pixel from A on B, and make A and B centralized in this pixel. Thank you.

Image A Image B

Final result make manually

enter image description here

Upvotes: 0

Views: 4525

Answers (3)

Mercury
Mercury

Reputation: 1965

you can do it manually:

img1 = 255-mean(imread('a1.png'),3);
img2 = 255-mean(imread('a2.png'),3);

subplot(221);imagesc(img1);axis image
[x1 y1] = ginput(1);
subplot(222);imagesc(img2);axis image
[x2 y2] = ginput(1);



x = x1-x2;
y = y1-y2;

T = maketform('affine',[1 0 x;0 1 y; 0 0 1]');
img2N = imtransform(img2,T,'xdata',[1 size(img1,2)],'ydata',[1 size(img1,1)]);

subplot(2,2,[3 4]);
imagesc(max(img1,img2N));axis image

for doing it automaticly, you can do this::

%size(img2) <= size(img1)
img1 = 255-mean(imread('a1.png'),3);
img2 = 255-mean(imread('a2.png'),3);
subplot(221);imagesc(img1);axis image
subplot(222);imagesc(img2);axis image
colormap(gray(256))
c = normxcorr2(img2,img1);
[y x] = find(c==max(c(:)));
y = y-size(img2,1);
x = x-size(img2,2);

T = maketform('affine',[1 0 x;0 1 y; 0 0 1]');
img2N = imtransform(img2,T,'xdata',[1 size(img1,2)],'ydata',[1 size(img1,1)]);
subplot(2,2,[3 4]);
imagesc(max(img1,img2N));axis image

Upvotes: 2

BRM
BRM

Reputation: 201

You will need to fine tune the 'XData' and 'YData' properties but you could do this...

rgbA = imread('A.jpg'):
rgbB = imread('B.jpg');
alpha(.2)
image(rgbA,'XData',2)
alpha(.2)
hold on
image(rgbB,'XData',2)
alpha(.2)

Upvotes: 0

chaohuang
chaohuang

Reputation: 4115

I think what you want is image registration, which requires, in your case, at least 2 control points, because it's affine transformation without reflect. Given the similarity of those 2 images, I think it's easy to find another referral point. After that you can use imtransform or simply cp2tform to perform the registration.

Upvotes: 0

Related Questions