Affine transformation code for --MatLab

Here to talk about our purpose, recently doing a project with Pix2Pix when he met a pair of poor image quality, there is a problem feature is not aligned, namely A map and a B map is composed of the image, we want to FIG object into the object B in a in the figure, but the coordinate position of the object a versus B large difference in FIG.

To solve this problem, we use the affine transformation technology to solve this problem, the principle of affine transformation there is a lot of online search, not described here, only here given code.

img_x = "./1.png";
img_y = "./2.png";
x = imread(img_x);  % 读取图像x
y = imread(img_y);  % 读取图像y
figure;
subplot(1, 2, 1); imshow(x);    % 显示图像x
subplot(1, 2, 2); imshow(y);    % 显示图像y
set(gcf, "outerposition", get(0, "screensize"));    % 将图像全萤幕显示
[x0,y0] = ginput(3);    % 通过鼠标点击三次取得三个坐标点
[x1,y1] = ginput(3);    % 通过鼠标点击三次取得目标图中对应的三个坐标点
close all;  % 关闭图像显示
in_points = [x0,y0];
out_points = [x1,y1];
tform2 = maketform('affine', in_points, out_points);    % 计算变换矩阵
T = affine2d(tform2.tdata.T);   % 将变换矩阵转化为仿射变换矩阵类型
z = imwarp(x,T,'OutputView',imref2d(size(y)));  % 进行仿射变换
img_result = "./3.png";
imwrite(z, img_result); % 存储结果

The effect is as follows:

Guess you like

Origin www.cnblogs.com/dereen/p/affine_matlab.html