Matlab实现计算机视觉中的基本图像变换

本文用matlab实现了计算机视觉中的6大基础图像变换操作,包括:

1. 水平、垂直镜像
2. 图像缩放
3. 平移
4. 旋转
5. 仿射
6. 透视

clear;
close all;
img  = imread('C:\Users\HP\Desktop\lena.bmp');
[m,n] = size(img);
subplot(1,3,1);
imshow(img);
title('原始图像');

% -----------------水平镜像-----------------
sp_mirror = img;
for i = 1 : m
    for j = 1 : n
        sp_mirror(i,j) = img(i,n+1-j);
    end
end
subplot(1,3,2);
imshow(sp_mirror);
title('水平镜像');

% -----------------垂直镜像-----------------
ch_mirror = img;
for i = 1 : m
    for j = 1 : n
        ch_mirror(i,j) = img(m+1-i,j);
    end
end
subplot(1,3,3);
imshow(ch_mirror);
title('垂直镜像');

% -----------------图像缩放-----------------
% X轴缩放量
timesX = 1.5;  
% Y轴缩放量
timesY = 1.5;  
% 构造结果矩阵
res = zeros(timesX * m, timesY * n);
% 缩放的变换矩阵
T = [1/timesX     0     0; 
        0     1/timesY  0;
        0         0     1];
for i = 1 : timesX * m
    for j = 1 : timesY * n
        temp = [i; j; 1];
        temp = T * temp;
        x = uint16(temp(1, 1));
        y = uint16(temp(2, 1));
        % 变换后的位置判断是否越界
        if (x <= m) && (y <= n) && (x >= 1) && (y >= 1)
            res(i, j) = img(x, y);
        end
    end
end
figure;
imshow(uint8(res));

% -----------------图像平移-----------------
% 水平向右平移50,竖直向下平移50
% 构造结果矩阵
res = zeros(m, n);
% 平移量X
dispx = 50;
% 平移量Y
dispy = 50;
T = [1   0   dispx;
     0   1   dispy;
     0   0    1];
% 平移的变换矩阵
for i = 1 : m
    for j = 1 : n
        temp = [i; j; 1];
        temp = T * temp;
        x = temp(1, 1);
        y = temp(2, 1);
        % 变换后的位置判断是否越界
        if (x <= m) && (y <= n) && (x >= 1) && (y >= 1)
            res(x, y) = img(i, j);
        end
    end
end
figure;
imshow(uint8(res));

% -----------------图像旋转-----------------
% 逆时针旋转30% 构造结果矩阵
res = zeros(m, n);
% 旋转角度 
alfa = -15 * pi / 180; 
% 旋转的变换矩阵
T = [cos(alfa) -sin(alfa) 0;
     sin(alfa) cos(alfa)  0;
         0        0       1]; 
for i = 1 : m
    for j = 1 : n
        temp = [i; j; 1];
        temp = T * temp;
        x = uint16(temp(1, 1));
        y = uint16(temp(2, 1));
        % 变换后的位置判断是否越界
        if (x <= m) && (y <= n) && (x >= 1) && (y >= 1) 
            res(i, j) = img(x, y); 
        end
    end
end
figure;
imshow(uint8(res));

% -----------------图像仿射变换-----------------
% 构造结果矩阵
res = zeros(m, n);
% 仿射变换矩阵
T = [  2  0.4  -100;
     0.8  1.2  -100;
       0    0    1]; 
for i = 1 : m
    for j = 1 : n
        temp = [i; j; 1];
        temp = T * temp;
        x = uint16(temp(1, 1));
        y = uint16(temp(2, 1));
        % 变换后的位置判断是否越界
        if (x <= m) && (y <= n) && (x >= 1) && (y >= 1) 
            res(i, j) = img(x, y); 
        end
    end
end
figure;
imshow(uint8(res));

% -----------------图像透视变换-----------------
% 构造结果矩阵
res = zeros(m, n);
% 旋转角度 
% 透视变换矩阵
T = [  2  0.4  -100;
     0.4  0.8  -100;
     -0.7  2.5    1]; 
for i = 1 : m
    for j = 1 : n
        temp = [i; j; 1];
        temp = T * temp;
        x = uint16(temp(1, 1));
        y = uint16(temp(2, 1));
        % 变换后的位置判断是否越界
        if (x <= m) && (y <= n) && (x >= 1) && (y >= 1) 
            res(i, j) = img(x, y); 
        end
    end
end
figure;
imshow(uint8(res));

附:lena_gray.bmp
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Polaris_T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值