Matlab基础知识一——图像平移,镜像,缩放,旋转

图像平移

图像平移函数move.m

function J = move(I, a, b)

[M, N, G] = size(I);
I = im2double(I);
J = ones(M + abs(a), N + abs(b), G); %建立新的矩阵,将新图像扩大,避免越过边界

for i = 1:M
    for j = 1:N
        if(a < 0 && b < 0)            
            J(i, j, :) = I(i, j, :);
        elseif(a > 0 && b > 0)
            J(i + a, j + b, :) = I(i, j , :);
        elseif(a < 0 && b > 0)
            J(i, j + b, :) = I(i, j, :);
        else
            J(i + a, j, :) = I(i, j, :);
        end
    end
end
end

通过改变a,b实现图像的平移

close all;
clear all;
clc;
I=imread('flower800x480_24b.bmp');
a = -50; b = -50;
J1 =move(I,a,b);
figure,
subplot(122),imshow(J1),axis on;
subplot(121),imshow(I),axis on;
size(I)
size(J1)

图像镜像

图像旋转函数mirror.m

function J = mirror(I, a)
[M, N, G] = size(I);
I = im2double(I);
J = ones(M, N, G); 
for i = 1:M
    for j = 1:N
        if(a == 0)%水平镜像            
            J(i, N-j+1, :) = I(i, j, :);
        elseif(a == 1 )%垂直镜像 
            J(M-i+1, j, :) = I(i, j, :);
        elseif(a == 2 )%水平垂直
            J(M-i+1, N-j+1, :) = I(i, j, :);
        end
    end
end
close all;
clear all;
clc;
I=imread('flower800x480_24b.bmp');
J1 =mirror(I,0);
J2=mirror(I,1);
J3=mirror(I,2);
figure,
subplot(132),imshow(J1),axis on;
subplot(131),imshow(J3),axis on;
subplot(133),imshow(J2),axis on;
size(I)
size(J1)

图像缩放

直接调用函数imresize().

close all;
clear all;
clc;
[I,map]=imread('flower800x480_24b.bmp');
J1 =imresize(I,0.25);%缩小
J2=imresize(I,2);%放大两倍
J3=imresize(I,[240,800]);%固定图片的宽高以缩放
J4=imresize(I,1.6,'bilinear');%双线性插值法
J5=imresize(I,1.6,'triangle');%三角函数插值法

figure,
subplot(131),imshow(J1),axis on;
subplot(132),imshow(J4),axis on;
subplot(133),imshow(J5),axis on;
size(I)
size(J1)

图像旋转

直接调用函数imrotate().

close all;
clear all;
clc;
I=imread('flower800x480_24b.bmp');
J1 =imrotate(I,30);
J2=imrotate(I,60,rotate'crop');
J3=imrotate(I,60,'loose');
figure,
subplot(132),imshow(J1),axis on;
subplot(131),imshow(J3),axis on;
subplot(133),imshow(J2),axis on;
%imrotate(I,angle,method,bbox)
%method:nearest/最近邻插值;bilinear/双线性插值;bicubic/双三次插值;
%bbox:说明返回图像的大小,crop表示相同大小;loose表示输出图像足够大;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值