Matlab image translation, rotation, scaling, mirroring

Today, learning the basic operation of the image using Matlab. In Matlab, the image is represented in the form of two-dimensional matrix. So the operation is the operation of the image matrix. 

Image scaling, translation, rotation, can be converted into matrix operations. 
Configured on the transformation matrix, refer to: 
[Gym 101047C Robotics Competition] Power matrix solving the point fast rotational position after N times translation
Reference picture: 
Artwork

1. Image translation

init = imread('Fig3.tif'); % 读取图像
[R, C] = size(init); % 获取图像大小 res = zeros(R, C); % 构造结果矩阵。每个像素点默认初始化为0(黑色) delX = 50; % 平移量X delY = 50; % 平移量Y tras = [1 0 delX; 0 1 delY; 0 0 1]; % 平移的变换矩阵 for i = 1 : R for j = 1 : C temp = [i; j; 1]; temp = tras * temp; % 矩阵乘法 x = temp(1, 1); y = temp(2, 1); % 变换后的位置判断是否越界 if (x <= R) & (y <= C) & (x >= 1) & (y >= 1) res(x, y) = init(i, j); end end end; imshow(uint8(res)); % 显示图像

Image translation

2. Image Rotation

init = imread('Fig3.tif'); % 读取图像
[R, C] = size(init); % 获取图像大小 res = zeros( R, C); % 构造结果矩阵。每个像素点默认初始化为0(黑色) alfa = -15 * 3.1415926 / 180.0; % 旋转角度 tras = [cos(alfa) -sin(alfa) 0; sin(alfa) cos(alfa) 0; 0 0 1]; % 旋转的变换矩阵 for i = 1 : R for j = 1 : C temp = [i; j; 1]; temp = tras * temp;% 矩阵乘法 x = uint16(temp(1, 1)); y = uint16(temp(2, 1)); % 变换后的位置判断是否越界 if (x <= R) & (y <= C) & (x >= 1) & (y >= 1) res(i, j) = init(x, y); end end end; imshow(uint8(res)); % 显示图像

Image rotation

3. The image scaling

init = imread('Fig3.tif'); % 读取图像
[R, C] = size(init); % 获取图像大小 timesX = 3; % X轴缩放量 timesY = 3; % Y轴缩放量 res = zeros(timesX * R, timesY * C); % 构造结果矩阵。每个像素点默认初始化为0(黑色) tras = [1/timesX 0 0; 0 1/timesY 0; 0 0 1]; % 缩放的变换矩阵 for i = 1 : timesX * R for j = 1 : timesY * C temp = [i; j; 1]; temp = tras * temp; % 矩阵乘法 x = uint8(temp(1, 1)); y = uint8(temp(2, 1)); % 变换后的位置判断是否越界 if (x <= R) & (y <= C) & (x >= 1) & (y >= 1) res(i, j) = init(x, y); end end end; imshow(uint8(res)); % 显示图像

Image scaling

4. Mirror image (horizontal)

init = imread('Fig3.tif');
[R, C] = size(init);
res = zeros(R, C);

for i = 1 : R for j = 1 : C x = i; y = C - j + 1; res(x, y) = init(i, j); end end imshow(uint8(res));

Mirror image

Guess you like

Origin www.cnblogs.com/Ph-one/p/11566673.html