matlab批量修改图片尺寸
时间: 2023-10-16 20:20:16 浏览: 189
你可以使用 MATLAB 中的 `imresize` 函数来批量修改图片尺寸。以下是一个简单的示例代码,可以将指定文件夹中的所有图片缩小至原来的一半尺寸:
```matlab
% 指定文件夹路径
folder_path = 'path/to/folder/';
% 获取文件夹中的所有图片文件
file_list = dir([folder_path '*.jpg']);
% 循环处理每个图片文件
for i = 1:length(file_list)
% 读取图片
img = imread([folder_path file_list(i).name]);
% 缩小图片尺寸至原来的一半
resized_img = imresize(img, 0.5);
% 保存修改后的图片
imwrite(resized_img, [folder_path 'resized_' file_list(i).name]);
end
```
你可以根据需求修改代码中的参数,比如修改缩小比例、修改保存文件的命名规则等。
相关问题
matlab批量修改图片尺寸的代码
在MATLAB中,你可以使用`imresize`函数批量修改图片的尺寸。这个函数可以调整图像的大小,保持原有图像的比例或者指定新的宽度和高度。下面是一个简单的例子,假设你有一个名为`image_folder`的文件夹,里面包含很多图片需要调整为指定的新尺寸:
```matlab
% 设置原始图像尺寸和目标尺寸
original_size = [old_width old_height]; % 原始宽度和高度
target_size = [new_width new_height]; % 新的目标宽度和高度
% 指定图片文件夹路径
folder_path = 'image_folder';
% 遍历文件夹中的所有.jpg或.png文件
files = dir(fullfile(folder_path, '*.jpg')); % 可以替换为*.png或其他扩展名
for i = 1:length(files)
% 加载图片
img = imread(fullfile(folder_path, files(i).name));
% 调整图片尺寸
resized_img = imresize(img, target_size);
% 写回原位置,覆盖原始文件(如果你不想覆盖,可以改其他保存位置)
imwrite(resized_img, fullfile(folder_path, files(i).name), 'JPEG'); % 替换为所需格式
end
matlab 批量保存图片
### 批量保存图片的方法
为了实现批量处理并保存图像,在 MATLAB 中可以编写脚本来循环读取文件夹中的所有图像,执行所需的操作(如调整大小),然后再将其保存到指定位置。下面提供了一个具体的例子说明如何完成这一操作[^1]。
```matlab
% 定义输入和输出路径
inputPath = 'C:\path\to\your\images';
outputPath = 'C:\desired\save\location';
% 获取目录下所有的jpg文件名列表
filePattern = fullfile(inputPath, '*.jpg');
allFiles = dir(filePattern);
for k = 1:length(allFiles)
baseFileName = allFiles(k).name;
fullInputFilespec = fullfile(inputPath, baseFileName);
% 读入当前迭代的图像
originalImage = imread(fullInputFilespec);
% 对图像进行必要的预处理(这里假设不做任何改变直接保存)
processedImage = imresize(originalImage, [224 224]);
% 构造新的文件名称以及完整的输出路径
[~, name, ~] = fileparts(baseFileName);
outputBaseFileName = sprintf('%s_resized.jpg', name);
fullOutputFilespec = fullfile(outputPath, outputBaseFileName);
% 将修改后的图像写入磁盘
imwrite(processedImage, fullOutputFilespec);
end
```
上述代码片段展示了怎样遍历特定文件夹内的 `.jpg` 文件,并通过 `imread()` 函数加载每张图片;接着利用 `imresize()` 方法调整尺寸至 `[224 224]` 像素大小作为示范性的处理步骤;最后借助 `imwrite()` 把经过变换的新图存回另一处设定好的存储地址中去。
阅读全文
相关推荐













