平移旋转以后,还需要插值,才能显示完整的图像
clear all; close all; clc;
img=imread('lena.bmp');
[h w]=size(img);
%平移旋转参数
a = 10; b = 20;
theta = 30 /180*pi;
% 旋转矩阵
rot=[cos(theta) -sin(theta) 0;
sin(theta) cos(theta) 0;
0 0 1];
% 平移矩阵
move = [ 1 0 a;
0 1 b;
0 0 1];
%平移加旋转
rot = rot * move;
pix1=[a b 1]*rot;\x09% 左上点的坐标
pix2=[a w+b 1]*rot; % 右上点的坐标
pix3=[h+a b 1]*rot;\x09% 左下点的坐标
pix4=[h+a w+b 1]*rot;\x09% 右下点的坐标
height = round(max([abs(pix1(1)-pix4(1)) abs(pix2(1)-pix3(1))])); %变换后图像的高度
width = round( max([abs(pix1(2)-pix4(2)) abs(pix2(2)-pix3(2))])); %变换后图像的宽度
imgn=zeros(height,width);
delta_y = abs(min([pix1(1) pix2(1) pix3(1) pix4(1)]));
delta_x = abs(min([pix1(2) pix2(2) pix3(2) pix4(2)]));
for i=1-delta_y:height
for j=1-delta_x:width
pix=[i j 1]/rot; %用变换后图像的点的坐标去寻找原图像点的坐标,
float_Y=pix(1)-floor(pix(1));
float_X=pix(2)-floor(pix(2));
if pix(1)>=1 && pix(2)>=1 && pix(1) <= h && pix(2) <= w
pix_up_left=[floor(pix(1)) floor(pix(2))]; %四个相邻的点
pix_up_right=[floor(pix(1)) ceil(pix(2))];
pix_down_left=[ceil(pix(1)) floor(pix(2))];
pix_down_right=[ceil(pix(1)) ceil(pix(2))];
value_up_left=(1-float_X)*(1-float_Y);\x09\x09
value_up_right=float_X*(1-float_Y);
value_down_left=(1-float_X)*float_Y;
value_down_right=float_X*float_Y;
\x09imgn(i+delta_y,j+delta_x) = value_up_left*img(pix_up_left(1),pix_up_left(2))+...
\x09value_up_right*img(pix_up_right(1),pix_up_right(2))+...
\x09value_down_left*img(pix_down_left(1),pix_down_left(2))+...
\x09value_down_right*img(pix_down_right(1),pix_down_right(2));
end
end
end
imshow(uint8(imgn));title('平移旋转并插值');