简体   繁体   English

如何在Matlab中基于像素对齐图像?

[英]How to align images based on a pixel in Matlab?

I have two similar images: [A] and [B] (please see images). 我有两个相似的图像:[A]和[B](请参见图像)。 They are offset in X and Y. How to align A over B, using an pixel from A as reference? 它们在X和Y中偏移。如何使用A中的像素作为参考,将A与B对齐? In other words, locating the indicated pixel from A on B, and make A and B centralized in this pixel. 换句话说,将指示的像素从A上定位在B上,并使A和B集中在该像素中。 Thank you. 谢谢。

图片A图B

Final result make manually 最终结果是手动制作

在此输入图像描述

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

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. 我认为你想要的是图像配准,在你的情况下,它需要至少2个控制点,因为它是没有反射的仿射变换。 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. 之后,您可以使用imtransform或简单地使用cp2tform来执行注册。

You will need to fine tune the 'XData' and 'YData' properties but you could do this... 你需要微调'XData'和'YData'属性,但你可以这样做......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM