一、图像平移
图像平移:将图像上的所有像素点按照给定的偏移量移动,平移不改变图像内容,只改变图像位置
设像素点
(
x
,
y
)
(x, y)
(x,y)进行平移到点
(
x
∗
,
y
∗
)
(x^*, y^*)
(x∗,y∗),其中
x
x
x轴方向的平移量为
Δ
x
\Delta{x}
Δx,
y
y
y轴方向的平移量为
Δ
y
\Delta{y}
Δy,则平移变换公式为:
{
x
∗
=
x
+
Δ
x
y
∗
=
y
+
Δ
y
\left\{ \begin{matrix} x^* = x + \Delta{x}\\ y^* = y + \Delta{y} \end{matrix} \right.
{x∗=x+Δxy∗=y+Δy
用矩阵表示为:
{
x
∗
y
∗
1
}
=
{
1
0
Δ
x
0
1
Δ
y
0
0
1
}
{
x
y
1
}
\left\{ \begin{matrix} x^* \\ y^* \\ 1 \end{matrix} \right\}= \left\{ \begin{matrix} 1 & 0 & \Delta{x} \\ 0 & 1 & \Delta{y} \\ 0 & 0 & 1 \end{matrix} \right\} \left\{ \begin{matrix} x \\ y \\ 1 \end{matrix} \right\}
⎩⎨⎧x∗y∗1⎭⎬⎫=⎩⎨⎧100010ΔxΔy1⎭⎬⎫⎩⎨⎧xy1⎭⎬⎫
0. 待平移处理的原图像
1. 不使用MATLAB自带函数
clc,clear,close all;
Image = im2double(imread('flower.jpg'));
[h, w, c] = size(Image);
NewImage1 = ones(h, w, c);
deltx = 20; delty = 40;
for Newx = 1:w
for Newy = 1:h
x = Newx - deltx;
y = Newy - delty;
if x > 0 && x < w && y > 0 && y < h
NewImage1(Newy, Newx, :) = Image(y, x, :);
end
end
end
NewImage2 = ones(h, w, c);
deltx = -20; delty = 40;
for Newx = 1:w
for Newy = 1:h
x = Newx - deltx;
y = Newy - delty;
if x > 0 && x < w && y > 0 && y < h
NewImage2(Newy, Newx, :) = Image(y, x, :);
end
end
end
NewImage3 = ones(h, w, c);
deltx = 20; delty = -40;
for Newx = 1:w
for Newy = 1:h
x = Newx - deltx;
y = Newy - delty;
if x > 0 && x < w && y > 0 && y < h
NewImage3(Newy, Newx, :) = Image(y, x, :);
end
end
end
NewImage4 = ones(h, w, c);
deltx = -20; delty = -40;
for Newx = 1:w
for Newy = 1:h
x = Newx - deltx;
y = Newy - delty;
if x > 0 && x < w && y > 0 && y < h
NewImage4(Newy, Newx, :) = Image(y, x, :);
end
end
end
subplot(221), imshow(NewImage1), title('deltx = 20, delty = 40');
subplot(222), imshow(NewImage2), title('deltx = -20, delty = 40');
subplot(223), imshow(NewImage3), title('deltx = 20, delty = -40');
subplot(224), imshow(NewImage4), title('deltx = -20, delty = -40');
图像平移效果图:
2. 使用MATLAB自带函数
%% 函数解析
% 产生转换结构
TForm = maketform(TransformType)
其中:TransformType可取'affine'、'profective'、'custom'、'box'、'composite'
% 平移变换
NewImage = imtransform(OldImage, TForm , ...)
clc,clear,close all;
Image = imread('flower.jpg');
deltax= 20;deltay = 40;
T = maketform('affine', [1 0 0;0 1 0;deltax deltay 1]);
NewImage1 = imtransform(Image, T, 'XData', [1 size(Image, 2)], 'YData', [1 size(Image, 1)], 'FillValue', 255);
NewImage2 = imtransform(Image, T, 'XData', [1 size(Image, 2)+deltax], 'YData', [1 size(Image, 1)+deltay], 'FillValue', 255);
subplot(131), imshow(Image), title('原图');
subplot(132), imshow(NewImage1), title('尺寸不变平移');
subplot(133), imshow(NewImage2), title('尺寸扩大平移');
自带函数图像平移效果图: