借鉴了 https://blog.youkuaiyun.com/ACMore_Xiong/article/details/53066464 中的代码并加以改进,原文中的旋转一图像左上角为中心,而不是图像中心位置,故旋转角度仅仅可以取(-90,90)不能满足我的要求,故修改之,并分享如下
init = imread('aligned_detect_0.555.jpg'); % 读取图像
[R, C, g] = size(init); % 获取图像大小
res = zeros( R, C); % 构造结果矩阵。每个像素点默认初始化为0(黑色)
alfa = -20 * 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;% 矩阵乘法
temp=temp+[R, C,1]';
x = uint16(mod(temp(1, 1),R));
y = uint16(mod(temp(2, 1),C));
% 变换后的位置判断是否越界
if (x <= R) & (y <= C) & (x >= 1) & (y >= 1)
res(i, j) = double(init(x, y, 1));
end
end
end;
subplot(2,1,1)
imshow(uint8(init),[]); % 显示图像
subplot(2,1,2)
imshow(uint8(res),[]); % 显示图像