function img_behind = im_rotate_2(img_front,degree,typename)
%函数说明:图像旋转
%输入参数:旋转前图像img_front,旋转角度degree(顺时针),图片类型('gray'or'color')typename
%输出参数:旋转后图像img_behind
if strcmp(typename,'gray')
size_front = size(img_front);
degree = degree*pi/180;
M = size(img_front,1);
N = size(img_front,2);
diag_longth = sqrt(M^2+N^2)/2;
alpha = atan(M/N);
beta = atan(N/M);
if abs(degree) > pi/2
degree_mid = pi - abs(degree);
else
degree_mid = abs(degree);
end
size_behind = round([2*diag_longth*sin(alpha+degree_mid) 2*diag_longth*sin(beta+degree_mid)]);
img_behind = zeros(size_behind);
rotation_matrix = [cos(degree) sin(degree);-sin(degree) cos(degree)];
point_front = round(size_front'/2);
point_behind = round(size_behind'/2);
for x = 1:size_behind(1)
for y = 1:size_behind(2)
coord_mid = rotation_matrix*([x;y]-point_behind);
coord_end = coord_mid+point_front;
x0 = round(coord_end(1));
y0 = round(coord_end(2));
if (x0 <= size_front(1) && x0 >= 1)&&(y0 <= size_front(2) && y0 >= 1 )
img_behind(x,y) = img_front(x0,y0);
end
end
end
else if strcmp(typename,'color')
img_behind_1 = im_rotate_2(img_front(:,:,1),degree,'gray');
img_behind_2 = im_rotate_2(img_front(:,:,2),degree,'gray');
img_behind_3 = im_rotate_2(img_front(:,:,3),degree,'gray');
img_behind = zeros(size(img_behind_1));
img_behind(:,:,1) = img_behind_1;
img_behind(:,:,2) = img_behind_2;
img_behind(:,:,3) = img_behind_3;
end
end
end
![](https://img-blog.csdnimg.cn/direct/e7cbc66ee2614c12aa2887c145aedae7.png)