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

发布时间:2022-08-19 12:53

本文用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
Matlab实现计算机视觉中的基本图像变换_第1张图片

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号