matlab图像任意角度旋转代码
时间: 2024-07-24 11:01:47 浏览: 59
在MATLAB中,你可以使用`imrotate`函数来对图像进行任意角度旋转。以下是一个基本的示例代码,展示了如何通过旋转矩阵(由角度和原点坐标确定)旋转图像:
```matlab
% 加载图像
img = imread('your_image_file.jpg');
% 定义旋转角度和旋转中心(可以设置为图像中心或自定义位置)
angle_degrees = your_angle; % 替换为实际角度(度)
rotation_center = [image_size(2)/2, image_size(1)/2]; % 图像中心,默认值
% 将角度转换为弧度
angle_radians = deg2rad(angle_degrees);
% 创建旋转矩阵
rotation_matrix = rotx(angle_radians); % 对x轴进行旋转,如果是其他方向则替换为对应的方向
% 执行旋转操作
rotated_img = imrotate(img, rotation_matrix, 'crop'); % 使用'crop'选项保持原始尺寸并裁剪多余的边缘
% 显示原图和旋转后的图像
subplot(1, 2, 1), imshow(img);
title('Original Image');
subplot(1, 2, 2), imshow(rotated_img);
title(['Rotated by ', num2str(angle_degrees), ' degrees']);
% 保存旋转后的图片
imwrite(rotated_img, 'rotated_image.jpg');
```
请注意,这里的`your_image_file.jpg`需要替换为你要旋转的实际图片文件名。同时,`image_size`应该是你需要旋转图像的真实尺寸。