图像处理之图像平移

一、图像平移

图像平移:将图像上的所有像素点按照给定的偏移量移动,平移不改变图像内容,只改变图像位置

设像素点 ( 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\} xy1=100010ΔxΔy1xy1

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('尺寸扩大平移');

自带函数图像平移效果图:
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值