matlab 调整两张图像大小相同
时间: 2024-10-23 12:12:33 浏览: 147
在MATLAB中调整两张图像大小至相同的尺寸通常涉及到两个步骤:读取图像和调整图像大小。假设你有两个变量`img1`和`img2`分别存储了两张图片:
1. **读取图像**:
使用`imread`函数读取图像文件:
```matlab
img1 = imread('path_to_image1.jpg');
img2 = imread('path_to_image2.jpg');
```
2. **调整图像大小**:
可以使用`resize`函数来调整图像到相同的宽度和高度。例如,如果你想将它们都调整到500x500像素:
```matlab
% 获取原图像尺寸
size1 = size(img1);
size2 = size(img2);
% 计算目标尺寸
target_size = [500 500];
% 确保新尺寸不会改变图像长宽比,这里采用保持纵横比缩放的方式
if size1(1) / size1(2) ~= size2(1) / size2(2)
error('The aspect ratios of the images are different, cannot resize equally.');
end
new_img1 = imresize(img1, target_size);
new_img2 = imresize(img2, target_size);
```
如果调整后的图像大小导致失真,可以考虑使用其他插值方法,如`bicubic`、`nearest`等,作为`imresize`的第二个参数。完成上述操作后,`new_img1`和`new_img2`就是大小相同的图像。
阅读全文
相关推荐

















